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

ListDiffTest::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Diff\Tests\DiffOp\Diff;
4
5
use Diff\ArrayComparer\NativeArrayComparer;
6
use Diff\Differ\ListDiffer;
7
use Diff\DiffOp\Diff\Diff;
8
use Diff\DiffOp\DiffOpAdd;
9
use Diff\DiffOp\DiffOpRemove;
10
use Diff\Tests\DiffOp\DiffOpTest;
11
12
/**
13
 * @covers Diff\DiffOp\Diff\ListDiff
14
 *
15
 * @group Diff
16
 * @group DiffOp
17
 *
18
 * @license GPL-2.0+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class ListDiffTest extends DiffOpTest {
22
23
	/**
24
	 * @see DiffOpTest::getClass
25
	 *
26
	 * @since 0.1
27
	 *
28
	 * @return string
29
	 */
30
	public function getClass() {
31
		return '\Diff\DiffOp\Diff\ListDiff';
32
	}
33
34
	/**
35
	 * @see DiffOpTest::constructorProvider
36
	 *
37
	 * @since 0.1
38
	 */
39
	public function constructorProvider() {
40
		$operationLists = array();
41
42
		$operationLists[] = array();
43
44
		$operationLists[] = array(
45
			new DiffOpAdd( 42 ),
46
		);
47
48
		$operationLists[] = array(
49
			new DiffOpAdd( 42 ),
50
			new DiffOpRemove( 1 ),
51
		);
52
53
		$operationLists[] = array(
54
			new DiffOpRemove( 'spam' ),
55
			new DiffOpAdd( 42 ),
56
			new DiffOpAdd( 42 ),
57
			new DiffOpAdd( 9001 ),
58
			new DiffOpRemove( 1 ),
59
		);
60
61
		$argLists = array();
62
63
		foreach ( $operationLists as $operationList ) {
64
			$argLists[] = array( true, $operationList );
65
			$argLists[] = array( true, $operationList, 'foobar' );
66
		}
67
68
		$argLists[] = array( false, array( 42 ) );
69
		$argLists[] = array( false, array( new ListDiffer() ) );
70
		$argLists[] = array( false, array( '~=[,,_,,]:3' ) );
71
72
		return $argLists;
73
	}
74
75
	public function newFromArraysProvider() {
76
		return array(
77
			array(
78
				array(),
79
				array(),
80
				array(),
81
				array(),
82
			),
83
			array(
84
				array( 'foo' ),
85
				array(),
86
				array(),
87
				array( 'foo' ),
88
			),
89
			array(
90
				array(),
91
				array( 'foo' ),
92
				array( 'foo' ),
93
				array(),
94
			),
95
			array(
96
				array( 'foo' ),
97
				array( 'foo' ),
98
				array(),
99
				array(),
100
			),
101
			array(
102
				array( 'foo', 'foo' ),
103
				array( 'foo' ),
104
				array(),
105
				array(),
106
			),
107
			array(
108
				array( 'foo' ),
109
				array( 'foo', 'foo' ),
110
				array(),
111
				array(),
112
			),
113
			array(
114
				array( 'foo', 'bar' ),
115
				array( 'bar', 'foo' ),
116
				array(),
117
				array(),
118
			),
119
			array(
120
				array( 'foo', 'bar', 42, 'baz' ),
121
				array( 42, 1, 2, 3 ),
122
				array( 1, 2, 3 ),
123
				array( 'foo', 'bar', 'baz' ),
124
			),
125
			array(
126
				array( false, null ),
127
				array( 0, '0' ),
128
				array( 0, '0' ),
129
				array( false, null ),
130
			),
131
		);
132
	}
133
134
	/**
135
	 * @dataProvider newFromArraysProvider
136
	 */
137
	public function testNewFromArrays( array $from, array $to, array $additions, array $removals ) {
138
		$differ = new ListDiffer( new NativeArrayComparer() );
139
140
		$diff = new Diff( $differ->doDiff( $from, $to ), false );
141
142
		$this->assertInstanceOf( '\Diff\DiffOp\DiffOp', $diff );
143
		$this->assertInstanceOf( '\Diff\DiffOp\Diff\Diff', $diff );
144
		$this->assertInstanceOf( '\ArrayObject', $diff );
145
146
		// array_values because we only care about the values, not promises are made about the keys.
147
		$resultAdditions = array_values( $diff->getAddedValues() );
148
		$resultRemovals = array_values( $diff->getRemovedValues() );
149
150
		// Sort everything since no promises are made about ordering.
151
		asort( $resultAdditions );
152
		asort( $resultRemovals );
153
		asort( $additions );
154
		asort( $removals );
155
156
		$this->assertEquals( $additions, $resultAdditions, 'additions mismatch' );
157
		$this->assertEquals( $removals, $resultRemovals, 'removals mismatch' );
158
159
		$this->assertEquals(
160
			$additions === array() && $removals === array(),
161
			$diff->isEmpty()
162
		);
163
	}
164
165
}
166