Passed
Push — mwcredits ( 344ea7...bdc20a )
by no
07:28
created

PropertyIdTest::serializationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Entity\PropertyId;
7
8
/**
9
 * @covers Wikibase\DataModel\Entity\PropertyId
10
 * @covers Wikibase\DataModel\Entity\EntityId
11
 *
12
 * @group Wikibase
13
 * @group WikibaseDataModel
14
 * @group EntityIdTest
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class PropertyIdTest extends PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @dataProvider idSerializationProvider
23
	 */
24
	public function testCanConstructId( $idSerialization ) {
25
		$id = new PropertyId( $idSerialization );
26
27
		$this->assertEquals(
28
			strtoupper( $idSerialization ),
29
			$id->getSerialization()
30
		);
31
	}
32
33
	public function idSerializationProvider() {
34
		return array(
35
			array( 'p1' ),
36
			array( 'p100' ),
37
			array( 'p1337' ),
38
			array( 'p31337' ),
39
			array( 'P31337' ),
40
			array( 'P42' ),
41
			array( 'P2147483648' ),
42
		);
43
	}
44
45
	/**
46
	 * @dataProvider invalidIdSerializationProvider
47
	 */
48
	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) {
49
		$this->setExpectedException( 'InvalidArgumentException' );
50
		new PropertyId( $invalidSerialization );
51
	}
52
53
	public function invalidIdSerializationProvider() {
54
		return array(
55
			array( 'p' ),
56
			array( 'q1' ),
57
			array( 'pp1' ),
58
			array( '1p' ),
59
			array( 'p01' ),
60
			array( 'p 1' ),
61
			array( ' p1' ),
62
			array( 'p1 ' ),
63
			array( '1' ),
64
			array( ' ' ),
65
			array( '' ),
66
			array( '0' ),
67
			array( 0 ),
68
			array( 1 ),
69
		);
70
	}
71
72
	public function testGetNumericId() {
73
		$id = new PropertyId( 'P1' );
74
		$this->assertSame( 1, $id->getNumericId() );
75
	}
76
77
	public function testGetEntityType() {
78
		$id = new PropertyId( 'P1' );
79
		$this->assertSame( 'property', $id->getEntityType() );
80
	}
81
82
	public function testSerialize() {
83
		$id = new PropertyId( 'P1' );
84
		$this->assertSame( '["property","P1"]', $id->serialize() );
85
	}
86
87
	/**
88
	 * @dataProvider serializationProvider
89
	 */
90
	public function testUnserialize( $json, $expected ) {
91
		$id = new PropertyId( 'P1' );
92
		$id->unserialize( $json );
93
		$this->assertSame( $expected, $id->getSerialization() );
94
	}
95
96
	public function serializationProvider() {
97
		return array(
98
			array( '["property","P2"]', 'P2' ),
99
100
			// All these cases are kind of an injection vector and allow constructing invalid ids.
101
			array( '["string","P2"]', 'P2' ),
102
			array( '["","string"]', 'string' ),
103
			array( '["",""]', '' ),
104
			array( '["",2]', 2 ),
105
			array( '["",null]', null ),
106
			array( '', null ),
107
		);
108
	}
109
110
	/**
111
	 * @dataProvider numericIdProvider
112
	 */
113
	public function testNewFromNumber( $number ) {
114
		$id = PropertyId::newFromNumber( $number );
115
		$this->assertEquals( 'P' . $number, $id->getSerialization() );
116
	}
117
118
	public function numericIdProvider() {
119
		return array(
120
			array( 42 ),
121
			array( '42' ),
122
			array( 42.0 ),
123
			// Check for 32-bit integer overflow on 32-bit PHP systems.
124
			array( 2147483648 ),
125
			array( '2147483648' ),
126
		);
127
	}
128
129
	/**
130
	 * @dataProvider invalidNumericIdProvider
131
	 */
132
	public function testNewFromNumberWithInvalidNumericId( $number ) {
133
		$this->setExpectedException( 'InvalidArgumentException' );
134
		PropertyId::newFromNumber( $number );
135
	}
136
137
	public function invalidNumericIdProvider() {
138
		return array(
139
			array( 'P1' ),
140
			array( '42.1' ),
141
			array( 42.1 ),
142
			array( 2147483648.1 ),
143
		);
144
	}
145
146
}
147