These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Diff\Tests\Differ; |
||
4 | |||
5 | use Diff\Differ\CallbackListDiffer; |
||
6 | use Diff\Differ\Differ; |
||
7 | use Diff\DiffOp\DiffOpAdd; |
||
8 | use Diff\DiffOp\DiffOpRemove; |
||
9 | use Diff\Tests\DiffTestCase; |
||
10 | |||
11 | /** |
||
12 | * @covers Diff\Differ\CallbackListDiffer |
||
13 | * |
||
14 | * @group Diff |
||
15 | * @group Differ |
||
16 | * |
||
17 | * @licence GNU GPL v2+ |
||
18 | * @author Jeroen De Dauw < [email protected] > |
||
19 | */ |
||
20 | class CallbackListDifferTest extends DiffTestCase { |
||
21 | |||
22 | /** |
||
23 | * Returns those that both work for native and strict mode. |
||
24 | */ |
||
25 | private function getCommonArgLists() { |
||
26 | $argLists = array(); |
||
27 | |||
28 | $old = array(); |
||
29 | $new = array(); |
||
30 | $expected = array(); |
||
31 | |||
32 | $argLists[] = array( $old, $new, $expected, |
||
33 | 'There should be no difference between empty arrays' ); |
||
34 | |||
35 | |||
36 | $old = array( 42 ); |
||
37 | $new = array( 42 ); |
||
38 | $expected = array(); |
||
39 | |||
40 | $argLists[] = array( $old, $new, $expected, |
||
41 | 'There should be no difference between arrays with the same element' ); |
||
42 | |||
43 | |||
44 | $old = array( 42, 'ohi', 4.2, false ); |
||
45 | $new = array( 42, 'ohi', 4.2, false ); |
||
46 | $expected = array(); |
||
47 | |||
48 | $argLists[] = array( $old, $new, $expected, |
||
49 | 'There should be no difference between arrays with the same elements' ); |
||
50 | |||
51 | |||
52 | $old = array( 42, 'ohi', 4.2, false ); |
||
53 | $new = array( false, 4.2, 'ohi', 42 ); |
||
54 | $expected = array(); |
||
55 | |||
56 | $argLists[] = array( $old, $new, $expected, |
||
57 | 'There should be no difference between arrays with the same elements even when not ordered the same' ); |
||
58 | |||
59 | |||
60 | $old = array(); |
||
61 | $new = array( 42 ); |
||
62 | $expected = array( new DiffOpAdd( 42 ) ); |
||
63 | |||
64 | $argLists[] = array( $old, $new, $expected, |
||
65 | 'An array with a single element should be an add operation different from an empty array' ); |
||
66 | |||
67 | |||
68 | $old = array( 42 ); |
||
69 | $new = array(); |
||
70 | $expected = array( new DiffOpRemove( 42 ) ); |
||
71 | |||
72 | $argLists[] = array( $old, $new, $expected, |
||
73 | 'An empty array should be a remove operation different from an array with one element' ); |
||
74 | |||
75 | |||
76 | $old = array( 1 ); |
||
77 | $new = array( 2 ); |
||
78 | $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); |
||
79 | |||
80 | $argLists[] = array( $old, $new, $expected, |
||
81 | 'Two arrays with a single different element should differ by an add and a remove op' ); |
||
82 | |||
83 | |||
84 | $old = array( 9001, 42, 1, 0 ); |
||
85 | $new = array( 9001, 2, 0, 42 ); |
||
86 | $expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) ); |
||
87 | |||
88 | $argLists[] = array( |
||
89 | $old, |
||
90 | $new, |
||
91 | $expected, |
||
92 | 'Two arrays with a single different element should differ by an add ' |
||
93 | . 'and a remove op even when they share identical elements' |
||
94 | ); |
||
95 | |||
96 | return $argLists; |
||
97 | } |
||
98 | |||
99 | public function toDiffProvider() { |
||
100 | $argLists = $this->getCommonArgLists(); |
||
101 | |||
102 | $old = array( 42, 42 ); |
||
103 | $new = array( 42 ); |
||
104 | $expected = array( new DiffOpRemove( 42 ) ); |
||
105 | |||
106 | $argLists[] = array( $old, $new, $expected, |
||
107 | '[42, 42] to [42] should [rem(42)]' ); |
||
108 | |||
109 | |||
110 | $old = array( 42 ); |
||
111 | $new = array( 42, 42 ); |
||
112 | $expected = array( new DiffOpAdd( 42 ) ); |
||
113 | |||
114 | $argLists[] = array( $old, $new, $expected, |
||
115 | '[42] to [42, 42] should [add(42)]' ); |
||
116 | |||
117 | |||
118 | $old = array( '42' ); |
||
119 | $new = array( 42 ); |
||
120 | $expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) ); |
||
121 | |||
122 | $argLists[] = array( $old, $new, $expected, |
||
123 | '["42"] to [42] should [rem("42"), add(42)]' ); |
||
124 | |||
125 | |||
126 | $old = array( array( 1 ) ); |
||
127 | $new = array( array( 2 ) ); |
||
128 | $expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) ); |
||
129 | |||
130 | $argLists[] = array( $old, $new, $expected, |
||
131 | '[[1]] to [[2]] should [rem([1]), add([2])]' ); |
||
132 | |||
133 | |||
134 | $old = array( array( 2 ) ); |
||
135 | $new = array( array( 2 ) ); |
||
136 | $expected = array(); |
||
137 | |||
138 | $argLists[] = array( $old, $new, $expected, |
||
139 | '[[2]] to [[2]] should result in an empty diff' ); |
||
140 | |||
141 | // test "soft" object comparison |
||
142 | $obj1 = new \stdClass(); |
||
143 | $obj2 = new \stdClass(); |
||
144 | $objX = new \stdClass(); |
||
145 | |||
146 | $obj1->test = 'Test'; |
||
147 | $obj2->test = 'Test'; |
||
148 | $objX->xest = 'Test'; |
||
149 | |||
150 | $old = array( $obj1 ); |
||
151 | $new = array( $obj2 ); |
||
152 | $expected = array( ); |
||
153 | |||
154 | $argLists[] = array( $old, $new, $expected, |
||
155 | 'Two arrays containing equivalent objects should result in an empty diff' ); |
||
156 | |||
157 | $old = array( $obj1 ); |
||
158 | $new = array( $objX ); |
||
159 | $expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX ) ); |
||
160 | |||
161 | $argLists[] = array( $old, $new, $expected, |
||
162 | 'Two arrays containing different objects of the same type should result in an add and a remove op.' ); |
||
163 | |||
164 | return $argLists; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @dataProvider toDiffProvider |
||
169 | */ |
||
170 | public function testDoDiff( $old, $new, $expected, $message = '' ) { |
||
171 | $callback = function( $foo, $bar ) { |
||
172 | return is_object( $foo ) ? $foo == $bar : $foo === $bar; |
||
173 | }; |
||
174 | |||
175 | $this->doTestDiff( new CallbackListDiffer( $callback ), $old, $new, $expected, $message ); |
||
176 | } |
||
177 | |||
178 | private function doTestDiff( Differ $differ, $old, $new, $expected, $message ) { |
||
179 | $actual = $differ->doDiff( $old, $new ); |
||
180 | |||
181 | $this->assertArrayEquals( $expected, $actual, false, false, $message ); |
||
182 | } |
||
183 | |||
184 | public function testCallbackComparisonReturningFalse() { |
||
185 | $differ = new CallbackListDiffer( function( $foo, $bar ) { |
||
0 ignored issues
–
show
|
|||
186 | return false; |
||
187 | } ); |
||
188 | |||
189 | $actual = $differ->doDiff( array( 1, '2' ), array( 1, '2', 'foo' ) ); |
||
190 | |||
191 | $expected = array( |
||
192 | new DiffOpAdd( 1 ), |
||
193 | new DiffOpAdd( '2' ), |
||
194 | new DiffOpAdd( 'foo' ), |
||
195 | new DiffOpRemove( 1 ), |
||
196 | new DiffOpRemove( '2' ), |
||
197 | ); |
||
198 | |||
199 | $this->assertArrayEquals( |
||
200 | $expected, $actual, false, false, |
||
201 | 'All elements should be removed and added when comparison callback always returns false' |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | public function testCallbackComparisonReturningTrue() { |
||
206 | $differ = new CallbackListDiffer( function( $foo, $bar ) { |
||
0 ignored issues
–
show
|
|||
207 | return true; |
||
208 | } ); |
||
209 | |||
210 | $actual = $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); |
||
211 | |||
212 | $expected = array(); |
||
213 | |||
214 | $this->assertArrayEquals( |
||
215 | $expected, $actual, false, false, |
||
216 | 'No elements should be removed or added when comparison callback always returns true' |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | public function testCallbackComparisonReturningNyanCat() { |
||
221 | $differ = new CallbackListDiffer( function( $foo, $bar ) { |
||
0 ignored issues
–
show
|
|||
222 | return '~=[,,_,,]:3'; |
||
223 | } ); |
||
224 | |||
225 | $this->setExpectedException( 'RuntimeException' ); |
||
226 | |||
227 | $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) ); |
||
228 | } |
||
229 | |||
230 | } |
||
231 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.