Passed
Push — master ( c7b7b3...d0017a )
by Daniel
07:28
created

testGivenInvalidLanguageCode_removeByLanguageIsNoOp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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( [ new AliasGroup( 'en', [ '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 [
39
			'en' => new AliasGroup( 'en', [ 'foo' ] ),
40
			'de' => new AliasGroup( 'de', [ '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 = [
54
			new AliasGroup( 'en', [ 'foo' ] ),
55
			new AliasGroup( 'en', [ 'bar' ] ),
56
57
			new AliasGroup( 'de', [ 'baz' ] ),
58
59
			new AliasGroup( 'nl', [ 'bah' ] ),
60
			new AliasGroup( 'nl', [ 'blah' ] ),
61
			new AliasGroup( 'nl', [ 'spam' ] ),
62
		];
63
64
		$list = new AliasGroupList( $array );
65
66
		$this->assertEquals(
67
			[
68
				'en' => new AliasGroup( 'en', [ 'bar' ] ),
69
				'de' => new AliasGroup( 'de', [ 'baz' ] ),
70
				'nl' => new AliasGroup( 'nl', [ 'spam' ] ),
71
			],
72
			iterator_to_array( $list )
73
		);
74
	}
75
76
	public function testCanIterateOverList() {
77
		$group = new AliasGroup( 'en', [ 'foo' ] );
78
79
		$list = new AliasGroupList( [ $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' );
92
		new AliasGroupList( [ 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', [ 'foo' ] );
97
98
		$list = new AliasGroupList( [
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' );
113
		$list->getByLanguage( $languageCode );
114
	}
115
116
	public function testGivenNonSetLanguageCode_getByLanguageThrowsException() {
117
		$list = new AliasGroupList();
118
119
		$this->setExpectedException( 'OutOfBoundsException' );
120
		$list->getByLanguage( 'en' );
121
	}
122
123
	public function testGivenGroupForNewLanguage_setGroupAddsGroup() {
124
		$enGroup = new AliasGroup( 'en', [ 'foo', 'bar' ] );
125
		$deGroup = new AliasGroup( 'de', [ 'baz', 'bah' ] );
126
127
		$list = new AliasGroupList( [ $enGroup ] );
128
		$expectedList = new AliasGroupList( [ $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', [ 'foo', 'bar' ] );
137
		$newEnGroup = new AliasGroup( 'en', [ 'foo', 'bar', 'bah' ] );
138
139
		$list = new AliasGroupList( [ $enGroup ] );
140
		$expectedList = new AliasGroupList( [ $newEnGroup ] );
141
142
		$list->setGroup( $newEnGroup );
143
		$this->assertEquals( $expectedList, $list );
144
	}
145
146
	public function testGivenNotSetLanguage_removeByLanguageIsNoOp() {
147
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ '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( [ new AliasGroup( 'en', [ '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( [ new AliasGroup( 'en', [ 'foo' ] ) ] );
168
		$list->removeByLanguage( $languageCode );
169
		$this->assertFalse( $list->isEmpty() );
170
	}
171
172
	public function testGivenEmptyGroups_constructorRemovesThem() {
173
		$enGroup = new AliasGroup( 'en', [ 'foo' ] );
174
175
		$list = new AliasGroupList( [
176
			new AliasGroup( 'de' ),
177
			$enGroup,
178
			new AliasGroup( 'en' ),
179
			new AliasGroup( 'nl' ),
180
		] );
181
182
		$expectedList = new AliasGroupList( [
183
			new AliasGroup( 'en' ),
184
		] );
185
186
		$this->assertEquals( $expectedList, $list );
187
	}
188
189
	public function testGivenEmptyGroup_setGroupRemovesGroup() {
190
		$list = new AliasGroupList( [
191
			new AliasGroup( 'en', [ '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( [
209
			new AliasGroup( 'en', [ 'foo' ] ),
210
			new AliasGroup( 'de', [ '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( [
219
			new AliasGroup( 'en', [ 'foo' ] ),
220
			new AliasGroup( 'de', [ 'bar' ] ),
221
		] );
222
223
		$this->assertFalse( $list->equals( new AliasGroupList() ) );
224
225
		$this->assertFalse( $list->equals(
226
			new AliasGroupList( [
227
				new AliasGroup( 'en', [ 'foo' ] ),
228
				new AliasGroup( 'de', [ 'bar' ] ),
229
				new AliasGroup( 'nl', [ '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( [
242
			new AliasGroup( 'en', [ 'foo' ] ),
243
			new AliasGroup( 'de', [ 'bar' ] ),
244
		] );
245
246
		$this->assertTrue( $list->equals(
247
			new AliasGroupList( [
248
				new AliasGroup( 'de', [ 'bar' ] ),
249
				new AliasGroup( 'en', [ 'foo' ] ),
250
			] )
251
		) );
252
	}
253
254
	public function testGivenNonSetLanguageGroup_hasAliasGroupReturnsFalse() {
255
		$list = new AliasGroupList();
256
		$this->assertFalse( $list->hasAliasGroup( new AliasGroup( 'en', [ 'kittens' ] ) ) );
257
	}
258
259
	public function testGivenMismatchingGroup_hasAliasGroupReturnsFalse() {
260
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'cats' ] ) ] );
261
		$this->assertFalse( $list->hasAliasGroup( new AliasGroup( 'en', [ 'kittens' ] ) ) );
262
	}
263
264
	public function testGivenMatchingGroup_hasAliasGroupReturnsTrue() {
265
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'kittens' ] ) ] );
266
		$this->assertTrue( $list->hasAliasGroup( new AliasGroup( 'en', [ '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 [
284
			[ null ],
285
			[ 21 ],
286
			[ '' ],
287
		];
288
	}
289
290
	public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() {
291
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'cats' ] ) ] );
292
		$this->assertFalse( $list->hasGroupForLanguage( 'de' ) );
293
	}
294
295
	public function testGivenMatchingGroup_hasGroupForLanguageReturnsTrue() {
296
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'kittens' ] ) ] );
297
		$this->assertTrue( $list->hasGroupForLanguage( 'en' ) );
298
	}
299
300
	public function testGivenAliasGroupArgs_setGroupTextsSetsAliasGroup() {
301
		$list = new AliasGroupList();
302
303
		$list->setAliasesForLanguage( 'en', [ 'foo', 'bar' ] );
304
305
		$this->assertEquals(
306
			new AliasGroup( 'en', [ 'foo', 'bar' ] ),
307
			$list->getByLanguage( 'en' )
308
		);
309
	}
310
311
	public function testGivenInvalidLanguageCode_setGroupTextsThrowsException() {
312
		$list = new AliasGroupList();
313
314
		$this->setExpectedException( 'InvalidArgumentException' );
315
		$list->setAliasesForLanguage( null, [ 'foo', 'bar' ] );
316
	}
317
318
	public function testGivenInvalidAliases_setGroupTextsThrowsException() {
319
		$list = new AliasGroupList();
320
321
		$this->setExpectedException( 'InvalidArgumentException' );
322
		$list->setAliasesForLanguage( 'en', [ 'foo', null ] );
323
	}
324
325
	public function testToArray() {
326
		$array = [
327
			'en' => new AliasGroup( 'en', [ 'foo' ] ),
328
			'de' => new AliasGroup( 'de', [ 'bar' ] ),
329
			'nl' => new AliasGroup( 'nl', [ '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( [] ) );
340
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( [ 'en', 'de' ] ) );
341
	}
342
343
	public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() {
344
		$list = new AliasGroupList();
345
		$list->setAliasesForLanguage( 'en', [ 'foo' ] );
346
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
347
348
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( [] ) );
349
	}
350
351
	public function testGivenAllLanguages_getWithLanguagesReturnsFullList() {
352
		$list = new AliasGroupList();
353
		$list->setAliasesForLanguage( 'en', [ 'foo' ] );
354
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
355
356
		$this->assertEquals( $list, $list->getWithLanguages( [ 'en', 'de' ] ) );
357
	}
358
359
	public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() {
360
		$list = new AliasGroupList();
361
		$list->setAliasesForLanguage( 'en', [ 'foo' ] );
362
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
363
		$list->setAliasesForLanguage( 'nl', [ 'baz' ] );
364
		$list->setAliasesForLanguage( 'fr', [ 'hax' ] );
365
366
		$expectedList = new AliasGroupList();
367
		$expectedList->setAliasesForLanguage( 'en', [ 'foo' ] );
368
		$expectedList->setAliasesForLanguage( 'nl', [ 'baz' ] );
369
370
		$this->assertEquals( $expectedList, $list->getWithLanguages( [ 'en', 'nl' ] ) );
371
	}
372
373
	public function testToTextArray() {
374
		$list = new AliasGroupList();
375
		$list->setAliasesForLanguage( 'en', [ 'foo', 'baz' ] );
376
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
377
378
		$expected = [
379
			'en' => [ 'foo', 'baz' ],
380
			'de' => [ 'bar' ],
381
		];
382
383
		$this->assertEquals( $expected, $list->toTextArray() );
384
	}
385
386
	public function testClear() {
387
		$list = new AliasGroupList();
388
		$list->setAliasesForLanguage( 'en', [ 'foo', 'baz' ] );
389
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
390
391
		$list->clear();
392
393
		$this->assertEquals( new AliasGroupList(), $list );
394
	}
395
396
}
397