1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Totem package |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE file |
6
|
|
|
* that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright Baptiste Clavié <[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/MIT-License MIT License |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Totem; |
13
|
|
|
|
14
|
|
|
use stdClass; |
15
|
|
|
|
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
|
18
|
|
|
use Totem\Snapshot\ArraySnapshot; |
19
|
|
|
use Totem\Snapshot\ObjectSnapshot; |
20
|
|
|
use Totem\Snapshot\CollectionSnapshot; |
21
|
|
|
|
22
|
|
|
class SetTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @dataProvider invalidEntryProvider |
26
|
|
|
*/ |
27
|
|
|
public function testSetChangesWithChangedStructure($old, $new, $class) |
28
|
|
|
{ |
29
|
|
|
$set = new Set; |
30
|
|
|
$set->compute(new Snapshot(['data' => $old]), new Snapshot(['data' => $new])); |
31
|
|
|
|
32
|
|
|
$this->assertInstanceOf('Totem\\Change\\' . $class, $set->getChange('1')); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function invalidEntryProvider() |
36
|
|
|
{ |
37
|
|
|
return [[['foo'], ['foo', 'bar'], 'Addition'], |
38
|
|
|
[['foo', 'bar'], ['foo'], 'Removal']]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testHasChanged() |
42
|
|
|
{ |
43
|
|
|
$old = new Snapshot(['data' => ['foo' => 'bar', 'baz' => 'fubar']]); |
44
|
|
|
$new = new Snapshot(['data' => ['foo' => 'bar', 'baz' => 'fubaz']]); |
45
|
|
|
|
46
|
|
|
$set = new Set; |
47
|
|
|
$set->compute($old, $new); |
48
|
|
|
|
49
|
|
|
$this->assertFalse($set->hasChanged('foo')); |
50
|
|
|
$this->assertFalse(isset($set['foo'])); |
51
|
|
|
$this->assertTrue($set->hasChanged('baz')); |
52
|
|
|
$this->assertTrue(isset($set['baz'])); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @expectedException OutOfBoundsException |
57
|
|
|
*/ |
58
|
|
|
public function testGetChangeWithInvalidProperty() |
59
|
|
|
{ |
60
|
|
|
$old = new Snapshot(['data' => ['foo' => 'bar']]); |
61
|
|
|
|
62
|
|
|
$set = new Set; |
63
|
|
|
$set->compute($old, $old); |
64
|
|
|
|
65
|
|
|
$set->getChange('foo'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// @todo to break up |
69
|
|
|
public function testGetChange() |
70
|
|
|
{ |
71
|
|
|
$o = [new stdClass, (object) ['foo' => 'bar']]; |
72
|
|
|
|
73
|
|
|
$old = $new = ['foo' => 'foo', |
74
|
|
|
'bar' => new ArraySnapshot(['foo', 'bar']), |
75
|
|
|
'baz' => new ObjectSnapshot($o[0]), |
76
|
|
|
'qux' => 'foo', |
77
|
|
|
'fubar' => new ObjectSnapshot($o[1]), |
78
|
|
|
'fubaz' => new ArraySnapshot(['foo', 'bar']), |
79
|
|
|
'fuqux' => new ArraySnapshot(['foo']), |
80
|
|
|
'kludge' => new ArraySnapshot(['foo']), |
81
|
|
|
'xyzzy' => new ArraySnapshot(['foo'])]; |
82
|
|
|
|
83
|
|
|
$o[1]->foo = 'baz'; |
84
|
|
|
|
85
|
|
|
$new['foo'] = 'bar'; |
86
|
|
|
$new['bar'] = new ArraySnapshot(['foo', 'baz']); |
87
|
|
|
$new['baz'] = new ObjectSnapshot($o[0]); |
88
|
|
|
$new['qux'] = 42; |
89
|
|
|
$new['fubar'] = new ObjectSnapshot($o[1]); |
90
|
|
|
$new['fubaz'] = new ArraySnapshot(['foo', 'bar', 'baz']); |
91
|
|
|
$new['fuqux'] = new ObjectSnapshot((object) []); |
92
|
|
|
$new['kludge'] = 42; |
93
|
|
|
$new['xyzzy'] = (object) []; |
94
|
|
|
|
95
|
|
|
$set = new Set; |
96
|
|
|
$set->compute(new Snapshot(['data' => $old]), new Snapshot(['data' => $new])); |
97
|
|
|
|
98
|
|
|
$this->assertInstanceOf('Totem\\Change\\Modification', $set->getChange('fuqux')); |
99
|
|
|
$this->assertInstanceOf('Totem\\Change\\Modification', $set->getChange('foo')); |
100
|
|
|
$this->assertInstanceOf('Totem\\Set', $set->getChange('bar')); |
101
|
|
|
$this->assertInstanceOf('Totem\\Change\\Modification', $set['foo']); |
102
|
|
|
$this->assertInstanceOf('Totem\\Set', $set->getChange('fubar')); |
103
|
|
|
$this->assertInstanceOf('Totem\\Change\\Modification', $set->getChange('fubar')->getChange('foo')); |
104
|
|
|
$this->assertInstanceOf('Totem\\Change\\Modification', $set->getChange('kludge')); |
105
|
|
|
$this->assertInstanceOf('Totem\\Change\\Modification', $set->getChange('xyzzy')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testIterator() |
109
|
|
|
{ |
110
|
|
|
$set = new Set; |
111
|
|
|
$set->compute(new Snapshot(['data' => ['foo']]), new Snapshot(['data' => ['bar']])); |
112
|
|
|
|
113
|
|
|
$this->assertInstanceOf('ArrayIterator', $set->getIterator()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @expectedException BadMethodCallException |
118
|
|
|
*/ |
119
|
|
|
public function testForbidenSetter() |
120
|
|
|
{ |
121
|
|
|
$set = new Set; |
122
|
|
|
$old = new Snapshot; |
123
|
|
|
$set->compute($old, $old); |
124
|
|
|
|
125
|
|
|
$set[] = 'baz'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @expectedException BadMethodCallException |
130
|
|
|
*/ |
131
|
|
|
public function testForbidenUnsetter() |
132
|
|
|
{ |
133
|
|
|
$set = new Set; |
134
|
|
|
$old = new Snapshot; |
135
|
|
|
$set->compute($old, $old); |
136
|
|
|
|
137
|
|
|
unset($set[0]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @expectedException RuntimeException |
142
|
|
|
* @expectedExceptionMessage The changeset was not computed yet ! |
143
|
|
|
*/ |
144
|
|
|
public function testHasChangedNotComputedShouldThrowException() |
145
|
|
|
{ |
146
|
|
|
$set = new Set; |
147
|
|
|
$set->hasChanged('foo'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @expectedException RuntimeException |
152
|
|
|
* @expectedExceptionMessage The changeset was not computed yet ! |
153
|
|
|
*/ |
154
|
|
|
public function testNotComputedCountShouldThrowException() |
155
|
|
|
{ |
156
|
|
|
$set = new Set; |
157
|
|
|
count($set); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @expectedException RuntimeException |
162
|
|
|
* @expectedExceptionMessage The changeset was not computed yet ! |
163
|
|
|
*/ |
164
|
|
|
public function testNotComputedIteratorShouldThrowException() |
165
|
|
|
{ |
166
|
|
|
$set = new Set; |
167
|
|
|
$set->getIterator(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testAlreadyComputedSetShouldNotRecompute() |
171
|
|
|
{ |
172
|
|
|
$old = new Snapshot(['data' => ['foo']]); |
173
|
|
|
$new = new Snapshot(['data' => ['bar']]); |
174
|
|
|
|
175
|
|
|
$set = new Set($old, $new); // implicitly compute the set in the constructor |
176
|
|
|
|
177
|
|
|
$this->assertCount(1, $set); |
178
|
|
|
$this->assertTrue($set->hasChanged(0)); |
179
|
|
|
$this->assertEquals('foo', $set->getChange(0)->getOld()); |
180
|
|
|
$this->assertEquals('bar', $set->getChange(0)->getNew()); |
181
|
|
|
|
182
|
|
|
$set->compute($old, $new); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testComputeCollections() |
186
|
|
|
{ |
187
|
|
|
$old = $new = [['foo' => 'bar', 'baz' => 'fubar'], ['foo' => 'baz', 'baz' => 'fubar']]; |
188
|
|
|
$new[0]['baz'] = 'fubaz'; |
189
|
|
|
|
190
|
|
|
$old = new CollectionSnapshot($old, '[foo]'); |
191
|
|
|
$new = new CollectionSnapshot($new, '[foo]'); |
192
|
|
|
|
193
|
|
|
$set = new Set; |
194
|
|
|
$set->compute($old, $new); |
195
|
|
|
|
196
|
|
|
$this->assertContainsOnly('integer', array_keys(iterator_to_array($set))); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** @dataProvider unaffectedSnapshotComputerProvider */ |
200
|
|
|
public function testUnaffectedCollections(AbstractSnapshot $origin, AbstractSnapshot $upstream) |
201
|
|
|
{ |
202
|
|
|
$set = new Set; |
203
|
|
|
$set->compute($origin, $upstream); |
204
|
|
|
|
205
|
|
|
$this->assertNotContainsOnly('integer',array_keys(iterator_to_array($set))); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function unaffectedSnapshotComputerProvider() |
209
|
|
|
{ |
210
|
|
|
$old = ['foo' => 'bar', 'baz' => 'fubar']; |
211
|
|
|
|
212
|
|
|
return [[new CollectionSnapshot([$old], '[foo]'), new ArraySnapshot($old)], |
213
|
|
|
[new ArraySnapshot($old), new CollectionSnapshot([$old], '[foo]')], |
214
|
|
|
[new ArraySnapshot($old), new ArraySnapshot(array_merge($old, ['baz' => 'fubaz']))]]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|