Completed
Push — master ( f4f4da...605cf2 )
by Jeroen De
41s
created

tests/phpunit/Differ/CallbackListDifferTest.php (6 issues)

Severity

Upgrade to new PHP Analysis Engine

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
 * @license GPL-2.0+
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
		$old = array( 42 );
36
		$new = array( 42 );
37
		$expected = array();
38
39
		$argLists[] = array( $old, $new, $expected,
40
			'There should be no difference between arrays with the same element' );
41
42
		$old = array( 42, 'ohi', 4.2, false );
43
		$new = array( 42, 'ohi', 4.2, false );
44
		$expected = array();
45
46
		$argLists[] = array( $old, $new, $expected,
47
			'There should be no difference between arrays with the same elements' );
48
49
		$old = array( 42, 'ohi', 4.2, false );
50
		$new = array( false, 4.2, 'ohi', 42 );
51
		$expected = array();
52
53
		$argLists[] = array( $old, $new, $expected,
54
			'There should be no difference between arrays with the same elements even when not ordered the same' );
55
56
		$old = array();
57
		$new = array( 42 );
58
		$expected = array( new DiffOpAdd( 42 ) );
59
60
		$argLists[] = array( $old, $new, $expected,
61
			'An array with a single element should be an add operation different from an empty array' );
62
63
		$old = array( 42 );
64
		$new = array();
65
		$expected = array( new DiffOpRemove( 42 ) );
66
67
		$argLists[] = array( $old, $new, $expected,
68
			'An empty array should be a remove operation different from an array with one element' );
69
70
		$old = array( 1 );
71
		$new = array( 2 );
72
		$expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) );
73
74
		$argLists[] = array( $old, $new, $expected,
75
			'Two arrays with a single different element should differ by an add and a remove op' );
76
77
		$old = array( 9001, 42, 1, 0 );
78
		$new = array( 9001, 2, 0, 42 );
79
		$expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) );
80
81
		$argLists[] = array(
82
			$old,
83
			$new,
84
			$expected,
85
			'Two arrays with a single different element should differ by an add '
86
				. 'and a remove op even when they share identical elements'
87
		);
88
89
		return $argLists;
90
	}
91
92
	public function toDiffProvider() {
93
		$argLists = $this->getCommonArgLists();
94
95
		$old = array( 42, 42 );
96
		$new = array( 42 );
97
		$expected = array( new DiffOpRemove( 42 ) );
98
99
		$argLists[] = array( $old, $new, $expected,
100
			'[42, 42] to [42] should [rem(42)]' );
101
102
		$old = array( 42 );
103
		$new = array( 42, 42 );
104
		$expected = array( new DiffOpAdd( 42 ) );
105
106
		$argLists[] = array( $old, $new, $expected,
107
			'[42] to [42, 42] should [add(42)]' );
108
109
		$old = array( '42' );
110
		$new = array( 42 );
111
		$expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) );
112
113
		$argLists[] = array( $old, $new, $expected,
114
			'["42"] to [42] should [rem("42"), add(42)]' );
115
116
		$old = array( array( 1 ) );
117
		$new = array( array( 2 ) );
118
		$expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) );
119
120
		$argLists[] = array( $old, $new, $expected,
121
			'[[1]] to [[2]] should [rem([1]), add([2])]' );
122
123
		$old = array( array( 2 ) );
124
		$new = array( array( 2 ) );
125
		$expected = array();
126
127
		$argLists[] = array( $old, $new, $expected,
128
			'[[2]] to [[2]] should result in an empty diff' );
129
130
		// test "soft" object comparison
131
		$obj1 = new \stdClass();
132
		$obj2 = new \stdClass();
133
		$objX = new \stdClass();
134
135
		$obj1->test = 'Test';
136
		$obj2->test = 'Test';
137
		$objX->xest = 'Test';
138
139
		$old = array( $obj1 );
140
		$new = array( $obj2 );
141
		$expected = array( );
142
143
		$argLists[] = array( $old, $new, $expected,
144
			'Two arrays containing equivalent objects should result in an empty diff' );
145
146
		$old = array( $obj1 );
147
		$new = array( $objX );
148
		$expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX )  );
149
150
		$argLists[] = array( $old, $new, $expected,
151
			'Two arrays containing different objects of the same type should result in an add and a remove op.' );
152
153
		return $argLists;
154
	}
155
156
	/**
157
	 * @dataProvider toDiffProvider
158
	 */
159
	public function testDoDiff( $old, $new, $expected, $message = '' ) {
160
		$callback = function( $foo, $bar ) {
161
			return is_object( $foo ) ? $foo == $bar : $foo === $bar;
162
		};
163
164
		$this->doTestDiff( new CallbackListDiffer( $callback ), $old, $new, $expected, $message );
165
	}
166
167
	private function doTestDiff( Differ $differ, $old, $new, $expected, $message ) {
168
		$actual = $differ->doDiff( $old, $new );
169
170
		$this->assertArrayEquals( $expected, $actual, false, false, $message );
171
	}
172
173
	public function testCallbackComparisonReturningFalse() {
174
		$differ = new CallbackListDiffer( function( $foo, $bar ) {
0 ignored issues
show
The parameter $foo is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $bar is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
			return false;
176
		} );
177
178
		$actual = $differ->doDiff( array( 1, '2' ), array( 1, '2', 'foo' ) );
179
180
		$expected = array(
181
			new DiffOpAdd( 1 ),
182
			new DiffOpAdd( '2' ),
183
			new DiffOpAdd( 'foo' ),
184
			new DiffOpRemove( 1 ),
185
			new DiffOpRemove( '2' ),
186
		);
187
188
		$this->assertArrayEquals(
189
			$expected, $actual, false, false,
190
			'All elements should be removed and added when comparison callback always returns false'
191
		);
192
	}
193
194
	public function testCallbackComparisonReturningTrue() {
195
		$differ = new CallbackListDiffer( function( $foo, $bar ) {
0 ignored issues
show
The parameter $foo is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $bar is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
196
			return true;
197
		} );
198
199
		$actual = $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) );
200
201
		$expected = array();
202
203
		$this->assertArrayEquals(
204
			$expected, $actual, false, false,
205
			'No elements should be removed or added when comparison callback always returns true'
206
		);
207
	}
208
209
	public function testCallbackComparisonReturningNyanCat() {
210
		$differ = new CallbackListDiffer( function( $foo, $bar ) {
0 ignored issues
show
The parameter $foo is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $bar is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
211
			return '~=[,,_,,]:3';
212
		} );
213
214
		$this->setExpectedException( 'RuntimeException' );
215
216
		$differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) );
217
	}
218
219
}
220