Passed
Push — rm-hasharray ( 353511 )
by Jeroen De
03:16
created

SnakListTest::orderByPropertyProvider()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 91
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 8.518
c 0
b 0
f 0
cc 3
eloc 46
nc 3
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
namespace Wikibase\DataModel\Tests\Snak;
4
5
use DataValues\StringValue;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
9
use Wikibase\DataModel\Snak\PropertyValueSnak;
10
use Wikibase\DataModel\Snak\Snak;
11
use Wikibase\DataModel\Snak\SnakList;
12
13
/**
14
 * @covers Wikibase\DataModel\Snak\SnakList
15
 *
16
 * @license GPL-2.0+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Addshore
19
 * @author Thiemo Mättig
20
 */
21
class SnakListTest extends \PHPUnit_Framework_TestCase {
22
23
	public function elementInstancesProvider() {
24
		$id42 = new PropertyId( 'P42' );
25
26
		$argLists = [];
27
28
		$argLists[] = [ [ new PropertyNoValueSnak( $id42 ) ] ];
29
		$argLists[] = [ [ new PropertyNoValueSnak( new PropertyId( 'P9001' ) ) ] ];
30
		$argLists[] = [ [ new PropertyValueSnak( $id42, new StringValue( 'a' ) ) ] ];
31
32
		return $argLists;
33
	}
34
35
	public function instanceProvider() {
36
		$instances = [];
37
38
		foreach ( $this->constructorProvider() as $args ) {
39
			$instances[] = [ new SnakList( array_key_exists( 0, $args ) ? $args[0] : null ) ];
40
		}
41
42
		return $instances;
43
	}
44
45
	public function constructorProvider() {
46
		$id42 = new PropertyId( 'P42' );
47
		$id9001 = new PropertyId( 'P9001' );
48
49
		return [
50
			[],
51
			[ [] ],
52
			[ [
53
				new PropertyNoValueSnak( $id42 )
54
			] ],
55
			[ [
56
				new PropertyNoValueSnak( $id42 ),
57
				new PropertyNoValueSnak( $id9001 ),
58
			] ],
59
			[ [
60
				new PropertyNoValueSnak( $id42 ),
61
				new PropertyNoValueSnak( $id9001 ),
62
				new PropertyValueSnak( $id42, new StringValue( 'a' ) ),
63
			] ],
64
		];
65
	}
66
67
	/**
68
	 * @dataProvider invalidConstructorArgumentsProvider
69
	 * @expectedException InvalidArgumentException
70
	 */
71
	public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) {
72
		new SnakList( $input );
73
	}
74
75
	public function invalidConstructorArgumentsProvider() {
76
		$id1 = new PropertyId( 'P1' );
77
78
		return [
79
			[ false ],
80
			[ 1 ],
81
			[ 0.1 ],
82
			[ 'string' ],
83
			[ $id1 ],
84
			[ new PropertyNoValueSnak( $id1 ) ],
85
			[ new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ],
86
			[ [ null ] ],
87
			[ [ $id1 ] ],
88
			[ [ new SnakList() ] ],
89
		];
90
	}
91
92
	public function testGivenAssociativeArray_constructorPreservesArrayKeys() {
93
		$snakList = new SnakList( [ 'key' => new PropertyNoValueSnak( 1 ) ] );
94
		$this->assertSame( [ 'key' ], array_keys( iterator_to_array( $snakList ) ) );
95
	}
96
97
	/**
98
	 * @dataProvider instanceProvider
99
	 * @param SnakList $array
100
	 */
101
	public function testHasSnak( SnakList $array ) {
102
		/**
103
		 * @var Snak $hashable
104
		 */
105
		foreach ( iterator_to_array( $array ) as $hashable ) {
106
			$this->assertTrue( $array->hasSnak( $hashable ) );
107
			$this->assertTrue( $array->hasSnakHash( $hashable->getHash() ) );
108
			$array->removeSnak( $hashable );
109
			$this->assertFalse( $array->hasSnak( $hashable ) );
110
			$this->assertFalse( $array->hasSnakHash( $hashable->getHash() ) );
111
		}
112
113
		$this->assertTrue( true );
114
	}
115
116
	/**
117
	 * @dataProvider instanceProvider
118
	 * @param SnakList $array
119
	 */
120
	public function testRemoveSnak( SnakList $array ) {
121
		$elementCount = $array->count();
122
123
		/**
124
		 * @var Snak $element
125
		 */
126
		foreach ( iterator_to_array( $array ) as $element ) {
127
			$this->assertTrue( $array->hasSnak( $element ) );
128
129
			if ( $elementCount % 2 === 0 ) {
130
				$array->removeSnak( $element );
131
			}
132
			else {
133
				$array->removeSnakHash( $element->getHash() );
134
			}
135
136
			$this->assertFalse( $array->hasSnak( $element ) );
137
			$this->assertEquals( --$elementCount, $array->count() );
138
		}
139
140
		$element = new PropertyNoValueSnak( new PropertyId( 'P42' ) );
141
142
		$array->removeSnak( $element );
143
		$array->removeSnakHash( $element->getHash() );
144
145
		$this->assertTrue( true );
146
	}
147
148
	/**
149
	 * @dataProvider instanceProvider
150
	 * @param SnakList $array
151
	 */
152
	public function testAddSnak( SnakList $array ) {
153
		$elementCount = $array->count();
154
155
		$elements = $this->elementInstancesProvider();
156
		$element = array_shift( $elements );
157
		$element = $element[0][0];
158
159
		if ( !$array->hasSnak( $element ) ) {
160
			++$elementCount;
161
		}
162
163
		$this->assertEquals( !$array->hasSnak( $element ), $array->addSnak( $element ) );
164
165
		$this->assertEquals( $elementCount, $array->count() );
166
167
		$this->assertFalse( $array->addSnak( $element ) );
168
169
		$this->assertEquals( $elementCount, $array->count() );
170
	}
171
172
	public function orderByPropertyProvider() {
173
		$id1 = new PropertyId( 'P1' );
174
		$id2 = new PropertyId( 'P2' );
175
		$id3 = new PropertyId( 'P3' );
176
177
		/**
178
		 * List of test data containing snaks to initialize SnakList objects. The first list of
179
		 * snaks represents the snak list to be used as test input while the second represents the
180
		 * expected result.
181
		 * @var array
182
		 */
183
		$rawArguments = [
184
			'Default order' => [
185
				[],
186
				[],
187
			],
188
			'Unknown id in order' => [
189
				[],
190
				[],
191
				[ 'P1' ]
192
			],
193
			[
194
				[ new PropertyNoValueSnak( $id1 ) ],
195
				[ new PropertyNoValueSnak( $id1 ) ],
196
			],
197
			[
198
				[
199
					new PropertyNoValueSnak( $id2 ),
200
					new PropertyNoValueSnak( $id1 ),
201
				],
202
				[
203
					new PropertyNoValueSnak( $id2 ),
204
					new PropertyNoValueSnak( $id1 ),
205
				],
206
			],
207
			[
208
				[
209
					new PropertyNoValueSnak( $id1 ),
210
					new PropertyNoValueSnak( $id2 ),
211
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
212
				],
213
				[
214
					new PropertyNoValueSnak( $id1 ),
215
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
216
					new PropertyNoValueSnak( $id2 ),
217
				],
218
			],
219
			'With additional order' => [
220
				[
221
					new PropertyNoValueSnak( $id3 ),
222
					new PropertyNoValueSnak( $id2 ),
223
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
224
				],
225
				[
226
					new PropertyNoValueSnak( $id2 ),
227
					new PropertyNoValueSnak( $id3 ),
228
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
229
				],
230
				[ 'P2' ]
231
			],
232
			[
233
				[
234
					new PropertyNoValueSnak( $id3 ),
235
					new PropertyNoValueSnak( $id2 ),
236
					new PropertyNoValueSnak( $id2 ),
237
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
238
					new PropertyNoValueSnak( $id1 ),
239
				],
240
				[
241
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
242
					new PropertyNoValueSnak( $id1 ),
243
					new PropertyNoValueSnak( $id3 ),
244
					new PropertyNoValueSnak( $id2 ),
245
					new PropertyNoValueSnak( $id2 ),
246
				],
247
				[ 'P1' ]
248
			],
249
		];
250
251
		$arguments = [];
252
253
		foreach ( $rawArguments as $key => $rawArgument ) {
254
			$arguments[$key] = [
255
				new SnakList( $rawArgument[0] ),
256
				new SnakList( $rawArgument[1] ),
257
				array_key_exists( 2, $rawArgument ) ? $rawArgument[2] : []
258
			];
259
		}
260
261
		return $arguments;
262
	}
263
264
	/**
265
	 * @dataProvider orderByPropertyProvider
266
	 */
267
	public function testOrderByProperty( SnakList $snakList, SnakList $expected, array $order = [] ) {
268
		$initialSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) );
269
270
		$snakList->orderByProperty( $order );
271
272
		// Instantiate new SnakList resetting the snaks' array keys. This allows comparing the
273
		// reordered SnakList to the expected SnakList.
274
		$orderedSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) );
275
276
		$this->assertEquals( $expected, $orderedSnakList );
277
278
		if ( $orderedSnakList->equals( $initialSnakList ) ) {
279
			$this->assertSame( $initialSnakList->getHash(), $snakList->getHash() );
280
		} else {
281
			$this->assertNotSame( $initialSnakList->getHash(), $snakList->getHash() );
282
		}
283
	}
284
285
	/**
286
	 * @dataProvider instanceProvider
287
	 * @param SnakList $array
288
	 */
289
	public function testEquals( SnakList $array ) {
290
		$this->assertTrue( $array->equals( $array ) );
291
		$this->assertFalse( $array->equals( 42 ) );
292
	}
293
294
}
295