Completed
Pull Request — master (#694)
by Leszek
04:07
created

PropertyIdTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 3
cbo 2
dl 0
loc 163
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstructId() 0 8 1
A idSerializationProvider() 0 14 1
A testCannotConstructWithInvalidSerialization() 0 4 1
A invalidIdSerializationProvider() 0 21 1
A testGetNumericId() 0 4 1
A testGetEntityType() 0 4 1
A testSerialize() 0 4 1
A testUnserialize() 0 5 1
A serializationProvider() 0 13 1
A testNewFromNumber() 0 4 1
A numericIdProvider() 0 10 1
A testNewFromNumberWithRepositoryName() 0 4 1
A testNewFromNumberWithInvalidNumericId() 0 4 1
A invalidNumericIdProvider() 0 9 1
A testNewFromNumberThrowsExceptionOnInvalidRepositoryName() 0 4 1
A provideInvalidRepositoryNames() 0 9 1
A testGetNumericIdThrowsExceptionOnForeignIds() 0 4 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Entity\PropertyId;
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Wikimedia\Assert\ParameterAssertionException;
10
11
/**
12
 * @covers Wikibase\DataModel\Entity\PropertyId
13
 * @covers Wikibase\DataModel\Entity\EntityId
14
 *
15
 * @group Wikibase
16
 * @group WikibaseDataModel
17
 *
18
 * @license GPL-2.0+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class PropertyIdTest extends PHPUnit_Framework_TestCase {
22
23
	/**
24
	 * @dataProvider idSerializationProvider
25
	 */
26
	public function testCanConstructId( $idSerialization, $normalizedIdSerialization ) {
27
		$id = new PropertyId( $idSerialization );
28
29
		$this->assertEquals(
30
			$normalizedIdSerialization,
31
			$id->getSerialization()
32
		);
33
	}
34
35
	public function idSerializationProvider() {
36
		return [
37
			[ 'p1', 'P1' ],
38
			[ 'p100', 'P100' ],
39
			[ 'p1337', 'P1337' ],
40
			[ 'p31337', 'P31337' ],
41
			[ 'P31337', 'P31337' ],
42
			[ 'P42', 'P42' ],
43
			[ ':P42', 'P42' ],
44
			[ 'foo:P42', 'foo:P42' ],
45
			[ 'foo:bar:p42', 'foo:bar:P42' ],
46
			[ 'P2147483647', 'P2147483647' ],
47
		];
48
	}
49
50
	/**
51
	 * @dataProvider invalidIdSerializationProvider
52
	 */
53
	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) {
54
		$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...
55
		new PropertyId( $invalidSerialization );
56
	}
57
58
	public function invalidIdSerializationProvider() {
59
		return [
60
			[ "P1\n" ],
61
			[ 'p' ],
62
			[ 'q1' ],
63
			[ 'pp1' ],
64
			[ '1p' ],
65
			[ 'p01' ],
66
			[ 'p 1' ],
67
			[ ' p1' ],
68
			[ 'p1 ' ],
69
			[ '1' ],
70
			[ ' ' ],
71
			[ '' ],
72
			[ '0' ],
73
			[ 0 ],
74
			[ 1 ],
75
			[ 'P2147483648' ],
76
			[ 'P99999999999' ],
77
		];
78
	}
79
80
	public function testGetNumericId() {
81
		$id = new PropertyId( 'P1' );
82
		$this->assertSame( 1, $id->getNumericId() );
83
	}
84
85
	public function testGetEntityType() {
86
		$id = new PropertyId( 'P1' );
87
		$this->assertSame( 'property', $id->getEntityType() );
88
	}
89
90
	public function testSerialize() {
91
		$id = new PropertyId( 'P1' );
92
		$this->assertSame( '["property","P1"]', $id->serialize() );
93
	}
94
95
	/**
96
	 * @dataProvider serializationProvider
97
	 */
98
	public function testUnserialize( $json, $expected ) {
99
		$id = new PropertyId( 'P1' );
100
		$id->unserialize( $json );
101
		$this->assertSame( $expected, $id->getSerialization() );
102
	}
103
104
	public function serializationProvider() {
105
		return [
106
			[ '["property","P2"]', 'P2' ],
107
108
			// All these cases are kind of an injection vector and allow constructing invalid ids.
109
			[ '["string","P2"]', 'P2' ],
110
			[ '["","string"]', 'string' ],
111
			[ '["",""]', '' ],
112
			[ '["",2]', 2 ],
113
			[ '["",null]', null ],
114
			[ '', null ],
115
		];
116
	}
117
118
	/**
119
	 * @dataProvider numericIdProvider
120
	 */
121
	public function testNewFromNumber( $number ) {
122
		$id = PropertyId::newFromNumber( $number );
123
		$this->assertEquals( 'P' . $number, $id->getSerialization() );
124
	}
125
126
	public function numericIdProvider() {
127
		return [
128
			[ 42 ],
129
			[ '42' ],
130
			[ 42.0 ],
131
			// Check for 32-bit integer overflow on 32-bit PHP systems.
132
			[ 2147483647 ],
133
			[ '2147483647' ],
134
		];
135
	}
136
137
	public function testNewFromNumberWithRepositoryName() {
138
		$id = PropertyId::newFromNumber( 123, 'foo' );
139
		$this->assertEquals( 'foo:P123', $id->getSerialization() );
140
	}
141
142
	/**
143
	 * @dataProvider invalidNumericIdProvider
144
	 */
145
	public function testNewFromNumberWithInvalidNumericId( $number ) {
146
		$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...
147
		PropertyId::newFromNumber( $number );
148
	}
149
150
	public function invalidNumericIdProvider() {
151
		return [
152
			[ 'P1' ],
153
			[ '42.1' ],
154
			[ 42.1 ],
155
			[ 2147483648 ],
156
			[ '2147483648' ],
157
		];
158
	}
159
160
	/**
161
	 * @dataProvider provideInvalidRepositoryNames
162
	 */
163
	public function testNewFromNumberThrowsExceptionOnInvalidRepositoryName( $repositoryName ) {
164
		$this->setExpectedException( ParameterAssertionException::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...
165
		PropertyId::newFromNumber( 123, $repositoryName );
166
	}
167
168
	public function provideInvalidRepositoryNames() {
169
		return [
170
			[ 'fo:o' ],
171
			[ ':' ],
172
			[ 100 ],
173
			[ false ],
174
			[ null ],
175
		];
176
	}
177
178
	public function testGetNumericIdThrowsExceptionOnForeignIds() {
179
		$this->setExpectedException( RuntimeException::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...
180
		( new PropertyId( 'foo:P42' ) )->getNumericId();
181
	}
182
183
}
184