testGivenInvalidLanguageCode_setGroupTextsThrowsException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Term;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use Wikibase\DataModel\Term\AliasGroup;
8
use Wikibase\DataModel\Term\AliasGroupList;
9
10
/**
11
 * @covers \Wikibase\DataModel\Term\AliasGroupList
12
 * @uses \Wikibase\DataModel\Term\AliasGroup
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class AliasGroupListTest extends \PHPUnit\Framework\TestCase {
18
19
	public function testIsEmpty() {
20
		$list = new AliasGroupList();
21
		$this->assertTrue( $list->isEmpty() );
22
23
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] );
24
		$this->assertFalse( $list->isEmpty() );
25
	}
26
27
	public function testGivenNoTerms_sizeIsZero() {
28
		$list = new AliasGroupList();
29
		$this->assertCount( 0, $list );
30
	}
31
32
	public function testGivenTwoTerms_countReturnsTwo() {
33
		$list = new AliasGroupList( $this->getTwoGroups() );
34
35
		$this->assertCount( 2, $list );
36
	}
37
38
	private function getTwoGroups() {
39
		return [
40
			'en' => new AliasGroup( 'en', [ 'foo' ] ),
41
			'de' => new AliasGroup( 'de', [ 'bar', 'baz' ] ),
42
		];
43
	}
44
45
	public function testGivenTwoGroups_listContainsThem() {
46
		$array = $this->getTwoGroups();
47
48
		$list = new AliasGroupList( $array );
49
50
		$this->assertSame( $array, iterator_to_array( $list ) );
51
	}
52
53
	public function testGivenGroupsWithTheSameLanguage_onlyTheLastOnesAreRetained() {
54
		$array = [
55
			new AliasGroup( 'en', [ 'foo' ] ),
56
			new AliasGroup( 'en', [ 'bar' ] ),
57
58
			new AliasGroup( 'de', [ 'baz' ] ),
59
60
			new AliasGroup( 'nl', [ 'bah' ] ),
61
			new AliasGroup( 'nl', [ 'blah' ] ),
62
			new AliasGroup( 'nl', [ 'spam' ] ),
63
		];
64
65
		$list = new AliasGroupList( $array );
66
67
		$this->assertEquals(
68
			[
69
				'en' => new AliasGroup( 'en', [ 'bar' ] ),
70
				'de' => new AliasGroup( 'de', [ 'baz' ] ),
71
				'nl' => new AliasGroup( 'nl', [ 'spam' ] ),
72
			],
73
			iterator_to_array( $list )
74
		);
75
	}
76
77
	public function testCanIterateOverList() {
78
		$group = new AliasGroup( 'en', [ 'foo' ] );
79
80
		$list = new AliasGroupList( [ $group ] );
81
82
		/**
83
		 * @var AliasGroup $aliasGroup
84
		 */
85
		foreach ( $list as $key => $aliasGroup ) {
86
			$this->assertEquals( $group, $aliasGroup );
87
			$this->assertSame( $aliasGroup->getLanguageCode(), $key );
88
		}
89
	}
90
91
	public function testGivenNonAliasGroups_constructorThrowsException() {
92
		$this->expectException( InvalidArgumentException::class );
93
		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...
94
	}
95
96
	public function testGivenSetLanguageCode_getByLanguageReturnsGroup() {
97
		$enGroup = new AliasGroup( 'en', [ 'foo' ] );
98
99
		$list = new AliasGroupList( [
100
			new AliasGroup( 'de' ),
101
			$enGroup,
102
			new AliasGroup( 'nl' ),
103
		] );
104
105
		$this->assertEquals( $enGroup, $list->getByLanguage( 'en' ) );
106
	}
107
108
	/**
109
	 * @dataProvider invalidLanguageCodeProvider
110
	 */
111
	public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) {
112
		$list = new AliasGroupList();
113
		$this->expectException( OutOfBoundsException::class );
114
		$list->getByLanguage( $languageCode );
115
	}
116
117
	public function testGivenNonSetLanguageCode_getByLanguageThrowsException() {
118
		$list = new AliasGroupList();
119
120
		$this->expectException( OutOfBoundsException::class );
121
		$list->getByLanguage( 'en' );
122
	}
123
124
	public function testGivenGroupForNewLanguage_setGroupAddsGroup() {
125
		$enGroup = new AliasGroup( 'en', [ 'foo', 'bar' ] );
126
		$deGroup = new AliasGroup( 'de', [ 'baz', 'bah' ] );
127
128
		$list = new AliasGroupList( [ $enGroup ] );
129
		$expectedList = new AliasGroupList( [ $enGroup, $deGroup ] );
130
131
		$list->setGroup( $deGroup );
132
133
		$this->assertEquals( $expectedList, $list );
134
	}
135
136
	public function testGivenLabelForExistingLanguage_setLabelReplacesLabel() {
137
		$enGroup = new AliasGroup( 'en', [ 'foo', 'bar' ] );
138
		$newEnGroup = new AliasGroup( 'en', [ 'foo', 'bar', 'bah' ] );
139
140
		$list = new AliasGroupList( [ $enGroup ] );
141
		$expectedList = new AliasGroupList( [ $newEnGroup ] );
142
143
		$list->setGroup( $newEnGroup );
144
		$this->assertEquals( $expectedList, $list );
145
	}
146
147
	public function testGivenNotSetLanguage_removeByLanguageIsNoOp() {
148
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo', 'bar' ] ) ] );
149
		$originalList = clone $list;
150
151
		$list->removeByLanguage( 'de' );
152
153
		$this->assertEquals( $originalList, $list );
154
	}
155
156
	public function testGivenSetLanguage_removeByLanguageRemovesIt() {
157
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo', 'bar' ] ) ] );
158
159
		$list->removeByLanguage( 'en' );
160
161
		$this->assertTrue( $list->isEmpty() );
162
	}
163
164
	/**
165
	 * @dataProvider invalidLanguageCodeProvider
166
	 */
167
	public function testGivenInvalidLanguageCode_removeByLanguageIsNoOp( $languageCode ) {
168
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] );
169
		$list->removeByLanguage( $languageCode );
170
		$this->assertFalse( $list->isEmpty() );
171
	}
172
173
	public function testGivenEmptyGroups_constructorRemovesThem() {
174
		$list = new AliasGroupList( [
175
			new AliasGroup( 'de' ),
176
			new AliasGroup( 'en', [ 'foo' ] ),
177
			new AliasGroup( 'en' ),
178
		] );
179
180
		$this->assertTrue( $list->isEmpty() );
181
	}
182
183
	public function testGivenEmptyGroup_setGroupRemovesGroup() {
184
		$list = new AliasGroupList( [
185
			new AliasGroup( 'en', [ 'foo' ] ),
186
		] );
187
188
		$list->setGroup( new AliasGroup( 'en' ) );
189
		$list->setGroup( new AliasGroup( 'de' ) );
190
191
		$this->assertEquals( new AliasGroupList(), $list );
192
	}
193
194
	public function testEmptyListEqualsEmptyList() {
195
		$list = new AliasGroupList();
196
		$this->assertTrue( $list->equals( new AliasGroupList() ) );
197
	}
198
199
	public function testFilledListEqualsItself() {
200
		$list = new AliasGroupList( [
201
			new AliasGroup( 'en', [ 'foo' ] ),
202
			new AliasGroup( 'de', [ 'bar' ] ),
203
		] );
204
205
		$this->assertTrue( $list->equals( $list ) );
206
		$this->assertTrue( $list->equals( clone $list ) );
207
	}
208
209
	public function testDifferentListsDoNotEqual() {
210
		$list = new AliasGroupList( [
211
			new AliasGroup( 'en', [ 'foo' ] ),
212
			new AliasGroup( 'de', [ 'bar' ] ),
213
		] );
214
215
		$this->assertFalse( $list->equals( new AliasGroupList() ) );
216
217
		$this->assertFalse( $list->equals(
218
			new AliasGroupList( [
219
				new AliasGroup( 'en', [ 'foo' ] ),
220
				new AliasGroup( 'de', [ 'bar' ] ),
221
				new AliasGroup( 'nl', [ 'baz' ] ),
222
			] )
223
		) );
224
	}
225
226
	public function testGivenNonAliasGroupList_equalsReturnsFalse() {
227
		$list = new AliasGroupList();
228
		$this->assertFalse( $list->equals( null ) );
229
		$this->assertFalse( $list->equals( new \stdClass() ) );
230
	}
231
232
	public function testGivenListsThatOnlyDifferInOrder_equalsReturnsTrue() {
233
		$list = new AliasGroupList( [
234
			new AliasGroup( 'en', [ 'foo' ] ),
235
			new AliasGroup( 'de', [ 'bar' ] ),
236
		] );
237
238
		$this->assertTrue( $list->equals(
239
			new AliasGroupList( [
240
				new AliasGroup( 'de', [ 'bar' ] ),
241
				new AliasGroup( 'en', [ 'foo' ] ),
242
			] )
243
		) );
244
	}
245
246
	public function testGivenNonSetLanguageGroup_hasAliasGroupReturnsFalse() {
247
		$list = new AliasGroupList();
248
		$this->assertFalse( $list->hasAliasGroup( new AliasGroup( 'en', [ 'kittens' ] ) ) );
249
	}
250
251
	public function testGivenMismatchingGroup_hasAliasGroupReturnsFalse() {
252
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'cats' ] ) ] );
253
		$this->assertFalse( $list->hasAliasGroup( new AliasGroup( 'en', [ 'kittens' ] ) ) );
254
	}
255
256
	public function testGivenMatchingGroup_hasAliasGroupReturnsTrue() {
257
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'kittens' ] ) ] );
258
		$this->assertTrue( $list->hasAliasGroup( new AliasGroup( 'en', [ 'kittens' ] ) ) );
259
	}
260
261
	public function testGivenNonSetLanguageGroup_hasGroupForLanguageReturnsFalse() {
262
		$list = new AliasGroupList();
263
		$this->assertFalse( $list->hasGroupForLanguage( 'en' ) );
264
	}
265
266
	/**
267
	 * @dataProvider invalidLanguageCodeProvider
268
	 */
269
	public function testGivenInvalidLanguageCode_hasGroupForLanguageReturnsFalse( $languageCode ) {
270
		$list = new AliasGroupList();
271
		$this->assertFalse( $list->hasGroupForLanguage( $languageCode ) );
272
	}
273
274
	public function invalidLanguageCodeProvider() {
275
		return [
276
			[ null ],
277
			[ 21 ],
278
			[ '' ],
279
		];
280
	}
281
282
	public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() {
283
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'cats' ] ) ] );
284
		$this->assertFalse( $list->hasGroupForLanguage( 'de' ) );
285
	}
286
287
	public function testGivenMatchingGroup_hasGroupForLanguageReturnsTrue() {
288
		$list = new AliasGroupList( [ new AliasGroup( 'en', [ 'kittens' ] ) ] );
289
		$this->assertTrue( $list->hasGroupForLanguage( 'en' ) );
290
	}
291
292
	public function testGivenAliasGroupArgs_setGroupTextsSetsAliasGroup() {
293
		$list = new AliasGroupList();
294
295
		$list->setAliasesForLanguage( 'en', [ 'foo', 'bar' ] );
296
297
		$this->assertEquals(
298
			new AliasGroup( 'en', [ 'foo', 'bar' ] ),
299
			$list->getByLanguage( 'en' )
300
		);
301
	}
302
303
	public function testGivenInvalidLanguageCode_setGroupTextsThrowsException() {
304
		$list = new AliasGroupList();
305
306
		$this->expectException( InvalidArgumentException::class );
307
		$list->setAliasesForLanguage( null, [ 'foo', 'bar' ] );
308
	}
309
310
	public function testGivenInvalidAliases_setGroupTextsThrowsException() {
311
		$list = new AliasGroupList();
312
313
		$this->expectException( InvalidArgumentException::class );
314
		$list->setAliasesForLanguage( 'en', [ 'foo', null ] );
315
	}
316
317
	public function testToArray() {
318
		$array = [
319
			'en' => new AliasGroup( 'en', [ 'foo' ] ),
320
			'de' => new AliasGroup( 'de', [ 'bar' ] ),
321
			'nl' => new AliasGroup( 'nl', [ 'baz' ] ),
322
		];
323
324
		$list = new AliasGroupList( $array );
325
326
		$this->assertSame( $array, $list->toArray() );
327
	}
328
329
	public function testGivenEmptyList_getWithLanguagesReturnsEmptyList() {
330
		$list = new AliasGroupList();
331
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( [] ) );
332
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( [ 'en', 'de' ] ) );
333
	}
334
335
	public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() {
336
		$list = new AliasGroupList();
337
		$list->setAliasesForLanguage( 'en', [ 'foo' ] );
338
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
339
340
		$this->assertEquals( new AliasGroupList(), $list->getWithLanguages( [] ) );
341
	}
342
343
	public function testGivenAllLanguages_getWithLanguagesReturnsFullList() {
344
		$list = new AliasGroupList();
345
		$list->setAliasesForLanguage( 'en', [ 'foo' ] );
346
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
347
348
		$this->assertEquals( $list, $list->getWithLanguages( [ 'en', 'de' ] ) );
349
	}
350
351
	public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() {
352
		$list = new AliasGroupList();
353
		$list->setAliasesForLanguage( 'en', [ 'foo' ] );
354
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
355
		$list->setAliasesForLanguage( 'nl', [ 'baz' ] );
356
		$list->setAliasesForLanguage( 'fr', [ 'hax' ] );
357
358
		$expectedList = new AliasGroupList();
359
		$expectedList->setAliasesForLanguage( 'en', [ 'foo' ] );
360
		$expectedList->setAliasesForLanguage( 'nl', [ 'baz' ] );
361
362
		$this->assertEquals( $expectedList, $list->getWithLanguages( [ 'en', 'nl' ] ) );
363
	}
364
365
	public function testToTextArray() {
366
		$list = new AliasGroupList();
367
		$list->setAliasesForLanguage( 'en', [ 'foo', 'baz' ] );
368
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
369
370
		$expected = [
371
			'en' => [ 'foo', 'baz' ],
372
			'de' => [ 'bar' ],
373
		];
374
375
		$this->assertEquals( $expected, $list->toTextArray() );
376
	}
377
378
	public function testClear() {
379
		$list = new AliasGroupList();
380
		$list->setAliasesForLanguage( 'en', [ 'foo', 'baz' ] );
381
		$list->setAliasesForLanguage( 'de', [ 'bar' ] );
382
383
		$list->clear();
384
385
		$this->assertEquals( new AliasGroupList(), $list );
386
	}
387
388
}
389