Passed
Push — revert-676-revert-674-int32doc... ( f49f8e...534464 )
by no
04:37
created

EntityIdTest::serializationSplitProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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
 *
20
 * @license GPL-2.0+
21
 * @author Jeroen De Dauw < [email protected] >
22
 * @author John Erling Blad < [email protected] >
23
 */
24
class EntityIdTest extends \PHPUnit_Framework_TestCase {
25
26
	public function instanceProvider() {
27
		$ids = [];
28
29
		$ids[] = [ new ItemId( 'Q1' ), '' ];
30
		$ids[] = [ new ItemId( 'Q42' ), '' ];
31
		$ids[] = [ new ItemId( 'Q31337' ), '' ];
32
		$ids[] = [ new ItemId( 'Q2147483647' ), '' ];
33
		$ids[] = [ new ItemId( ':Q2147483647' ), '' ];
34
		$ids[] = [ new ItemId( 'foo:Q2147483647' ), 'foo' ];
35
		$ids[] = [ new PropertyId( 'P101010' ), '' ];
36
		$ids[] = [ new PropertyId( 'foo:bar:P101010' ), 'foo' ];
37
38
		return $ids;
39
	}
40
41
	/**
42
	 * @dataProvider instanceProvider
43
	 */
44
	public function testEqualsSimple( EntityId $id ) {
45
		$this->assertTrue( $id->equals( $id ) );
46
		$this->assertTrue( $id->equals( unserialize( serialize( $id ) ) ) );
47
		$this->assertFalse( $id->equals( $id->getSerialization() ) );
48
		$this->assertFalse( $id->equals( $id->getEntityType() ) );
49
	}
50
51
	/**
52
	 * @dataProvider instanceProvider
53
	 */
54
	public function testSerializationRoundtrip( EntityId $id ) {
55
		$this->assertEquals( $id, unserialize( serialize( $id ) ) );
56
	}
57
58
	public function testDeserializationCompatibility() {
59
		$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}';
60
61
		$this->assertEquals(
62
			new ItemId( 'q123' ),
63
			unserialize( $v05serialization )
64
		);
65
	}
66
67
	/**
68
	 * This test will change when the serialization format changes.
69
	 * If it is being changed intentionally, the test should be updated.
70
	 * It is just here to catch unintentional changes.
71
	 */
72
	public function testSerializationStability() {
73
		$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}';
74
		$id = new ItemId( 'q123' );
75
76
		$this->assertEquals(
77
			serialize( $id ),
78
			$v05serialization
79
		);
80
	}
81
82
	/**
83
	 * @dataProvider instanceProvider
84
	 */
85
	public function testReturnTypeOfToString( EntityId $id ) {
86
		$this->assertInternalType( 'string', $id->__toString() );
87
	}
88
89
	public function testIsForeign() {
90
		$this->assertFalse( ( new ItemId( 'Q42' ) )->isForeign() );
91
		$this->assertFalse( ( new ItemId( ':Q42' ) )->isForeign() );
92
		$this->assertTrue( ( new ItemId( 'foo:Q42' ) )->isForeign() );
93
		$this->assertFalse( ( new PropertyId( ':P42' ) )->isForeign() );
94
		$this->assertTrue( ( new PropertyId( 'foo:P42' ) )->isForeign() );
95
	}
96
97
	/**
98
	 * @dataProvider instanceProvider
99
	 */
100
	public function testGetRepositoryName( EntityId $id, $repoName ) {
101
		$this->assertSame( $repoName, $id->getRepositoryName() );
102
	}
103
104
	public function serializationSplitProvider() {
105
		return [
106
			[ 'Q42', [ '', '', 'Q42' ] ],
107
			[ 'foo:Q42', [ 'foo', '', 'Q42' ] ],
108
			[ '0:Q42', [ '0', '', 'Q42' ] ],
109
			[ 'foo:bar:baz:Q42', [ 'foo', 'bar:baz', 'Q42' ] ],
110
		];
111
	}
112
113
	/**
114
	 * @dataProvider serializationSplitProvider
115
	 */
116
	public function testSplitSerialization( $serialization, $split ) {
117
		$this->assertSame( $split, EntityId::splitSerialization( $serialization ) );
118
	}
119
120
	/**
121
	 * @dataProvider invalidSerializationProvider
122
	 */
123
	public function testSplitSerializationFails_GivenInvalidSerialization( $serialization ) {
124
		$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...
125
		EntityId::splitSerialization( $serialization );
126
	}
127
128
	/**
129
	 * @dataProvider serializationSplitProvider
130
	 */
131
	public function testJoinSerialization( $serialization, $split ) {
132
		$this->assertSame( $serialization, EntityId::joinSerialization( $split ) );
133
	}
134
135
	/**
136
	 * @dataProvider invalidJoinSerializationDataProvider
137
	 */
138
	public function testJoinSerializationFails_GivenEmptyId( $parts ) {
139
		$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...
140
		EntityId::joinSerialization( $parts );
141
	}
142
143
	public function invalidJoinSerializationDataProvider() {
144
		return [
145
			[ [ 'Q42', '', '' ] ],
146
			[ [ '', 'Q42', '' ] ],
147
			[ [ 'foo', 'Q42', '' ] ],
148
		];
149
	}
150
151
	public function testGivenNotNormalizedSerialization_splitSerializationReturnsNormalizedParts() {
152
		$this->assertSame( [ '', '', 'Q42' ], EntityId::splitSerialization( ':Q42' ) );
153
		$this->assertSame( [ 'foo', 'bar', 'Q42' ], EntityId::splitSerialization( ':foo:bar:Q42' ) );
154
	}
155
156
	public function localPartDataProvider() {
157
		return [
158
			[ 'Q42', 'Q42' ],
159
			[ ':Q42', 'Q42' ],
160
			[ 'foo:Q42', 'Q42' ],
161
			[ 'foo:bar:Q42', 'bar:Q42' ],
162
		];
163
	}
164
165
	/**
166
	 * @dataProvider localPartDataProvider
167
	 */
168
	public function testGetLocalPart( $serialization, $localPart ) {
169
		$id = new ItemId( $serialization );
170
		$this->assertSame( $localPart, $id->getLocalPart() );
171
	}
172
173
	public function invalidSerializationProvider() {
174
		return [
175
			[ 's p a c e s:Q42' ],
176
			[ '::Q42' ],
177
			[ '' ],
178
			[ ':' ],
179
			[ 42 ],
180
			[ null ],
181
		];
182
	}
183
184
	/**
185
	 * @dataProvider invalidSerializationProvider
186
	 */
187
	public function testConstructor( $serialization ) {
188
		$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...
189
190
		$mock = $this->getMockBuilder( EntityId::class )
191
			->disableOriginalConstructor()
192
			->getMockForAbstractClass();
193
194
		$constructor = ( new ReflectionClass( EntityId::class ) )->getConstructor();
195
		$constructor->invoke( $mock, $serialization );
196
	}
197
198
}
199