SiteLinkTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 203
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 4 1
A testGetSiteId() 0 4 1
A siteIdProvider() 0 9 1
A invalidStringIdentifierProvider() 0 9 1
A testGetPageName() 0 4 1
A pageNameProvider() 0 9 1
A testGetBadges() 0 4 1
A badgesProvider() 0 36 1
A invalidBadgesProvider() 0 14 1
A testSelfComparisonReturnsTrue() 0 7 1
A linkProvider() 0 7 1
A testGivenNonEqualLinks_equalsReturnsFalse() 0 4 1
A nonEqualityProvider() 0 20 1
A testCanConstructWithItemIdSet() 0 11 1
A testGivenNonItemIdCollectionForBadges_constructorThrowsException() 0 4 1
A testCannotConstructWithNonStringSiteId() 0 4 1
A testCannotConstructWithNonStringPageName() 0 4 1
A testCannotConstructWithInvalidBadges() 0 4 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\ItemIdSet;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\SiteLink;
10
11
/**
12
 * @covers \Wikibase\DataModel\SiteLink
13
 *
14
 * @group Wikibase
15
 * @group WikibaseDataModel
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Michał Łazowik
20
 * @author Thiemo Kreuz
21
 */
22
class SiteLinkTest extends \PHPUnit\Framework\TestCase {
23
24
	public function testCanConstruct() {
25
		new SiteLink( 'enwiki', 'Wikidata' );
26
		$this->assertTrue( true );
27
	}
28
29
	/**
30
	 * @dataProvider siteIdProvider
31
	 */
32
	public function testGetSiteId( $siteId ) {
33
		$siteLink = new SiteLink( $siteId, 'Wikidata' );
34
		$this->assertSame( $siteId, $siteLink->getSiteId() );
35
	}
36
37
	public function siteIdProvider() {
38
		$argLists = [];
39
40
		$argLists[] = [ 'enwiki' ];
41
		$argLists[] = [ 'nlwiki' ];
42
		$argLists[] = [ 'Nyan!' ];
43
44
		return $argLists;
45
	}
46
47
	/**
48
	 * @dataProvider invalidStringIdentifierProvider
49
	 */
50
	public function testCannotConstructWithNonStringSiteId( $invalidSiteId ) {
51
		$this->expectException( InvalidArgumentException::class );
52
		new SiteLink( $invalidSiteId, 'Wikidata' );
53
	}
54
55
	public function invalidStringIdentifierProvider() {
56
		return [
57
			[ null ],
58
			[ true ],
59
			[ 42 ],
60
			[ '' ],
61
			[ [] ],
62
		];
63
	}
64
65
	/**
66
	 * @dataProvider pageNameProvider
67
	 */
68
	public function testGetPageName( $pageName ) {
69
		$siteLink = new SiteLink( 'enwiki', $pageName );
70
		$this->assertSame( $pageName, $siteLink->getPageName() );
71
	}
72
73
	public function pageNameProvider() {
74
		$argLists = [];
75
76
		$argLists[] = [ 'Wikidata' ];
77
		$argLists[] = [ 'Nyan_Cat' ];
78
		$argLists[] = [ 'NYAN DATA ALL ACROSS THE SKY ~=[,,_,,]:3' ];
79
80
		return $argLists;
81
	}
82
83
	/**
84
	 * @dataProvider invalidStringIdentifierProvider
85
	 */
86
	public function testCannotConstructWithNonStringPageName( $invalidPageName ) {
87
		$this->expectException( InvalidArgumentException::class );
88
		new SiteLink( 'enwiki', $invalidPageName );
89
	}
90
91
	/**
92
	 * @dataProvider badgesProvider
93
	 */
94
	public function testGetBadges( $badges, $expected ) {
95
		$siteLink = new SiteLink( 'enwiki', 'Wikidata', $badges );
96
		$this->assertEquals( $expected, $siteLink->getBadges() );
97
	}
98
99
	public function badgesProvider() {
100
		$argLists = [];
101
102
		$argLists[] = [ null, [] ];
103
104
		$badges = [];
105
		$expected = array_values( $badges );
106
107
		$argLists[] = [ $badges, $expected ];
108
109
		$badges = [
110
			new ItemId( 'Q149' )
111
		];
112
		$expected = array_values( $badges );
113
114
		$argLists[] = [ $badges, $expected ];
115
116
		// removing from the middle of array
117
		$badges = [
118
			new ItemId( 'Q36' ),
119
			new ItemId( 'Q149' ),
120
			new ItemId( 'Q7' )
121
		];
122
123
		$key = array_search(
124
			new ItemId( 'Q149' ),
125
			$badges
126
		);
127
		unset( $badges[$key] );
128
129
		$expected = array_values( $badges );
130
131
		$argLists[] = [ $badges, $expected ];
132
133
		return $argLists;
134
	}
135
136
	/**
137
	 * @dataProvider invalidBadgesProvider
138
	 */
139
	public function testCannotConstructWithInvalidBadges( $invalidBadges ) {
140
		$this->expectException( InvalidArgumentException::class );
141
		new SiteLink( 'enwiki', 'Wikidata', $invalidBadges );
142
	}
143
144
	public function invalidBadgesProvider() {
145
		return [
146
			// Stuff that's not an array
147
			[ true ],
148
			[ 42 ],
149
			[ 'nyan nyan' ],
150
			// Arrays with stuff that's not even an object
151
			[ [ 'nyan', 42 ] ],
152
			[ [ 'nyan', [] ] ],
153
			// Arrays with Entities that aren't Items
154
			[ [ new PropertyId( 'P2' ), new ItemId( 'Q149' ) ] ],
155
			[ [ new PropertyId( 'P2' ), new PropertyId( 'P3' ) ] ],
156
		];
157
	}
158
159
	/**
160
	 * @dataProvider linkProvider
161
	 */
162
	public function testSelfComparisonReturnsTrue( SiteLink $link ) {
163
		$this->assertTrue( $link->equals( $link ) );
164
165
		$linkCopy = unserialize( serialize( $link ) );
166
		$this->assertTrue( $link->equals( $linkCopy ) );
167
		$this->assertTrue( $linkCopy->equals( $link ) );
168
	}
169
170
	public function linkProvider() {
171
		return [
172
			[ new SiteLink( 'foo', 'Bar' ) ],
173
			[ new SiteLink( 'foo', 'Bar', [ new ItemId( 'Q42' ), new ItemId( 'Q9001' ) ] ) ],
174
			[ new SiteLink( 'foo', 'foo' ) ],
175
		];
176
	}
177
178
	/**
179
	 * @dataProvider nonEqualityProvider
180
	 */
181
	public function testGivenNonEqualLinks_equalsReturnsFalse( SiteLink $linkOne, SiteLink $linkTwo ) {
182
		$this->assertFalse( $linkOne->equals( $linkTwo ) );
183
		$this->assertFalse( $linkTwo->equals( $linkOne ) );
184
	}
185
186
	public function nonEqualityProvider() {
187
		return [
188
			[
189
				new SiteLink( 'foo', 'bar' ),
190
				new SiteLink( 'foo', 'Bar' ),
191
			],
192
			[
193
				new SiteLink( 'foo', 'bar' ),
194
				new SiteLink( 'Foo', 'bar' ),
195
			],
196
			[
197
				new SiteLink( 'foo', 'bar' ),
198
				new SiteLink( 'foo', 'bar', [ new ItemId( 'Q42' ) ] ),
199
			],
200
			[
201
				new SiteLink( 'foo', 'bar', [ new ItemId( 'Q42' ) ] ),
202
				new SiteLink( 'foo', 'bar', [ new ItemId( 'Q42' ), new ItemId( 'Q9001' ) ] ),
203
			],
204
		];
205
	}
206
207
	public function testCanConstructWithItemIdSet() {
208
		$badgesArray = [
209
			new ItemId( 'Q36' ),
210
			new ItemId( 'Q149' ),
211
		];
212
		$badges = new ItemIdSet( $badgesArray );
213
214
		$siteLink = new SiteLink( 'foo', 'bar', $badges );
215
216
		$this->assertEquals( $badgesArray, $siteLink->getBadges() );
217
	}
218
219
	public function testGivenNonItemIdCollectionForBadges_constructorThrowsException() {
220
		$this->expectException( InvalidArgumentException::class );
221
		new SiteLink( 'foo', 'bar', 42 );
0 ignored issues
show
Documentation introduced by
42 is of type integer, but the function expects a object<Wikibase\DataMode...el\Entity\ItemId>>|null.

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...
222
	}
223
224
}
225