Completed
Push — master ( 942d83...2583a5 )
by Leszek
02:49 queued 02:22
created

SiteLinkListTest::testEmptyListHasCountZero()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests;
4
5
use Wikibase\DataModel\Entity\ItemId;
6
use Wikibase\DataModel\Entity\ItemIdSet;
7
use Wikibase\DataModel\SiteLink;
8
use Wikibase\DataModel\SiteLinkList;
9
10
/**
11
 * @covers Wikibase\DataModel\SiteLinkList
12
 *
13
 * @license GPL-2.0+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class SiteLinkListTest extends \PHPUnit_Framework_TestCase {
17
18
	/**
19
	 * @dataProvider notSiteLinksProvider
20
	 */
21
	public function testGivenNonSiteLinks_constructorThrowsException( array $notSiteLinks ) {
22
		$this->setExpectedException( 'InvalidArgumentException' );
23
		new SiteLinkList( $notSiteLinks );
24
	}
25
26
	public function notSiteLinksProvider() {
27
		return [
28
			[
29
				[
30
					null
31
				]
32
			],
33
34
			[
35
				[
36
					42
37
				]
38
			],
39
40
			[
41
				[
42
					new SiteLink( 'foo', 'bar' ),
43
					42,
44
					new SiteLink( 'baz', 'bah' ),
45
				]
46
			],
47
		];
48
	}
49
50
	/**
51
	 * @dataProvider siteLinkArrayProvider
52
	 */
53
	public function testInputRoundtripsUsingIteratorToArray( array $siteLinkArray ) {
54
		$list = new SiteLinkList( $siteLinkArray );
55
		$this->assertEquals( $siteLinkArray, array_values( iterator_to_array( $list ) ) );
56
	}
57
58
	public function siteLinkArrayProvider() {
59
		return [
60
			[
61
				[
62
				]
63
			],
64
65
			[
66
				[
67
					new SiteLink( 'foo', 'bar' )
68
				]
69
			],
70
71
			[
72
				[
73
					new SiteLink( 'foo', 'bar' ),
74
					new SiteLink( 'baz', 'bah' ),
75
					new SiteLink( 'hax', 'bar' ),
76
				]
77
			],
78
		];
79
	}
80
81
	public function testEmptyCollectionHasZeroSize() {
82
		$list = new SiteLinkList( [] );
83
		$this->assertCount( 0, $list );
84
	}
85
86
	/**
87
	 * @dataProvider siteLinkArrayWithDuplicateSiteIdProvider
88
	 */
89
	public function testGivenSiteIdTwice_constructorThrowsException( array $siteLinkArray ) {
90
		$this->setExpectedException( 'InvalidArgumentException' );
91
		new SiteLinkList( $siteLinkArray );
92
	}
93
94
	public function siteLinkArrayWithDuplicateSiteIdProvider() {
95
		return [
96
			[
97
				[
98
					new SiteLink( 'foo', 'bar' ),
99
					new SiteLink( 'foo', 'bar' ),
100
				]
101
			],
102
103
			[
104
				[
105
					new SiteLink( 'foo', 'one' ),
106
					new SiteLink( 'baz', 'two' ),
107
					new SiteLink( 'foo', 'tree' ),
108
				]
109
			],
110
		];
111
	}
112
113
	public function testGetIteratorReturnsTraversableWithSiteIdKeys() {
114
		$list = new SiteLinkList( [
115
			new SiteLink( 'first', 'one' ),
116
			new SiteLink( 'second', 'two' ),
117
			new SiteLink( 'third', 'tree' ),
118
		] );
119
120
		$this->assertEquals(
121
			[
122
				'first' => new SiteLink( 'first', 'one' ),
123
				'second' => new SiteLink( 'second', 'two' ),
124
				'third' => new SiteLink( 'third', 'tree' ),
125
			],
126
			iterator_to_array( $list )
127
		);
128
	}
129
130
	public function testGivenNonString_getBySiteIdThrowsException() {
131
		$list = new SiteLinkList( [] );
132
133
		$this->setExpectedException( 'InvalidArgumentException' );
134
		$list->getBySiteId( 32202 );
135
	}
136
137
	public function testGivenUnknownSiteId_getBySiteIdThrowsException() {
138
		$link = new SiteLink( 'first', 'one' );
139
140
		$list = new SiteLinkList( [ $link ] );
141
142
		$this->setExpectedException( 'OutOfBoundsException' );
143
		$list->getBySiteId( 'foo' );
144
	}
145
146
	public function testGivenKnownSiteId_getBySiteIdReturnsSiteLink() {
147
		$link = new SiteLink( 'first', 'one' );
148
149
		$list = new SiteLinkList( [ $link ] );
150
151
		$this->assertEquals( $link, $list->getBySiteId( 'first' ) );
152
	}
153
154
	/**
155
	 * @dataProvider siteLinkArrayProvider
156
	 */
157
	public function testGivenTheSameSet_equalsReturnsTrue( array $links ) {
158
		$list = new SiteLinkList( $links );
159
		$this->assertTrue( $list->equals( $list ) );
160
		$this->assertTrue( $list->equals( unserialize( serialize( $list ) ) ) );
161
	}
162
163
	public function testGivenNonSiteLinkList_equalsReturnsFalse() {
164
		$set = new SiteLinkList();
165
		$this->assertFalse( $set->equals( null ) );
166
		$this->assertFalse( $set->equals( new \stdClass() ) );
167
	}
168
169
	public function testGivenDifferentList_equalsReturnsFalse() {
170
		$listOne = new SiteLinkList( [
171
			new SiteLink( 'foo', 'spam' ),
172
			new SiteLink( 'bar', 'hax' ),
173
		] );
174
175
		$listTwo = new SiteLinkList( [
176
			new SiteLink( 'foo', 'spam' ),
177
			new SiteLink( 'bar', 'HAX' ),
178
		] );
179
180
		$this->assertFalse( $listOne->equals( $listTwo ) );
181
	}
182
183
	public function testGivenSetWithDifferentOrder_equalsReturnsTrue() {
184
		$listOne = new SiteLinkList( [
185
			new SiteLink( 'foo', 'spam' ),
186
			new SiteLink( 'bar', 'hax' ),
187
		] );
188
189
		$listTwo = new SiteLinkList( [
190
			new SiteLink( 'bar', 'hax' ),
191
			new SiteLink( 'foo', 'spam' ),
192
		] );
193
194
		$this->assertTrue( $listOne->equals( $listTwo ) );
195
	}
196
197
	public function testGivenNonSiteId_removeSiteWithIdThrowsException() {
198
		$list = new SiteLinkList();
199
200
		$this->setExpectedException( 'InvalidArgumentException' );
201
		$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...
202
	}
203
204
	public function testGivenKnownId_removeSiteWithIdRemovesIt() {
205
		$list = new SiteLinkList( [
206
			new SiteLink( 'foo', 'spam' ),
207
			new SiteLink( 'bar', 'hax' ),
208
		] );
209
210
		$list->removeLinkWithSiteId( 'foo' );
211
212
		$this->assertFalse( $list->hasLinkWithSiteId( 'foo' ) );
213
		$this->assertTrue( $list->hasLinkWithSiteId( 'bar' ) );
214
	}
215
216
	public function testGivenUnknownId_removeSiteWithIdDoesNoOp() {
217
		$list = new SiteLinkList( [
218
			new SiteLink( 'foo', 'spam' ),
219
			new SiteLink( 'bar', 'hax' ),
220
		] );
221
222
		$expected = clone $list;
223
224
		$list->removeLinkWithSiteId( 'baz' );
225
226
		$this->assertTrue( $expected->equals( $list ) );
227
	}
228
229
	public function testDifferentInstancesWithSameBadgesAreEqual() {
230
		$list = new SiteLinkList( [
231
			new SiteLink( 'foo', 'spam', new ItemIdSet( [
232
				new ItemId( 'Q42' ),
233
				new ItemId( 'Q1337' )
234
			] ) ),
235
		] );
236
237
		$otherInstance = unserialize( serialize( $list ) );
238
239
		$this->assertTrue( $list->equals( $otherInstance ) );
240
	}
241
242
	public function testAddNewSiteLink() {
243
		$list = new SiteLinkList();
244
245
		$list->addNewSiteLink( 'enwiki', 'cats' );
246
		$list->addNewSiteLink( 'dewiki', 'katzen', [ new ItemId( 'Q1' ) ] );
247
248
		$this->assertTrue( $list->equals( new SiteLinkList( [
249
			new SiteLink( 'enwiki', 'cats' ),
250
			new SiteLink( 'dewiki', 'katzen', [ new ItemId( 'Q1' ) ] ),
251
		] ) ) );
252
	}
253
254
	public function testAddSiteLink() {
255
		$list = new SiteLinkList();
256
257
		$list->addSiteLink( new SiteLink( 'enwiki', 'cats' ) );
258
		$list->addSiteLink( new SiteLink( 'dewiki', 'katzen' ) );
259
260
		$this->assertTrue( $list->equals( new SiteLinkList( [
261
			new SiteLink( 'enwiki', 'cats' ),
262
			new SiteLink( 'dewiki', 'katzen' ),
263
		] ) ) );
264
	}
265
266
	public function testToArray() {
267
		$list = new SiteLinkList();
268
		$list->addNewSiteLink( 'enwiki', 'foo' );
269
		$list->addNewSiteLink( 'dewiki', 'bar' );
270
271
		$expected = [
272
			'enwiki' => new SiteLink( 'enwiki', 'foo' ),
273
			'dewiki' => new SiteLink( 'dewiki', 'bar' ),
274
		];
275
276
		$this->assertEquals( $expected, $list->toArray() );
277
	}
278
279
	public function testGivenNewSiteLink_setSiteLinkAddsIt() {
280
		$list = new SiteLinkList();
281
		$list->setSiteLink( new SiteLink( 'enwiki', 'foo' ) );
282
283
		$expectedList = new SiteLinkList();
284
		$expectedList->addNewSiteLink( 'enwiki', 'foo' );
285
286
		$this->assertEquals( $expectedList, $list );
287
	}
288
289
	public function testGivenSiteLinkWithExistingId_setSiteLinkReplacesIt() {
290
		$list = new SiteLinkList();
291
		$list->addNewSiteLink( 'enwiki', 'foo' );
292
		$list->addNewSiteLink( 'dewiki', 'bar' );
293
		$list->setSiteLink( new SiteLink( 'enwiki', 'HAX' ) );
294
295
		$expectedList = new SiteLinkList();
296
		$expectedList->addNewSiteLink( 'enwiki', 'HAX' );
297
		$expectedList->addNewSiteLink( 'dewiki', 'bar' );
298
299
		$this->assertEquals( $expectedList, $list );
300
	}
301
302
	public function testGivenNewSiteLink_setNewSiteLinkAddsIt() {
303
		$list = new SiteLinkList();
304
		$list->setNewSiteLink( 'enwiki', 'foo' );
305
306
		$expectedList = new SiteLinkList();
307
		$expectedList->addNewSiteLink( 'enwiki', 'foo' );
308
309
		$this->assertEquals( $expectedList, $list );
310
	}
311
312
	public function testGivenSiteLinkWithExistingId_setNewSiteLinkReplacesIt() {
313
		$list = new SiteLinkList();
314
		$list->addNewSiteLink( 'enwiki', 'foo' );
315
		$list->addNewSiteLink( 'dewiki', 'bar' );
316
		$list->setNewSiteLink( 'enwiki', 'HAX' );
317
318
		$expectedList = new SiteLinkList();
319
		$expectedList->addNewSiteLink( 'enwiki', 'HAX' );
320
		$expectedList->addNewSiteLink( 'dewiki', 'bar' );
321
322
		$this->assertEquals( $expectedList, $list );
323
	}
324
325
	public function testEmptyListHasCountZero() {
326
		$this->assertSame( 0, ( new SiteLinkList() )->count() );
327
	}
328
329
	public function testListWithElementsHasCorrectCount() {
330
		$list = new SiteLinkList();
331
		$list->addNewSiteLink( 'enwiki', 'foo' );
332
		$list->addNewSiteLink( 'dewiki', 'bar' );
333
		$list->setNewSiteLink( 'nlwiki', 'baz' );
334
335
		$this->assertSame( 3, $list->count() );
336
	}
337
338
}
339