Skip to content

Commit deba26f

Browse files
Maks3wweierophinney
authored andcommitted
Simplify test
1 parent 6d3373d commit deba26f

File tree

6 files changed

+17
-81
lines changed

6 files changed

+17
-81
lines changed

test/FastPriorityQueueTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,7 @@ public function testCanRetrieveQueueAsArray()
128128

129129
public function testIteratorFunctions()
130130
{
131-
$this->queue->rewind();
132-
133-
$i = 0;
134-
while ($this->queue->valid()) {
135-
$key = $this->queue->key();
136-
$value = $this->queue->current();
137-
$this->assertEquals($this->expected[$i], $value);
138-
$this->queue->next();
139-
++$i;
140-
}
141-
$this->assertFalse($this->queue->valid());
131+
$this->assertEquals($this->expected, iterator_to_array($this->queue));
142132
}
143133

144134
public function testRewindOperation()

test/PriorityListTest.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ public function testLIFOOnly()
101101
$this->list->insert('foobar', new \stdClass());
102102
$this->list->insert('barbaz', new \stdClass());
103103

104-
$orders = [];
105-
106-
foreach ($this->list as $key => $value) {
107-
$orders[] = $key;
108-
}
104+
$orders = array_keys(iterator_to_array($this->list));
109105

110106
$this->assertEquals(['barbaz', 'foobar', 'baz', 'bar', 'foo'], $orders);
111107
}
@@ -116,11 +112,7 @@ public function testPriorityOnly()
116112
$this->list->insert('bar', new \stdClass(), 0);
117113
$this->list->insert('baz', new \stdClass(), 2);
118114

119-
$orders = [];
120-
121-
foreach ($this->list as $key => $value) {
122-
$orders[] = $key;
123-
}
115+
$orders = array_keys(iterator_to_array($this->list));
124116

125117
$this->assertEquals(['baz', 'foo', 'bar'], $orders);
126118
}
@@ -131,11 +123,7 @@ public function testLIFOWithPriority()
131123
$this->list->insert('bar', new \stdClass(), 0);
132124
$this->list->insert('baz', new \stdClass(), 1);
133125

134-
$orders = [];
135-
136-
foreach ($this->list as $key => $value) {
137-
$orders[] = $key;
138-
}
126+
$orders = array_keys(iterator_to_array($this->list));
139127

140128
$this->assertEquals(['baz', 'bar', 'foo'], $orders);
141129
}
@@ -147,11 +135,7 @@ public function testFIFOWithPriority()
147135
$this->list->insert('bar', new \stdClass(), 0);
148136
$this->list->insert('baz', new \stdClass(), 1);
149137

150-
$orders = [];
151-
152-
foreach ($this->list as $key => $value) {
153-
$orders[] = $key;
154-
}
138+
$orders = array_keys(iterator_to_array($this->list));
155139

156140
$this->assertEquals(['baz', 'foo', 'bar'], $orders);
157141
}
@@ -165,11 +149,7 @@ public function testFIFOOnly()
165149
$this->list->insert('foobar', new \stdClass());
166150
$this->list->insert('barbaz', new \stdClass());
167151

168-
$orders = [];
169-
170-
foreach ($this->list as $key => $value) {
171-
$orders[] = $key;
172-
}
152+
$orders = array_keys(iterator_to_array($this->list));
173153

174154
$this->assertEquals(['foo', 'bar', 'baz', 'foobar', 'barbaz'], $orders);
175155
}
@@ -180,11 +160,7 @@ public function testPriorityWithNegativesAndNull()
180160
$this->list->insert('bar', new \stdClass(), 1);
181161
$this->list->insert('baz', new \stdClass(), -1);
182162

183-
$orders = [];
184-
185-
foreach ($this->list as $key => $value) {
186-
$orders[] = $key;
187-
}
163+
$orders = array_keys(iterator_to_array($this->list));
188164

189165
$this->assertEquals(['bar', 'foo', 'baz'], $orders);
190166
}

test/PriorityQueueTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ public function testSerializationAndDeserializationShouldMaintainState()
3737
$count = count($this->queue);
3838
$this->assertSame($count, count($unserialized), 'Expected count ' . $count . '; received ' . count($unserialized));
3939

40-
$expected = [];
41-
foreach ($this->queue as $item) {
42-
$expected[] = $item;
43-
}
44-
$test = [];
45-
foreach ($unserialized as $item) {
46-
$test[] = $item;
47-
}
40+
$expected = iterator_to_array($this->queue);
41+
$test = iterator_to_array($unserialized);
4842
$this->assertSame($expected, $test, 'Expected: ' . var_export($expected, 1) . "\nReceived:" . var_export($test, 1));
4943
}
5044

@@ -101,10 +95,7 @@ public function testCanRemoveItemFromQueue()
10195
{
10296
$this->queue->remove('baz');
10397
$expected = ['bar', 'foo', 'bat'];
104-
$test = [];
105-
foreach ($this->queue as $item) {
106-
$test[] = $item;
107-
}
98+
$test = array_values(iterator_to_array($this->queue));
10899
$this->assertEquals($expected, $test);
109100
}
110101

test/SplPriorityQueueTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public function testMaintainsInsertOrderForDataOfEqualPriority()
3939
$queue->insert('bat', 1000);
4040

4141
$expected = ['foo', 'bar', 'baz', 'bat'];
42-
$test = [];
43-
foreach ($queue as $datum) {
44-
$test[] = $datum;
45-
}
42+
$test = array_values(iterator_to_array($queue));
4643
$this->assertEquals($expected, $test);
4744
}
4845

@@ -53,14 +50,8 @@ public function testSerializationAndDeserializationShouldMaintainState()
5350
$count = count($this->queue);
5451
$this->assertSame($count, count($unserialized), 'Expected count ' . $count . '; received ' . count($unserialized));
5552

56-
$expected = [];
57-
foreach ($this->queue as $item) {
58-
$expected[] = $item;
59-
}
60-
$test = [];
61-
foreach ($unserialized as $item) {
62-
$test[] = $item;
63-
}
53+
$expected = iterator_to_array($this->queue);
54+
$test = iterator_to_array($unserialized);
6455
$this->assertSame($expected, $test, 'Expected: ' . var_export($expected, 1) . "\nReceived:" . var_export($test, 1));
6556
}
6657

test/SplQueueTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,8 @@ public function testSerializationAndDeserializationShouldMaintainState()
3636
$count = count($this->queue);
3737
$this->assertSame($count, count($unserialized));
3838

39-
$expected = [];
40-
foreach ($this->queue as $item) {
41-
$expected[] = $item;
42-
}
43-
$test = [];
44-
foreach ($unserialized as $item) {
45-
$test[] = $item;
46-
}
39+
$expected = iterator_to_array($this->queue);
40+
$test = iterator_to_array($unserialized);
4741
$this->assertSame($expected, $test);
4842
}
4943

test/SplStackTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ public function testSerializationAndDeserializationShouldMaintainState()
3737
$count = count($this->stack);
3838
$this->assertSame($count, count($unserialized));
3939

40-
$expected = [];
41-
foreach ($this->stack as $item) {
42-
$expected[] = $item;
43-
}
44-
$test = [];
45-
foreach ($unserialized as $item) {
46-
$test[] = $item;
47-
}
40+
$expected = iterator_to_array($this->stack);
41+
$test = iterator_to_array($unserialized);
4842
$this->assertSame($expected, $test);
4943
}
5044

0 commit comments

Comments
 (0)