Completed
Pull Request — master (#670)
by no
10:29 queued 06:38
created

SiteLinkTest::badgesProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 36
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
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+
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Michał Łazowik
20
 * @author Thiemo Mättig
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->assertEquals( $siteId, $siteLink->getSiteId() );
35
	}
36
37
	public function siteIdProvider() {
38
		$argLists = array();
39
40
		$argLists[] = array( 'enwiki' );
41
		$argLists[] = array( 'nlwiki' );
42
		$argLists[] = array( 'Nyan!' );
43
44
		return $argLists;
45
	}
46
47
	/**
48
	 * @dataProvider invalidStringIdentifierProvider
49
	 * @expectedException InvalidArgumentException
50
	 */
51
	public function testCannotConstructWithNonStringSiteId( $invalidSiteId ) {
52
		new SiteLink( $invalidSiteId, 'Wikidata' );
53
	}
54
55
	public function invalidStringIdentifierProvider() {
56
		return array(
57
			array( null ),
58
			array( true ),
59
			array( 42 ),
60
			array( '' ),
61
			array( array() ),
62
		);
63
	}
64
65
	/**
66
	 * @dataProvider pageNameProvider
67
	 */
68
	public function testGetPageName( $pageName ) {
69
		$siteLink = new SiteLink( 'enwiki', $pageName );
70
		$this->assertEquals( $pageName, $siteLink->getPageName() );
71
	}
72
73
	public function pageNameProvider() {
74
		$argLists = array();
75
76
		$argLists[] = array( 'Wikidata' );
77
		$argLists[] = array( 'Nyan_Cat' );
78
		$argLists[] = array( 'NYAN DATA ALL ACROSS THE SKY ~=[,,_,,]:3' );
79
80
		return $argLists;
81
	}
82
83
	/**
84
	 * @dataProvider invalidStringIdentifierProvider
85
	 * @expectedException InvalidArgumentException
86
	 */
87
	public function testCannotConstructWithNonStringPageName( $invalidPageName ) {
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 = array();
101
102
		$argLists[] = array( null, array() );
103
104
		$badges = array();
105
		$expected = array_values( $badges );
106
107
		$argLists[] = array( $badges, $expected );
108
109
		$badges = array(
110
			new ItemId( 'Q149' )
111
		);
112
		$expected = array_values( $badges );
113
114
		$argLists[] = array( $badges, $expected );
115
116
		// removing from the middle of array
117
		$badges = array(
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[] = array( $badges, $expected );
132
133
		return $argLists;
134
	}
135
136
	/**
137
	 * @dataProvider invalidBadgesProvider
138
	 * @expectedException InvalidArgumentException
139
	 */
140
	public function testCannotConstructWithInvalidBadges( $invalidBadges ) {
141
		new SiteLink( 'enwiki', 'Wikidata', $invalidBadges );
142
	}
143
144
	public function invalidBadgesProvider() {
145
		return array(
146
			// Stuff that's not an array
147
			array( true ),
148
			array( 42 ),
149
			array( 'nyan nyan' ),
150
			// Arrays with stuff that's not even an object
151
			array( array( 'nyan', 42 ) ),
152
			array( array( 'nyan', array() ) ),
153
			// Arrays with Entities that aren't Items
154
			array( array( new PropertyId( 'P2' ), new ItemId( 'Q149' ) ) ),
155
			array( array( 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 array(
172
			array( new SiteLink( 'foo', 'Bar' ) ),
173
			array( new SiteLink( 'foo', 'Bar', array( new ItemId( 'Q42' ), new ItemId( 'Q9001' ) ) ) ),
174
			array( 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 array(
188
			array(
189
				new SiteLink( 'foo', 'bar' ),
190
				new SiteLink( 'foo', 'Bar' ),
191
			),
192
			array(
193
				new SiteLink( 'foo', 'bar' ),
194
				new SiteLink( 'Foo', 'bar' ),
195
			),
196
			array(
197
				new SiteLink( 'foo', 'bar' ),
198
				new SiteLink( 'foo', 'bar', array( new ItemId( 'Q42' ) ) ),
199
			),
200
			array(
201
				new SiteLink( 'foo', 'bar', array( new ItemId( 'Q42' ) ) ),
202
				new SiteLink( 'foo', 'bar', array( new ItemId( 'Q42' ), new ItemId( 'Q9001' ) ) ),
203
			),
204
		);
205
	}
206
207
	public function testCanConstructWithItemIdSet() {
208
		$badgesArray = array(
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->setExpectedException( 'InvalidArgumentException' );
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

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...
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