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