Completed
Pull Request — master (#678)
by Jakob
03:27
created

EntityIdTest::invalidSerializationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use ReflectionClass;
6
use InvalidArgumentException;
7
use RuntimeException;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
12
/**
13
 * @covers Wikibase\DataModel\Entity\EntityId
14
 * @uses Wikibase\DataModel\Entity\ItemId
15
 * @uses Wikibase\DataModel\Entity\PropertyId
16
 *
17
 * @group Wikibase
18
 * @group WikibaseDataModel
19
 * @group EntityIdTest
20
 *
21
 * @license GPL-2.0+
22
 * @author Jeroen De Dauw < [email protected] >
23
 * @author John Erling Blad < [email protected] >
24
 */
25
class EntityIdTest extends \PHPUnit_Framework_TestCase {
26
27
	public function instanceProvider() {
28
		$ids = array();
29
30
		$ids[] = array( new ItemId( 'Q1' ), '' );
31
		$ids[] = array( new ItemId( 'Q42' ), '' );
32
		$ids[] = array( new ItemId( 'Q31337' ), '' );
33
		$ids[] = array( new ItemId( 'Q2147483647' ), '' );
34
		$ids[] = array( new ItemId( ':Q2147483647' ), '' );
35
		$ids[] = array( new ItemId( 'foo:Q2147483647' ), 'foo' );
36
		$ids[] = array( new PropertyId( 'P101010' ), '' );
37
		$ids[] = array( new PropertyId( 'foo:bar:P101010' ), 'foo' );
38
39
		return $ids;
40
	}
41
42
	/**
43
	 * @dataProvider instanceProvider
44
	 */
45
	public function testEqualsSimple( EntityId $id ) {
46
		$this->assertTrue( $id->equals( $id ) );
47
		$this->assertTrue( $id->equals( unserialize( serialize( $id ) ) ) );
48
		$this->assertFalse( $id->equals( $id->getSerialization() ) );
49
		$this->assertFalse( $id->equals( $id->getEntityType() ) );
50
	}
51
52
	/**
53
	 * @dataProvider instanceProvider
54
	 */
55
	public function testSerializationRoundtrip( EntityId $id ) {
56
		$this->assertEquals( $id, unserialize( serialize( $id ) ) );
57
	}
58
59
	public function testDeserializationCompatibility() {
60
		$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}';
61
62
		$this->assertEquals(
63
			new ItemId( 'q123' ),
64
			unserialize( $v05serialization )
65
		);
66
	}
67
68
	/**
69
	 * This test will change when the serialization format changes.
70
	 * If it is being changed intentionally, the test should be updated.
71
	 * It is just here to catch unintentional changes.
72
	 */
73
	public function testSerializationStability() {
74
		$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}';
75
		$id = new ItemId( 'q123' );
76
77
		$this->assertEquals(
78
			serialize( $id ),
79
			$v05serialization
80
		);
81
	}
82
83
	/**
84
	 * @dataProvider instanceProvider
85
	 */
86
	public function testReturnTypeOfToString( EntityId $id ) {
87
		$this->assertInternalType( 'string', $id->__toString() );
88
	}
89
90
	public function testIsForeign() {
91
		$this->assertFalse( ( new ItemId( 'Q42' ) )->isForeign() );
92
		$this->assertFalse( ( new ItemId( ':Q42' ) )->isForeign() );
93
		$this->assertTrue( ( new ItemId( 'foo:Q42' ) )->isForeign() );
94
		$this->assertFalse( ( new PropertyId( ':P42' ) )->isForeign() );
95
		$this->assertTrue( ( new PropertyId( 'foo:P42' ) )->isForeign() );
96
	}
97
98
	/**
99
	 * @dataProvider instanceProvider
100
	 */
101
	public function testGetRepositoryName( EntityId $id, $repoName ) {
102
		$this->assertSame( $repoName, $id->getRepositoryName() );
103
	}
104
105
	public function serializationSplitProvider() {
106
		return array(
107
			array( 'Q42', array( '', '', 'Q42' ) ),
108
			array( 'foo:Q42', array( 'foo', '', 'Q42' ) ),
109
			array( '0:Q42', array( '0', '', 'Q42' ) ),
110
			array( 'foo:bar:baz:Q42', array( 'foo', 'bar:baz', 'Q42' ) ),
111
		);
112
	}
113
114
	/**
115
	 * @dataProvider serializationSplitProvider
116
	 */
117
	public function testSplitSerialization( $serialization, $split ) {
118
		$this->assertSame( $split, EntityId::splitSerialization( $serialization ) );
119
	}
120
121
	/**
122
	 * @dataProvider invalidSerializationProvider
123
	 */
124
	public function testSplitSerializationFails_GivenInvalidSerialization( $serialization ) {
125
		$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

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...
126
		EntityId::splitSerialization( $serialization );
127
	}
128
129
	/**
130
	 * @dataProvider serializationSplitProvider
131
	 */
132
	public function testJoinSerialization( $serialization, $split ) {
133
		$this->assertSame( $serialization, EntityId::joinSerialization( $split ) );
134
	}
135
136
	/**
137
	 * @dataProvider invalidJoinSerializationDataProvider
138
	 */
139
	public function testJoinSerializationFails_GivenEmptyId( $parts ) {
140
		$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

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...
141
		EntityId::joinSerialization( $parts );
142
	}
143
144
	public function invalidJoinSerializationDataProvider() {
145
		return array(
146
			array( array( 'Q42', '', '' ) ),
147
			array( array( '', 'Q42', '' ) ),
148
			array( array( 'foo', 'Q42', '' ) ),
149
		);
150
	}
151
152
	public function testGivenNotNormalizedSerialization_splitSerializationReturnsNormalizedParts() {
153
		$this->assertSame( array( '', '', 'Q42' ), EntityId::splitSerialization( ':Q42' ) );
154
		$this->assertSame( array( 'foo', 'bar', 'Q42' ), EntityId::splitSerialization( ':foo:bar:Q42' ) );
155
	}
156
157
	public function localPartDataProvider() {
158
		return array(
159
			array( 'Q42', 'Q42' ),
160
			array( ':Q42', 'Q42' ),
161
			array( 'foo:Q42', 'Q42' ),
162
			array( 'foo:bar:Q42', 'bar:Q42' ),
163
		);
164
	}
165
166
	/**
167
	 * @dataProvider localPartDataProvider
168
	 */
169
	public function testGetLocalPart( $serialization, $localPart ) {
170
		$id = new ItemId( $serialization );
171
		$this->assertSame( $localPart, $id->getLocalPart() );
172
	}
173
174
	public function invalidSerializationProvider() {
175
		return array(
176
			array( 's p a c e s:Q42' ),
177
			array( '::Q42' ),
178
			array( '' ),
179
			array( ':' ),
180
			array( 42 ),
181
			array( null ),
182
		);
183
	}
184
185
	/**
186
	 * @dataProvider invalidSerializationProvider
187
	 */
188
	public function testConstructor( $serialization ) {
189
		$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

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...
190
191
		$mock = $this->getMockBuilder( EntityId::class )
192
			->disableOriginalConstructor()
193
			->getMockForAbstractClass();
194
195
		$constructor = ( new ReflectionClass( EntityId::class ) )->getConstructor();
196
		$constructor->invoke( $mock, $serialization );
197
	}
198
199
}
200