Passed
Push — int32EntityId ( fa80fb )
by no
05:13
created

testGivenGroupsWithTheSameLanguage_onlyTheLastOnesAreRetained()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 9.0856
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Term;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Term\AliasGroup;
7
use Wikibase\DataModel\Term\AliasGroupList;
8
9
/**
10
 * @covers Wikibase\DataModel\Term\AliasGroupList
11
 * @uses Wikibase\DataModel\Term\AliasGroup
12
 *
13
 * @license GPL-2.0+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class AliasGroupListTest extends PHPUnit_Framework_TestCase {
17
18
	public function testIsEmpty() {
19
		$list = new AliasGroupList();
20
		$this->assertTrue( $list->isEmpty() );
21
22
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo' ) ) ) );
23
		$this->assertFalse( $list->isEmpty() );
24
	}
25
26
	public function testGivenNoTerms_sizeIsZero() {
27
		$list = new AliasGroupList();
28
		$this->assertCount( 0, $list );
29
	}
30
31
	public function testGivenTwoTerms_countReturnsTwo() {
32
		$list = new AliasGroupList( $this->getTwoGroups() );
33
34
		$this->assertCount( 2, $list );
35
	}
36
37
	private function getTwoGroups() {
38
		return array(
39
			'en' => new AliasGroup( 'en', array( 'foo' ) ),
40
			'de' => new AliasGroup( 'de', array( 'bar', 'baz' ) ),
41
		);
42
	}
43
44
	public function testGivenTwoGroups_listContainsThem() {
45
		$array = $this->getTwoGroups();
46
47
		$list = new AliasGroupList( $array );
48
49
		$this->assertEquals( $array, iterator_to_array( $list ) );
50
	}
51
52
	public function testGivenGroupsWithTheSameLanguage_onlyTheLastOnesAreRetained() {
53
		$array = array(
54
			new AliasGroup( 'en', array( 'foo' ) ),
55
			new AliasGroup( 'en', array( 'bar' ) ),
56
57
			new AliasGroup( 'de', array( 'baz' ) ),
58
59
			new AliasGroup( 'nl', array( 'bah' ) ),
60
			new AliasGroup( 'nl', array( 'blah' ) ),
61
			new AliasGroup( 'nl', array( 'spam' ) ),
62
		);
63
64
		$list = new AliasGroupList( $array );
65
66
		$this->assertEquals(
67
			array(
68
				'en' => new AliasGroup( 'en', array( 'bar' ) ),
69
				'de' => new AliasGroup( 'de', array( 'baz' ) ),
70
				'nl' => new AliasGroup( 'nl', array( 'spam' ) ),
71
			),
72
			iterator_to_array( $list )
73
		);
74
	}
75
76
	public function testCanIterateOverList() {
77
		$group = new AliasGroup( 'en', array( 'foo' ) );
78
79
		$list = new AliasGroupList( array( $group ) );
80
81
		/**
82
		 * @var AliasGroup $aliasGroup
83
		 */
84
		foreach ( $list as $key => $aliasGroup ) {
85
			$this->assertEquals( $group, $aliasGroup );
86
			$this->assertEquals( $aliasGroup->getLanguageCode(), $key );
87
		}
88
	}
89
90
	public function testGivenNonAliasGroups_constructorThrowsException() {
91
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
92
		new AliasGroupList( array( null ) );
0 ignored issues
show
Documentation introduced by
array(null) is of type array<integer,null,{"0":"null"}>, but the function expects a array<integer,object<Wik...Model\Term\AliasGroup>>.

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...
93
	}
94
95
	public function testGivenSetLanguageCode_getByLanguageReturnsGroup() {
96
		$enGroup = new AliasGroup( 'en', array( 'foo' ) );
97
98
		$list = new AliasGroupList( array(
99
			new AliasGroup( 'de' ),
100
			$enGroup,
101
			new AliasGroup( 'nl' ),
102
		) );
103
104
		$this->assertEquals( $enGroup, $list->getByLanguage( 'en' ) );
105
	}
106
107
	/**
108
	 * @dataProvider invalidLanguageCodeProvider
109
	 */
110
	public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) {
111
		$list = new AliasGroupList();
112
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
113
		$list->getByLanguage( $languageCode );
114
	}
115
116
	public function testGivenNonSetLanguageCode_getByLanguageThrowsException() {
117
		$list = new AliasGroupList();
118
119
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
120
		$list->getByLanguage( 'en' );
121
	}
122
123
	public function testGivenGroupForNewLanguage_setGroupAddsGroup() {
124
		$enGroup = new AliasGroup( 'en', array( 'foo', 'bar' ) );
125
		$deGroup = new AliasGroup( 'de', array( 'baz', 'bah' ) );
126
127
		$list = new AliasGroupList( array( $enGroup ) );
128
		$expectedList = new AliasGroupList( array( $enGroup, $deGroup ) );
129
130
		$list->setGroup( $deGroup );
131
132
		$this->assertEquals( $expectedList, $list );
133
	}
134
135
	public function testGivenLabelForExistingLanguage_setLabelReplacesLabel() {
136
		$enGroup = new AliasGroup( 'en', array( 'foo', 'bar' ) );
137
		$newEnGroup = new AliasGroup( 'en', array( 'foo', 'bar', 'bah' ) );
138
139
		$list = new AliasGroupList( array( $enGroup ) );
140
		$expectedList = new AliasGroupList( array( $newEnGroup ) );
141
142
		$list->setGroup( $newEnGroup );
143
		$this->assertEquals( $expectedList, $list );
144
	}
145
146
	public function testGivenNotSetLanguage_removeByLanguageIsNoOp() {
147
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo', 'bar' ) ) ) );
148
		$originalList = clone $list;
149
150
		$list->removeByLanguage( 'de' );
151
152
		$this->assertEquals( $originalList, $list );
153
	}
154
155
	public function testGivenSetLanguage_removeByLanguageRemovesIt() {
156
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo', 'bar' ) ) ) );
157
158
		$list->removeByLanguage( 'en' );
159
160
		$this->assertEquals( new AliasGroupList(), $list );
161
	}
162
163
	/**
164
	 * @dataProvider invalidLanguageCodeProvider
165
	 */
166
	public function testGivenInvalidLanguageCode_removeByLanguageIsNoOp( $languageCode ) {
167
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo' ) ) ) );
168
		$list->removeByLanguage( $languageCode );
169
		$this->assertFalse( $list->isEmpty() );
170
	}
171
172
	public function testGivenEmptyGroups_constructorRemovesThem() {
173
		$enGroup = new AliasGroup( 'en', array( 'foo' ) );
174
175
		$list = new AliasGroupList( array(
176
			new AliasGroup( 'de' ),
177
			$enGroup,
178
			new AliasGroup( 'en' ),
179
			new AliasGroup( 'nl' ),
180
		) );
181
182
		$expectedList = new AliasGroupList( array(
183
			new AliasGroup( 'en' ),
184
		) );
185
186
		$this->assertEquals( $expectedList, $list );
187
	}
188
189
	public function testGivenEmptyGroup_setGroupRemovesGroup() {
190
		$list = new AliasGroupList( array(
191
			new AliasGroup( 'en', array( 'foo' ) ),
192
		) );
193
194
		$expectedList = new AliasGroupList();
195
196
		$list->setGroup( new AliasGroup( 'en' ) );
197
		$list->setGroup( new AliasGroup( 'de' ) );
198
199
		$this->assertEquals( $expectedList, $list );
200
	}
201
202
	public function testEmptyListEqualsEmptyList() {
203
		$list = new AliasGroupList();
204
		$this->assertTrue( $list->equals( new AliasGroupList() ) );
205
	}
206
207
	public function testFilledListEqualsItself() {
208
		$list = new AliasGroupList( array(
209
			new AliasGroup( 'en', array( 'foo' ) ),
210
			new AliasGroup( 'de', array( 'bar' ) ),
211
		) );
212
213
		$this->assertTrue( $list->equals( $list ) );
214
		$this->assertTrue( $list->equals( clone $list ) );
215
	}
216
217
	public function testDifferentListsDoNotEqual() {
218
		$list = new AliasGroupList( array(
219
			new AliasGroup( 'en', array( 'foo' ) ),
220
			new AliasGroup( 'de', array( 'bar' ) ),
221
		) );
222
223
		$this->assertFalse( $list->equals( new AliasGroupList() ) );
224
225
		$this->assertFalse( $list->equals(
226
			new AliasGroupList( array(
227
				new AliasGroup( 'en', array( 'foo' ) ),
228
				new AliasGroup( 'de', array( 'bar' ) ),
229
				new AliasGroup( 'nl', array( 'baz' ) ),
230
			) )
231
		) );
232
	}
233
234
	public function testGivenNonAliasGroupList_equalsReturnsFalse() {
235
		$list = new AliasGroupList();
236
		$this->assertFalse( $list->equals( null ) );
237
		$this->assertFalse( $list->equals( new \stdClass() ) );
238
	}
239
240
	public function testGivenListsThatOnlyDifferInOrder_equalsReturnsTrue() {
241
		$list = new AliasGroupList( array(
242
			new AliasGroup( 'en', array( 'foo' ) ),
243
			new AliasGroup( 'de', array( 'bar' ) ),
244
		) );
245
246
		$this->assertTrue( $list->equals(
247
			new AliasGroupList( array(
248
				new AliasGroup( 'de', array( 'bar' ) ),
249
				new AliasGroup( 'en', array( 'foo' ) ),
250
			) )
251
		) );
252
	}
253
254
	public function testGivenNonSetLanguageGroup_hasAliasGroupReturnsFalse() {
255
		$list = new AliasGroupList();
256
		$this->assertFalse( $list->hasAliasGroup( new AliasGroup( 'en', array( 'kittens' ) ) ) );
257
	}
258
259
	public function testGivenMismatchingGroup_hasAliasGroupReturnsFalse() {
260
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'cats' ) ) ) );
261
		$this->assertFalse( $list->hasAliasGroup( new AliasGroup( 'en', array( 'kittens' ) ) ) );
262
	}
263
264
	public function testGivenMatchingGroup_hasAliasGroupReturnsTrue() {
265
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'kittens' ) ) ) );
266
		$this->assertTrue( $list->hasAliasGroup( new AliasGroup( 'en', array( 'kittens' ) ) ) );
267
	}
268
269
	public function testGivenNonSetLanguageGroup_hasGroupForLanguageReturnsFalse() {
270
		$list = new AliasGroupList();
271
		$this->assertFalse( $list->hasGroupForLanguage( 'en' ) );
272
	}
273
274
	/**
275
	 * @dataProvider invalidLanguageCodeProvider
276
	 */
277
	public function testGivenInvalidLanguageCode_hasGroupForLanguageReturnsFalse( $languageCode ) {
278
		$list = new AliasGroupList();
279
		$this->assertFalse( $list->hasGroupForLanguage( $languageCode ) );
280
	}
281
282
	public function invalidLanguageCodeProvider() {
283
		return array(
284
			array( null ),
285
			array( 21 ),
286
			array( '' ),
287
		);
288
	}
289
290
	public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() {
291
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'cats' ) ) ) );
292
		$this->assertFalse( $list->hasGroupForLanguage( 'de' ) );
293
	}
294
295
	public function testGivenMatchingGroup_hasGroupForLanguageReturnsTrue() {
296
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'kittens' ) ) ) );
297
		$this->assertTrue( $list->hasGroupForLanguage( 'en' ) );
298
	}
299
300
	public function testGivenAliasGroupArgs_setGroupTextsSetsAliasGroup() {
301
		$list = new AliasGroupList();
302
303
		$list->setAliasesForLanguage( 'en', array( 'foo', 'bar' ) );
304
305
		$this->assertEquals(
306
			new AliasGroup( 'en', array( 'foo', 'bar' ) ),
307
			$list->getByLanguage( 'en' )
308
		);
309
	}
310
311
	public function testGivenInvalidLanguageCode_setGroupTextsThrowsException() {
312
		$list = new AliasGroupList();
313
314
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
315
		$list->setAliasesForLanguage( null, array( 'foo', 'bar' ) );
316
	}
317
318
	public function testGivenInvalidAliases_setGroupTextsThrowsException() {
319
		$list = new AliasGroupList();
320
321
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
322
		$list->setAliasesForLanguage( 'en', array( 'foo', null ) );
323
	}
324
325
	public function testToArray() {
326
		$array = array(
327
			'en' => new AliasGroup( 'en', array( 'foo' ) ),
328
			'de' => new AliasGroup( 'de', array( 'bar' ) ),
329
			'nl' => new AliasGroup( 'nl', array( 'baz' ) ),
330
		);
331
332
		$list = new AliasGroupList( $array );
333
334
		$this->assertEquals( $array, $list->toArray() );
335
	}
336
337
	public function testGivenEmptyList_getWithLanguagesReturnsEmptyList() {
338
		$list = new AliasGroupList();
339
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( array() ) );
340
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( array( 'en', 'de' ) ) );
341
	}
342
343
	public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() {
344
		$list = new AliasGroupList();
345
		$list->setAliasesForLanguage( 'en', array( 'foo' ) );
346
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
347
348
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( array() ) );
349
	}
350
351
	public function testGivenAllLanguages_getWithLanguagesReturnsFullList() {
352
		$list = new AliasGroupList();
353
		$list->setAliasesForLanguage( 'en', array( 'foo' ) );
354
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
355
356
		$this->assertEquals( $list, $list->getWithLanguages( array( 'en', 'de' ) ) );
357
	}
358
359
	public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() {
360
		$list = new AliasGroupList();
361
		$list->setAliasesForLanguage( 'en', array( 'foo' ) );
362
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
363
		$list->setAliasesForLanguage( 'nl', array( 'baz' ) );
364
		$list->setAliasesForLanguage( 'fr', array( 'hax' ) );
365
366
		$expectedList = new AliasGroupList();
367
		$expectedList->setAliasesForLanguage( 'en', array( 'foo' ) );
368
		$expectedList->setAliasesForLanguage( 'nl', array( 'baz' ) );
369
370
		$this->assertEquals( $expectedList, $list->getWithLanguages( array( 'en', 'nl' ) ) );
371
	}
372
373
	public function testToTextArray() {
374
		$list = new AliasGroupList();
375
		$list->setAliasesForLanguage( 'en', array( 'foo', 'baz' ) );
376
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
377
378
		$expected = array(
379
			'en' => array( 'foo', 'baz' ),
380
			'de' => array( 'bar' ),
381
		);
382
383
		$this->assertEquals( $expected, $list->toTextArray() );
384
	}
385
386
}
387