-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathAlterableResultIteratorTest.php
152 lines (110 loc) · 4.83 KB
/
AlterableResultIteratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
namespace TheCodingMachine\TDBM;
use PHPUnit\Framework\TestCase;
class AlterableResultIteratorTest extends TestCase
{
public function testUnalteredResultSet(): void
{
$a = (object) ['a' => 'a'];
$b = (object) ['b' => 'c'];
$c = (object) ['b' => 'c'];
$iterator = new \ArrayIterator([$a, $b, $c]);
$alterableResultIterator = new AlterableResultIterator($iterator);
$this->assertEquals([$a, $b, $c], $alterableResultIterator->toArray());
$this->assertEquals([$a, $b, $c], $alterableResultIterator->jsonSerialize());
$this->assertEquals($a, $alterableResultIterator[0]);
$this->assertTrue(isset($alterableResultIterator[0]));
$this->assertCount(3, $alterableResultIterator);
$this->assertEquals($a, $alterableResultIterator->first());
}
public function testEmptyResultSet(): void
{
$alterableResultIterator = new AlterableResultIterator();
$this->assertEquals([], $alterableResultIterator->toArray());
$this->assertEquals([], iterator_to_array($alterableResultIterator->getIterator()));
$this->assertNull($alterableResultIterator->first());
}
public function testAlterEmptyResultSet(): void
{
$alterableResultIterator = new AlterableResultIterator();
$a = (object) ['a' => 'a'];
$b = (object) ['b' => 'c'];
$c = (object) ['b' => 'c'];
$alterableResultIterator->add($a);
$alterableResultIterator->add($b);
$alterableResultIterator->remove($b);
$alterableResultIterator->remove($c);
$this->assertEquals([$a], $alterableResultIterator->toArray());
}
public function testAlterFilledResultSet(): void
{
$a = (object) ['a' => 'a'];
$b = (object) ['b' => 'c'];
$c = (object) ['b' => 'c'];
$iterator = new \ArrayIterator([$a, $b]);
$alterableResultIterator = new AlterableResultIterator($iterator);
$alterableResultIterator->add($c);
$alterableResultIterator->remove($b);
$this->assertEquals([$a, $c], $alterableResultIterator->toArray());
$this->assertEquals([$c], iterator_to_array($alterableResultIterator->take(1, 1)));
}
public function testAddAfterToArray(): void
{
$a = (object) ['a' => 'a'];
$b = (object) ['b' => 'c'];
$c = (object) ['b' => 'c'];
$iterator = new \ArrayIterator([$a, $b]);
$alterableResultIterator = new AlterableResultIterator($iterator);
$this->assertEquals([$a, $b], $alterableResultIterator->toArray());
$alterableResultIterator->add($c);
$alterableResultIterator->remove($b);
$this->assertEquals([$a, $c], $alterableResultIterator->toArray());
}
public function testGetIterator(): void
{
$a = (object) ['a' => 'a'];
$b = (object) ['b' => 'c'];
$c = (object) ['b' => 'c'];
$iterator = new \ArrayIterator([$a, $b]);
$alterableResultIterator = new AlterableResultIterator($iterator);
// Test getting the iterator with no alterations (should serve the initial iterator)
$this->assertEquals([$a, $b], iterator_to_array($alterableResultIterator->getIterator()));
$this->assertInstanceOf(\ArrayIterator::class, $alterableResultIterator->getIterator());
$alterableResultIterator->add($c);
$this->assertEquals([$a, $b, $c], iterator_to_array($alterableResultIterator->getIterator()));
$this->assertInstanceOf(\ArrayIterator::class, $alterableResultIterator->getIterator());
}
public function testSetException(): void
{
$alterableResultIterator = new AlterableResultIterator();
$this->expectException(TDBMInvalidOperationException::class);
$alterableResultIterator[0] = 'foo';
}
public function testUnsetException(): void
{
$alterableResultIterator = new AlterableResultIterator();
$this->expectException(TDBMInvalidOperationException::class);
unset($alterableResultIterator[0]);
}
public function testMap(): void
{
$a = (object) ['foo' => 'bar'];
$iterator = new \ArrayIterator([$a]);
$alterableResultIterator = new AlterableResultIterator($iterator);
$map = $alterableResultIterator->map(function ($item) {
return $item->foo;
});
$this->assertEquals(['bar'], iterator_to_array($map));
}
public function testSetIterator(): void
{
$alterableResultIterator = new AlterableResultIterator();
$a = (object) ['a' => 'a'];
$b = (object) ['b' => 'c'];
$c = (object) ['b' => 'c'];
$iterator = new \ArrayIterator([$a, $b, $c]);
$alterableResultIterator->setResultIterator($iterator);
$this->assertEquals([$a, $b, $c], $alterableResultIterator->toArray());
}
}