Passed
Push — master ( e3acee...71d19a )
by Jeroen De
02:21
created

tests/unit/Differ/ListDifferTest.php (1 issue)

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
declare( strict_types = 1 );
4
5
namespace Diff\Tests\Differ;
6
7
use Diff\ArrayComparer\NativeArrayComparer;
8
use Diff\ArrayComparer\StrictArrayComparer;
9
use Diff\Differ\Differ;
10
use Diff\Differ\ListDiffer;
11
use Diff\DiffOp\DiffOpAdd;
12
use Diff\DiffOp\DiffOpRemove;
13
use Diff\Tests\DiffTestCase;
14
15
/**
16
 * @covers \Diff\Differ\ListDiffer
17
 *
18
 * @group Diff
19
 * @group Differ
20
 *
21
 * @license BSD-3-Clause
22
 * @author Jeroen De Dauw < [email protected] >
23
 */
24
class ListDifferTest extends DiffTestCase {
25
26
	public function arrayComparerProvider() {
27
		$add = array( new DiffOpAdd( 1 ) );
28
29
		return array(
30
			'null' => array( null, $add ),
31
			'native object' => array( new NativeArrayComparer(), array() ),
32
			'strict object' => array( new StrictArrayComparer(), $add ),
33
		);
34
	}
35
36
	/**
37
	 * @dataProvider arrayComparerProvider
38
	 */
39
	public function testConstructor( $arrayComparer, array $expected ) {
40
		$differ = new ListDiffer( $arrayComparer );
41
		$diff = $differ->doDiff( array( 1 ), array( 1, 1 ) );
42
		$this->assertEquals( $expected, $diff );
43
	}
44
45
	/**
46
	 * Returns those that both work for native and strict mode.
47
	 */
48
	private function getCommonArgLists() {
49
		$argLists = array();
50
51
		$old = array();
52
		$new = array();
53
		$expected = array();
54
55
		$argLists[] = array( $old, $new, $expected,
56
			'There should be no difference between empty arrays' );
57
58
		$old = array( 42 );
59
		$new = array( 42 );
60
		$expected = array();
61
62
		$argLists[] = array( $old, $new, $expected,
63
			'There should be no difference between arrays with the same element' );
64
65
		$old = array( 42, 'ohi', 4.2, false );
66
		$new = array( 42, 'ohi', 4.2, false );
67
		$expected = array();
68
69
		$argLists[] = array( $old, $new, $expected,
70
			'There should be no difference between arrays with the same elements' );
71
72
		$old = array( 42, 'ohi', 4.2, false );
73
		$new = array( false, 4.2, 'ohi', 42 );
74
		$expected = array();
75
76
		$argLists[] = array( $old, $new, $expected,
77
			'There should be no difference between arrays with the same elements even when not ordered the same' );
78
79
		$old = array();
80
		$new = array( 42 );
81
		$expected = array( new DiffOpAdd( 42 ) );
82
83
		$argLists[] = array( $old, $new, $expected,
84
			'An array with a single element should be an add operation different from an empty array' );
85
86
		$old = array( 42 );
87
		$new = array();
88
		$expected = array( new DiffOpRemove( 42 ) );
89
90
		$argLists[] = array( $old, $new, $expected,
91
			'An empty array should be a remove operation different from an array with one element' );
92
93
		$old = array( 1 );
94
		$new = array( 2 );
95
		$expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) );
96
97
		$argLists[] = array( $old, $new, $expected,
98
			'Two arrays with a single different element should differ by an add and a remove op' );
99
100
		$old = array( 9001, 42, 1, 0 );
101
		$new = array( 9001, 2, 0, 42 );
102
		$expected = array( new DiffOpRemove( 1 ), new DiffOpAdd( 2 ) );
103
104
		$argLists[] = array(
105
			$old,
106
			$new,
107
			$expected,
108
			'Two arrays with a single different element should differ by an add'
109
				. 'and a remove op even when they share identical elements'
110
		);
111
112
		return $argLists;
113
	}
114
115
	public function toDiffProvider() {
116
		$argLists = $this->getCommonArgLists();
117
118
		$old = array( 42, 42 );
119
		$new = array( 42 );
120
		$expected = array( new DiffOpRemove( 42 ) );
121
122
		$argLists[] = array( $old, $new, $expected,
123
			'[42, 42] to [42] should [rem(42)]' );
124
125
		$old = array( 42 );
126
		$new = array( 42, 42 );
127
		$expected = array( new DiffOpAdd( 42 ) );
128
129
		$argLists[] = array( $old, $new, $expected,
130
			'[42] to [42, 42] should [add(42)]' );
131
132
		$old = array( '42' );
133
		$new = array( 42 );
134
		$expected = array( new DiffOpRemove( '42' ), new DiffOpAdd( 42 ) );
135
136
		$argLists[] = array( $old, $new, $expected,
137
			'["42"] to [42] should [rem("42"), add(42)]' );
138
139
		$old = array( array( 1 ) );
140
		$new = array( array( 2 ) );
141
		$expected = array( new DiffOpRemove( array( 1 ) ), new DiffOpAdd( array( 2 ) ) );
142
143
		$argLists[] = array( $old, $new, $expected,
144
			'[[1]] to [[2]] should [rem([1]), add([2])]' );
145
146
		$old = array( array( 2 ) );
147
		$new = array( array( 2 ) );
148
		$expected = array();
149
150
		$argLists[] = array( $old, $new, $expected,
151
			'[[2]] to [[2]] should result in an empty diff' );
152
153
		// test "soft" object comparison
154
		$obj1 = new \stdClass();
155
		$obj2 = new \stdClass();
156
		$objX = new \stdClass();
157
158
		$obj1->test = 'Test';
159
		$obj2->test = 'Test';
160
		$objX->xest = 'Test';
161
162
		$old = array( $obj1 );
163
		$new = array( $obj2 );
164
		$expected = array( );
165
166
		$argLists[] = array( $old, $new, $expected,
167
			'Two arrays containing equivalent objects should result in an empty diff' );
168
169
		$old = array( $obj1 );
170
		$new = array( $objX );
171
		$expected = array( new DiffOpRemove( $obj1 ), new DiffOpAdd( $objX )  );
172
173
		$argLists[] = array( $old, $new, $expected,
174
			'Two arrays containing different objects of the same type should result in an add and a remove op.' );
175
176
		return $argLists;
177
	}
178
179
	/**
180
	 * @dataProvider toDiffProvider
181
	 */
182
	public function testDoDiff( $old, $new, $expected, $message = '' ) {
183
		$this->doTestDiff( new ListDiffer(), $old, $new, $expected, $message );
184
	}
185
186
	public function toDiffNativeProvider() {
187
		$argLists = $this->getCommonArgLists();
188
189
		$old = array( '42' );
190
		$new = array( 42 );
191
		$expected = array();
192
193
		$argLists[] = array( $old, $new, $expected,
194
			'["42"] to [42] should result in an empty diff' );
195
196
		$old = array( 42, 42 );
197
		$new = array( 42 );
198
		$expected = array();
199
200
		$argLists[] = array( $old, $new, $expected,
201
			'[42, 42] to [42] should result in an empty diff' );
202
203
		$old = array( 42 );
204
		$new = array( 42, 42 );
205
		$expected = array();
206
207
		$argLists[] = array( $old, $new, $expected,
208
			'[42] to [42, 42] should result in an empty diff' );
209
210
		// TODO: test toString()-based object comparison
211
212
		return $argLists;
213
	}
214
215
	/**
216
	 * @dataProvider toDiffNativeProvider
217
	 */
218
	public function testDoNativeDiff( $old, $new, $expected, $message = '' ) {
219
		$this->doTestDiff( new ListDiffer( new NativeArrayComparer() ), $old, $new, $expected, $message );
220
	}
221
222
	private function doTestDiff( Differ $differ, $old, $new, $expected, $message ) {
223
		$actual = $differ->doDiff( $old, $new );
224
225
		$this->assertArrayEquals( $expected, $actual, false, false, $message );
226
	}
227
228
	public function testDiffCallsArrayComparatorCorrectly() {
229
		$arrayComparer = $this->createMock( 'Diff\ArrayComparer\ArrayComparer' );
230
231
		$arrayComparer->expects( $this->exactly( 2 ) )
232
			->method( 'diffArrays' )
233
			->with(
234
				$this->equalTo( array( 42 ) ),
235
				$this->equalTo( array( 42 ) )
236
			)
237
			->will( $this->returnValue( array() ) );
238
239
		$differ = new ListDiffer( $arrayComparer );
0 ignored issues
show
$arrayComparer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Diff\ArrayComparer\ArrayComparer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
240
241
		$differ->doDiff( array( 42 ), array( 42 ) );
242
	}
243
244
}
245