Passed
Push — clear ( 11b8fa )
by Bene
03:57
created

AliasGroupListTest::testClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Term;
4
5
use InvalidArgumentException;
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
 * @licence GNU GPL v2+
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
	 * @expectedException InvalidArgumentException
110
	 */
111
	public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) {
112
		$list = new AliasGroupList();
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
	 * @expectedException InvalidArgumentException
166
	 */
167
	public function testGivenInvalidLanguageCode_removeByLanguageThrowsException( $languageCode ) {
168
		$list = new AliasGroupList();
169
		$list->removeByLanguage( $languageCode );
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
	 * @expectedException InvalidArgumentException
277
	 */
278
	public function testGivenInvalidLanguageCode_hasGroupForLanguageThrowsException( $languageCode ) {
279
		$list = new AliasGroupList();
280
		$list->hasGroupForLanguage( $languageCode );
281
	}
282
283
	public function invalidLanguageCodeProvider() {
284
		return array(
285
			array( null ),
286
			array( 21 ),
287
			array( '' ),
288
		);
289
	}
290
291
	public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() {
292
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'cats' ) ) ) );
293
		$this->assertFalse( $list->hasGroupForLanguage( 'de' ) );
294
	}
295
296
	public function testGivenMatchingGroup_hasGroupForLanguageReturnsTrue() {
297
		$list = new AliasGroupList( array( new AliasGroup( 'en', array( 'kittens' ) ) ) );
298
		$this->assertTrue( $list->hasGroupForLanguage( 'en' ) );
299
	}
300
301
	public function testGivenAliasGroupArgs_setGroupTextsSetsAliasGroup() {
302
		$list = new AliasGroupList();
303
304
		$list->setAliasesForLanguage( 'en', array( 'foo', 'bar' ) );
305
306
		$this->assertEquals(
307
			new AliasGroup( 'en', array( 'foo', 'bar' ) ),
308
			$list->getByLanguage( 'en' )
309
		);
310
	}
311
312
	public function testGivenInvalidLanguageCode_setGroupTextsThrowsException() {
313
		$list = new AliasGroupList();
314
315
		$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...
316
		$list->setAliasesForLanguage( null, array( 'foo', 'bar' ) );
317
	}
318
319
	public function testGivenInvalidAliases_setGroupTextsThrowsException() {
320
		$list = new AliasGroupList();
321
322
		$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...
323
		$list->setAliasesForLanguage( 'en', array( 'foo', null ) );
324
	}
325
326
	public function testToArray() {
327
		$array = array(
328
			'en' => new AliasGroup( 'en', array( 'foo' ) ),
329
			'de' => new AliasGroup( 'de', array( 'bar' ) ),
330
			'nl' => new AliasGroup( 'nl', array( 'baz' ) ),
331
		);
332
333
		$list = new AliasGroupList( $array );
334
335
		$this->assertEquals( $array, $list->toArray() );
336
	}
337
338
	public function testGivenEmptyList_getWithLanguagesReturnsEmptyList() {
339
		$list = new AliasGroupList();
340
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( array() ) );
341
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( array( 'en', 'de' ) ) );
342
	}
343
344
	public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() {
345
		$list = new AliasGroupList();
346
		$list->setAliasesForLanguage( 'en', array( 'foo' ) );
347
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
348
349
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( array() ) );
350
	}
351
352
	public function testGivenAllLanguages_getWithLanguagesReturnsFullList() {
353
		$list = new AliasGroupList();
354
		$list->setAliasesForLanguage( 'en', array( 'foo' ) );
355
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
356
357
		$this->assertEquals( $list, $list->getWithLanguages( array( 'en', 'de' ) ) );
358
	}
359
360
	public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() {
361
		$list = new AliasGroupList();
362
		$list->setAliasesForLanguage( 'en', array( 'foo' ) );
363
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
364
		$list->setAliasesForLanguage( 'nl', array( 'baz' ) );
365
		$list->setAliasesForLanguage( 'fr', array( 'hax' ) );
366
367
		$expectedList = new AliasGroupList();
368
		$expectedList->setAliasesForLanguage( 'en', array( 'foo' ) );
369
		$expectedList->setAliasesForLanguage( 'nl', array( 'baz' ) );
370
371
		$this->assertEquals( $expectedList, $list->getWithLanguages( array( 'en', 'nl' ) ) );
372
	}
373
374
	public function testToTextArray() {
375
		$list = new AliasGroupList();
376
		$list->setAliasesForLanguage( 'en', array( 'foo', 'baz' ) );
377
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
378
379
		$expected = array(
380
			'en' => array( 'foo', 'baz' ),
381
			'de' => array( 'bar' ),
382
		);
383
384
		$this->assertEquals( $expected, $list->toTextArray() );
385
	}
386
387
	public function testClear() {
388
		$list = new AliasGroupList();
389
		$list->setAliasesForLanguage( 'en', array( 'foo', 'baz' ) );
390
		$list->setAliasesForLanguage( 'de', array( 'bar' ) );
391
392
		$list->clear();
393
394
		$this->assertEquals( new AliasGroupList(), $list );
395
	}
396
397
}
398