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