Completed
Push — 21x ( 0e3ed4...f4f4da )
by adam
17:20 queued 21s
created

testCallbackComparisonReturningNyanCat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Diff\Tests\ArrayComparer;
4
5
use Diff\ArrayComparer\ArrayComparer;
6
use Diff\ArrayComparer\StrategicArrayComparer;
7
use Diff\Tests\DiffTestCase;
8
9
/**
10
 * @covers Diff\ArrayComparer\StrategicArrayComparer
11
 *
12
 * @group Diff
13
 *
14
 * @license GPL-2.0+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class StrategicArrayComparerTest extends DiffTestCase {
18
19
	public function testCanConstruct() {
20
		new StrategicArrayComparer( $this->getMock( 'Diff\Comparer\ValueComparer' ) );
21
		$this->assertTrue( true );
22
	}
23
24
	public function testDiffArraysWithComparerThatAlwaysReturnsTrue() {
25
		$valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' );
26
27
		$valueComparer->expects( $this->any() )
28
			->method( 'valuesAreEqual' )
29
			->will( $this->returnValue( true ) );
30
31
		$arrayComparer = new StrategicArrayComparer( $valueComparer );
32
33
		$this->assertNoDifference(
34
			$arrayComparer,
35
			array( 0, 2, 4 ),
36
			array( 1, 2, 9 )
37
		);
38
39
		$this->assertNoDifference(
40
			$arrayComparer,
41
			array( 1, 2, 3 ),
42
			array( 1, 2, 3 )
43
		);
44
45
		$this->assertNoDifference(
46
			$arrayComparer,
47
			array( 'bah' ),
48
			array( 'foo', 'bar', 'baz' )
49
		);
50
51
		$this->assertNoDifference(
52
			$arrayComparer,
53
			array(),
54
			array( 'foo', 'bar', 'baz' )
55
		);
56
57
		$this->assertNoDifference(
58
			$arrayComparer,
59
			array(),
60
			array()
61
		);
62
	}
63
64
	private function assertNoDifference( ArrayComparer $arrayComparer, array $arrayOne, array $arrayTwo ) {
65
		$this->assertEquals(
66
			array(),
67
			$arrayComparer->diffArrays(
68
				$arrayOne,
69
				$arrayTwo
70
			)
71
		);
72
	}
73
74
	public function testDiffArraysWithComparerThatAlwaysReturnsFalse() {
75
		$valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' );
76
77
		$valueComparer->expects( $this->any() )
78
			->method( 'valuesAreEqual' )
79
			->will( $this->returnValue( false ) );
80
81
		$arrayComparer = new StrategicArrayComparer( $valueComparer );
82
83
		$this->assertAllDifferent(
84
			$arrayComparer,
85
			array(),
86
			array()
87
		);
88
89
		$this->assertAllDifferent(
90
			$arrayComparer,
91
			array( 1, 2, 3 ),
92
			array()
93
		);
94
95
		$this->assertAllDifferent(
96
			$arrayComparer,
97
			array( 1, 2, 3 ),
98
			array( 1, 2, 3 )
99
		);
100
101
		$this->assertAllDifferent(
102
			$arrayComparer,
103
			array(),
104
			array( 1, 2, 3 )
105
		);
106
	}
107
108
	private function assertAllDifferent( ArrayComparer $arrayComparer, array $arrayOne, array $arrayTwo ) {
109
		$this->assertEquals(
110
			$arrayOne,
111
			$arrayComparer->diffArrays(
112
				$arrayOne,
113
				$arrayTwo
114
			)
115
		);
116
	}
117
118
	public function testQuantityMattersWithReturnTrue() {
119
		$valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' );
120
121
		$valueComparer->expects( $this->any() )
122
			->method( 'valuesAreEqual' )
123
			->will( $this->returnValue( true ) );
124
125
		$arrayComparer = new StrategicArrayComparer( $valueComparer );
126
127
		$this->assertEquals(
128
			array( 1, 1, 1 ),
129
			$arrayComparer->diffArrays(
130
				array( 1, 1, 1, 1 ),
131
				array( 1 )
132
			)
133
		);
134
135
		$this->assertEquals(
136
			array( 1 ),
137
			$arrayComparer->diffArrays(
138
				array( 1, 1, 1, 1 ),
139
				array( 1, 1, 1  )
140
			)
141
		);
142
	}
143
144
	public function testQuantityMattersWithSimpleComparison() {
145
		$valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' );
146
147
		$valueComparer->expects( $this->any() )
148
			->method( 'valuesAreEqual' )
149
			->will( $this->returnCallback( function( $firstValue, $secondValue ) {
150
				return $firstValue == $secondValue;
151
			} ) );
152
153
		$arrayComparer = new StrategicArrayComparer( $valueComparer );
154
155
		$this->assertEquals(
156
			array( 1, 2, 5 ),
157
			$arrayComparer->diffArrays(
158
				array( 1, 1, 2, 3, 2, 5 ),
159
				array( 1, 2, 3, 4  )
160
			)
161
		);
162
163
		$this->assertEquals(
164
			array( 1 ),
165
			$arrayComparer->diffArrays(
166
				array( 1, 1, 1, 2, 2, 3 ),
167
				array( 1, 1, 2, 2, 3, 3, 3 )
168
			)
169
		);
170
171
		$this->assertEquals(
172
			array( 1 ),
173
			$arrayComparer->diffArrays(
174
				array( 3, 1, 2, 1, 1, 2 ),
175
				array( 1, 3, 3, 2, 2, 3, 1 )
176
			)
177
		);
178
	}
179
180
	public function testValueComparerGetsCalledWithCorrectValues() {
181
		$valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' );
182
183
		$valueComparer->expects( $this->once() )
184
			->method( 'valuesAreEqual' )
185
			->with(
186
				$this->equalTo( 1 ),
187
				$this->equalTo( 2 )
188
			)
189
			->will( $this->returnValue( true ) );
190
191
		$arrayComparer = new StrategicArrayComparer( $valueComparer );
192
193
		$arrayComparer->diffArrays(
194
			array( 1 ),
195
			array( 2 )
196
		);
197
	}
198
199
	public function testCallbackComparisonReturningNyanCat() {
200
		$valueComparer = $this->getMock( 'Diff\Comparer\ValueComparer' );
201
202
		$valueComparer->expects( $this->once() )
203
			->method( 'valuesAreEqual' )
204
			->will( $this->returnValue( '~=[,,_,,]:3' ) );
205
206
		$arrayComparer = new StrategicArrayComparer( $valueComparer );
207
208
		$this->setExpectedException( 'RuntimeException' );
209
210
		$arrayComparer->diffArrays( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) );
211
	}
212
213
}
214