Completed
Push — master ( a64b83...f68fa9 )
by Leszek
07:44
created

testGivenUnknownId_removeSiteWithIdDoesNoOp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\ItemIdSet;
9
use Wikibase\DataModel\SiteLink;
10
use Wikibase\DataModel\SiteLinkList;
11
12
/**
13
 * @covers \Wikibase\DataModel\SiteLinkList
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class SiteLinkListTest extends \PHPUnit_Framework_TestCase {
19
20
	/**
21
	 * @dataProvider notSiteLinksProvider
22
	 */
23
	public function testGivenNonSiteLinks_constructorThrowsException( array $notSiteLinks ) {
24
		$this->expectException( InvalidArgumentException::class );
25
		new SiteLinkList( $notSiteLinks );
26
	}
27
28
	public function notSiteLinksProvider() {
29
		return [
30
			[
31
				[
32
					null
33
				]
34
			],
35
36
			[
37
				[
38
					42
39
				]
40
			],
41
42
			[
43
				[
44
					new SiteLink( 'foo', 'bar' ),
45
					42,
46
					new SiteLink( 'baz', 'bah' ),
47
				]
48
			],
49
		];
50
	}
51
52
	/**
53
	 * @dataProvider siteLinkArrayProvider
54
	 */
55
	public function testInputRoundtripsUsingIteratorToArray( array $siteLinkArray ) {
56
		$list = new SiteLinkList( $siteLinkArray );
57
		$this->assertEquals( $siteLinkArray, array_values( iterator_to_array( $list ) ) );
58
	}
59
60
	public function siteLinkArrayProvider() {
61
		return [
62
			[
63
				[
64
				]
65
			],
66
67
			[
68
				[
69
					new SiteLink( 'foo', 'bar' )
70
				]
71
			],
72
73
			[
74
				[
75
					new SiteLink( 'foo', 'bar' ),
76
					new SiteLink( 'baz', 'bah' ),
77
					new SiteLink( 'hax', 'bar' ),
78
				]
79
			],
80
		];
81
	}
82
83
	public function testEmptyCollectionHasZeroSize() {
84
		$list = new SiteLinkList( [] );
85
		$this->assertCount( 0, $list );
86
	}
87
88
	/**
89
	 * @dataProvider siteLinkArrayWithDuplicateSiteIdProvider
90
	 */
91
	public function testGivenSiteIdTwice_constructorThrowsException( array $siteLinkArray ) {
92
		$this->expectException( InvalidArgumentException::class );
93
		new SiteLinkList( $siteLinkArray );
94
	}
95
96
	public function siteLinkArrayWithDuplicateSiteIdProvider() {
97
		return [
98
			[
99
				[
100
					new SiteLink( 'foo', 'bar' ),
101
					new SiteLink( 'foo', 'bar' ),
102
				]
103
			],
104
105
			[
106
				[
107
					new SiteLink( 'foo', 'one' ),
108
					new SiteLink( 'baz', 'two' ),
109
					new SiteLink( 'foo', 'tree' ),
110
				]
111
			],
112
		];
113
	}
114
115
	public function testGetIteratorReturnsTraversableWithSiteIdKeys() {
116
		$list = new SiteLinkList( [
117
			new SiteLink( 'first', 'one' ),
118
			new SiteLink( 'second', 'two' ),
119
			new SiteLink( 'third', 'tree' ),
120
		] );
121
122
		$this->assertEquals(
123
			[
124
				'first' => new SiteLink( 'first', 'one' ),
125
				'second' => new SiteLink( 'second', 'two' ),
126
				'third' => new SiteLink( 'third', 'tree' ),
127
			],
128
			iterator_to_array( $list )
129
		);
130
	}
131
132
	public function testGivenNonString_getBySiteIdThrowsException() {
133
		$list = new SiteLinkList( [] );
134
135
		$this->expectException( InvalidArgumentException::class );
136
		$list->getBySiteId( 32202 );
137
	}
138
139
	public function testGivenUnknownSiteId_getBySiteIdThrowsException() {
140
		$link = new SiteLink( 'first', 'one' );
141
142
		$list = new SiteLinkList( [ $link ] );
143
144
		$this->expectException( OutOfBoundsException::class );
145
		$list->getBySiteId( 'foo' );
146
	}
147
148
	public function testGivenKnownSiteId_getBySiteIdReturnsSiteLink() {
149
		$link = new SiteLink( 'first', 'one' );
150
151
		$list = new SiteLinkList( [ $link ] );
152
153
		$this->assertEquals( $link, $list->getBySiteId( 'first' ) );
154
	}
155
156
	/**
157
	 * @dataProvider siteLinkArrayProvider
158
	 */
159
	public function testGivenTheSameSet_equalsReturnsTrue( array $links ) {
160
		$list = new SiteLinkList( $links );
161
		$this->assertTrue( $list->equals( $list ) );
162
		$this->assertTrue( $list->equals( unserialize( serialize( $list ) ) ) );
163
	}
164
165
	public function testGivenNonSiteLinkList_equalsReturnsFalse() {
166
		$set = new SiteLinkList();
167
		$this->assertFalse( $set->equals( null ) );
168
		$this->assertFalse( $set->equals( new \stdClass() ) );
169
	}
170
171
	public function testGivenDifferentList_equalsReturnsFalse() {
172
		$listOne = new SiteLinkList( [
173
			new SiteLink( 'foo', 'spam' ),
174
			new SiteLink( 'bar', 'hax' ),
175
		] );
176
177
		$listTwo = new SiteLinkList( [
178
			new SiteLink( 'foo', 'spam' ),
179
			new SiteLink( 'bar', 'HAX' ),
180
		] );
181
182
		$this->assertFalse( $listOne->equals( $listTwo ) );
183
	}
184
185
	public function testGivenSetWithDifferentOrder_equalsReturnsTrue() {
186
		$listOne = new SiteLinkList( [
187
			new SiteLink( 'foo', 'spam' ),
188
			new SiteLink( 'bar', 'hax' ),
189
		] );
190
191
		$listTwo = new SiteLinkList( [
192
			new SiteLink( 'bar', 'hax' ),
193
			new SiteLink( 'foo', 'spam' ),
194
		] );
195
196
		$this->assertTrue( $listOne->equals( $listTwo ) );
197
	}
198
199
	public function testGivenNonSiteId_removeSiteWithIdThrowsException() {
200
		$list = new SiteLinkList();
201
202
		$this->expectException( InvalidArgumentException::class );
203
		$list->removeLinkWithSiteId( [] );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

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...
204
	}
205
206
	public function testGivenKnownId_removeSiteWithIdRemovesIt() {
207
		$list = new SiteLinkList( [
208
			new SiteLink( 'foo', 'spam' ),
209
			new SiteLink( 'bar', 'hax' ),
210
		] );
211
212
		$list->removeLinkWithSiteId( 'foo' );
213
214
		$this->assertFalse( $list->hasLinkWithSiteId( 'foo' ) );
215
		$this->assertTrue( $list->hasLinkWithSiteId( 'bar' ) );
216
	}
217
218
	public function testGivenUnknownId_removeSiteWithIdDoesNoOp() {
219
		$list = new SiteLinkList( [
220
			new SiteLink( 'foo', 'spam' ),
221
			new SiteLink( 'bar', 'hax' ),
222
		] );
223
224
		$expected = clone $list;
225
226
		$list->removeLinkWithSiteId( 'baz' );
227
228
		$this->assertTrue( $expected->equals( $list ) );
229
	}
230
231
	public function testDifferentInstancesWithSameBadgesAreEqual() {
232
		$list = new SiteLinkList( [
233
			new SiteLink( 'foo', 'spam', new ItemIdSet( [
234
				new ItemId( 'Q42' ),
235
				new ItemId( 'Q1337' )
236
			] ) ),
237
		] );
238
239
		$otherInstance = unserialize( serialize( $list ) );
240
241
		$this->assertTrue( $list->equals( $otherInstance ) );
242
	}
243
244
	public function testAddNewSiteLink() {
245
		$list = new SiteLinkList();
246
247
		$list->addNewSiteLink( 'enwiki', 'cats' );
248
		$list->addNewSiteLink( 'dewiki', 'katzen', [ new ItemId( 'Q1' ) ] );
249
250
		$this->assertTrue( $list->equals( new SiteLinkList( [
251
			new SiteLink( 'enwiki', 'cats' ),
252
			new SiteLink( 'dewiki', 'katzen', [ new ItemId( 'Q1' ) ] ),
253
		] ) ) );
254
	}
255
256
	public function testAddSiteLink() {
257
		$list = new SiteLinkList();
258
259
		$list->addSiteLink( new SiteLink( 'enwiki', 'cats' ) );
260
		$list->addSiteLink( new SiteLink( 'dewiki', 'katzen' ) );
261
262
		$this->assertTrue( $list->equals( new SiteLinkList( [
263
			new SiteLink( 'enwiki', 'cats' ),
264
			new SiteLink( 'dewiki', 'katzen' ),
265
		] ) ) );
266
	}
267
268
	public function testToArray() {
269
		$list = new SiteLinkList();
270
		$list->addNewSiteLink( 'enwiki', 'foo' );
271
		$list->addNewSiteLink( 'dewiki', 'bar' );
272
273
		$expected = [
274
			'enwiki' => new SiteLink( 'enwiki', 'foo' ),
275
			'dewiki' => new SiteLink( 'dewiki', 'bar' ),
276
		];
277
278
		$this->assertEquals( $expected, $list->toArray() );
279
	}
280
281
	public function testGivenNewSiteLink_setSiteLinkAddsIt() {
282
		$list = new SiteLinkList();
283
		$list->setSiteLink( new SiteLink( 'enwiki', 'foo' ) );
284
285
		$expectedList = new SiteLinkList();
286
		$expectedList->addNewSiteLink( 'enwiki', 'foo' );
287
288
		$this->assertEquals( $expectedList, $list );
289
	}
290
291
	public function testGivenSiteLinkWithExistingId_setSiteLinkReplacesIt() {
292
		$list = new SiteLinkList();
293
		$list->addNewSiteLink( 'enwiki', 'foo' );
294
		$list->addNewSiteLink( 'dewiki', 'bar' );
295
		$list->setSiteLink( new SiteLink( 'enwiki', 'HAX' ) );
296
297
		$expectedList = new SiteLinkList();
298
		$expectedList->addNewSiteLink( 'enwiki', 'HAX' );
299
		$expectedList->addNewSiteLink( 'dewiki', 'bar' );
300
301
		$this->assertEquals( $expectedList, $list );
302
	}
303
304
	public function testGivenNewSiteLink_setNewSiteLinkAddsIt() {
305
		$list = new SiteLinkList();
306
		$list->setNewSiteLink( 'enwiki', 'foo' );
307
308
		$expectedList = new SiteLinkList();
309
		$expectedList->addNewSiteLink( 'enwiki', 'foo' );
310
311
		$this->assertEquals( $expectedList, $list );
312
	}
313
314
	public function testGivenSiteLinkWithExistingId_setNewSiteLinkReplacesIt() {
315
		$list = new SiteLinkList();
316
		$list->addNewSiteLink( 'enwiki', 'foo' );
317
		$list->addNewSiteLink( 'dewiki', 'bar' );
318
		$list->setNewSiteLink( 'enwiki', 'HAX' );
319
320
		$expectedList = new SiteLinkList();
321
		$expectedList->addNewSiteLink( 'enwiki', 'HAX' );
322
		$expectedList->addNewSiteLink( 'dewiki', 'bar' );
323
324
		$this->assertEquals( $expectedList, $list );
325
	}
326
327
	public function testEmptyListHasCountZero() {
328
		$this->assertSame( 0, ( new SiteLinkList() )->count() );
329
	}
330
331
	public function testListWithElementsHasCorrectCount() {
332
		$list = new SiteLinkList();
333
		$list->addNewSiteLink( 'enwiki', 'foo' );
334
		$list->addNewSiteLink( 'dewiki', 'bar' );
335
		$list->setNewSiteLink( 'nlwiki', 'baz' );
336
337
		$this->assertSame( 3, $list->count() );
338
	}
339
340
	public function testCanConstructWithIterable() {
341
		$links = [ new SiteLink( 'enwiki', 'foo' ) ];
342
343
		$this->assertEquals(
344
			new SiteLinkList( $links ),
345
			new SiteLinkList( new SiteLinkList( $links ) )
0 ignored issues
show
Documentation introduced by
new \Wikibase\DataModel\SiteLinkList($links) is of type object<Wikibase\DataModel\SiteLinkList>, but the function expects a object<Wikibase\DataMode...se\DataModel\SiteLink>>.

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...
346
		);
347
	}
348
349
	public function testWhenProvidingNonIterable_constructorThrowsException() {
350
		$this->setExpectedException( InvalidArgumentException::class );
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; use expectException() instead

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...
351
		new SiteLinkList( new SiteLink( 'enwiki', 'foo' ) );
0 ignored issues
show
Documentation introduced by
new \Wikibase\DataModel\SiteLink('enwiki', 'foo') is of type object<Wikibase\DataModel\SiteLink>, but the function expects a object<Wikibase\DataMode...se\DataModel\SiteLink>>.

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...
352
	}
353
354
}
355