Completed
Push — master ( 62dba0...c7ac65 )
by Jeroen De
9s
created

ListPatcherTest::patchProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 8.7468
c 0
b 0
f 0
cc 1
eloc 58
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Diff\Tests\Patcher;
6
7
use Diff\DiffOp\Diff\Diff;
8
use Diff\DiffOp\DiffOpAdd;
9
use Diff\DiffOp\DiffOpRemove;
10
use Diff\Patcher\ListPatcher;
11
use Diff\Patcher\Patcher;
12
use Diff\Tests\DiffTestCase;
13
use stdClass;
14
15
/**
16
 * @covers Diff\Patcher\ListPatcher
17
 * @covers Diff\Patcher\ThrowingPatcher
18
 *
19
 * @group Diff
20
 * @group DiffPatcher
21
 *
22
 * @license GPL-2.0+
23
 * @author Jeroen De Dauw < [email protected] >
24
 */
25
class ListPatcherTest extends DiffTestCase {
26
27
	public function patchProvider() {
28
		$argLists = array();
29
30
		$patcher = new ListPatcher();
31
		$base = array();
32
		$diff = new Diff();
33
		$expected = array();
34
35
		$argLists[] = array( $patcher, $base, $diff, $expected );
36
37
		$patcher = new ListPatcher();
38
		$base = array( 4, 2 );
39
		$diff = new Diff();
40
		$expected = array( 4, 2 );
41
42
		$argLists[] = array( $patcher, $base, $diff, $expected );
43
44
		$patcher = new ListPatcher();
45
		$base = array();
46
		$diff = new Diff( array(
47
			new DiffOpAdd( 9001 )
48
		) );
49
		$expected = array( 9001 );
50
51
		$argLists[] = array( $patcher, $base, $diff, $expected );
52
53
		$patcher = new ListPatcher();
54
		$base = array( 4, 2 );
55
		$diff = new Diff( array(
56
			new DiffOpAdd( 9001 )
57
		) );
58
		$expected = array( 4, 2, 9001 );
59
60
		$argLists[] = array( $patcher, $base, $diff, $expected );
61
62
		$patcher = new ListPatcher();
63
		$base = array( 4, 2 );
64
		$diff = new Diff( array(
65
			new DiffOpAdd( 9001 ),
66
			new DiffOpAdd( 9002 ),
67
			new DiffOpAdd( 2 )
68
		) );
69
		$expected = array( 4, 2, 9001, 9002, 2 );
70
71
		$argLists[] = array( $patcher, $base, $diff, $expected );
72
73
		$patcher = new ListPatcher();
74
		$base = array( 0, 1, 2, 3, 4 );
75
		$diff = new Diff( array(
76
			new DiffOpRemove( 2 ),
77
			new DiffOpRemove( 3 ),
78
		) );
79
		$expected = array( 0, 1, 4 );
80
81
		$argLists[] = array( $patcher, $base, $diff, $expected );
82
83
		$patcher = new ListPatcher();
84
		$base = array( 0, 1, 2, 2, 2, 3, 4 );
85
		$diff = new Diff( array(
86
			new DiffOpRemove( 2 ),
87
			new DiffOpRemove( 3 ),
88
			new DiffOpAdd( 6 ),
89
			new DiffOpRemove( 2 ),
90
		) );
91
		$expected = array( 0, 1, 2, 4, 6 );
92
93
		$argLists[] = array( $patcher, $base, $diff, $expected );
94
95
		$patcher = new ListPatcher();
96
		$base = array(
97
			$this->newObject( 'foo' ),
98
			$this->newObject( 'bar' ),
99
		);
100
		$diff = new Diff( array(
101
			new DiffOpRemove( $this->newObject( 'foo') ),
102
			new DiffOpAdd( $this->newObject( 'baz' ) ),
103
		) );
104
		$expected = array( $this->newObject( 'bar' ), $this->newObject( 'baz' ) );
105
106
		$argLists[] = array( $patcher, $base, $diff, $expected );
107
108
		return $argLists;
109
	}
110
111
	private function newObject( $value ) : stdClass {
112
		$object = new stdClass();
113
		$object->element = $value;
114
		return $object;
115
	}
116
117
	/**
118
	 * @dataProvider patchProvider
119
	 *
120
	 * @param Patcher $patcher
121
	 * @param array $base
122
	 * @param Diff $diff
123
	 * @param array $expected
124
	 */
125
	public function testPatch( Patcher $patcher, array $base, Diff $diff, array $expected ) {
126
		$actual = $patcher->patch( $base, $diff );
127
128
		$this->assertArrayEquals( $expected, $actual );
129
	}
130
131
	/**
132
	 * @dataProvider getApplicableDiffProvider
133
	 *
134
	 * @param Diff $diff
135
	 * @param array $currentObject
136
	 * @param Diff $expected
137
	 * @param string|null $message
138
	 */
139
	public function testGetApplicableDiff( Diff $diff, array $currentObject, Diff $expected, $message = null ) {
140
		$patcher = new ListPatcher();
141
		$actual = $patcher->getApplicableDiff( $currentObject, $diff );
142
143
		$this->assertEquals( $expected->getOperations(), $actual->getOperations(), $message );
144
	}
145
146
	public function getApplicableDiffProvider() {
147
		// Diff, current object, expected
148
		$argLists = array();
149
150
		$diff = new Diff( array(), false );
151
		$currentObject = array();
152
		$expected = clone $diff;
153
154
		$argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on empty base' );
155
156
		$diff = new Diff( array(), false );
157
158
		$currentObject = array( 'foo' => 0, 'bar' => 1 );
159
160
		$expected = clone $diff;
161
162
		$argLists[] = array( $diff, $currentObject, $expected, 'Empty diff should remain empty on non-empty base' );
163
164
		$diff = new Diff( array(
165
			new DiffOpRemove( 9001 ),
166
		), false );
167
168
		$currentObject = array();
169
170
		$expected = new Diff( array(), false );
171
172
		$argLists[] = array( $diff, $currentObject, $expected, 'Remove ops should be removed on empty base' );
173
174
		$diff = new Diff( array(
175
			new DiffOpAdd( 42 ),
176
			new DiffOpRemove( 9001 ),
177
		), false );
178
179
		$currentObject = array();
180
181
		$expected = new Diff( array(
182
			new DiffOpAdd( 42 ),
183
		), true );
184
185
		$argLists[] = array(
186
			$diff,
187
			$currentObject,
188
			$expected,
189
			'Add ops not present in the base should be retained (ListDiff)'
190
		);
191
192
		$diff = new Diff( array(
193
			new DiffOpAdd( 42 ),
194
			new DiffOpRemove( 9001 ),
195
		), false );
196
197
		$currentObject = array( 1, 42, 9001 );
198
199
		$expected = new Diff( array(
200
			new DiffOpAdd( 42 ),
201
			new DiffOpRemove( 9001 ),
202
		), false );
203
204
		$argLists[] = array(
205
			$diff,
206
			$currentObject,
207
			$expected,
208
			'Add ops with values present in the base should be retained in ListDiff'
209
		);
210
211
		$diff = new Diff( array(
212
			new DiffOpAdd( 42 ),
213
		), false );
214
215
		$currentObject = array();
216
217
		$expected = clone $diff;
218
219
		$argLists[] = array(
220
			$diff,
221
			$currentObject,
222
			$expected,
223
			'list diffs containing only add ops should be retained even when not in the base'
224
		);
225
226
		$diff = new Diff( array(
227
			new DiffOpRemove( 42 ),
228
			new DiffOpRemove( 9001 ),
229
		), false );
230
231
		$currentObject = array(
232
			42,
233
			72010,
234
			9001,
235
		);
236
237
		$expected = clone $diff;
238
239
		$argLists[] = array(
240
			$diff,
241
			$currentObject,
242
			$expected,
243
			'list diffs containing only remove ops should be retained when present in the base'
244
		);
245
246
		return $argLists;
247
	}
248
249
	public function testPatchMapRaisesError() {
250
		$patcher = new ListPatcher();
251
252
		$patcher->patch( array(), new Diff( array(), true ) );
253
254
		$patcher->throwErrors();
255
		$this->expectException( 'Diff\Patcher\PatcherException' );
256
257
		$patcher->patch( array(), new Diff( array(), true ) );
258
	}
259
260
}
261