Passed
Push — entityIdInjection ( 49cc31 )
by no
66:29 queued 55:58
created

PropertyIdTest::invalidSerializationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 8
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( $serialization, $expected ) {
91
		$id = new PropertyId( 'P1' );
92
		$id->unserialize( $serialization );
93
		$this->assertSame( $expected, $id->getSerialization() );
94
	}
95
96
	public function serializationProvider() {
97
		return array(
98
			// TODO: Inline!
99
			array( '["property","P2"]', 'P2' ),
100
		);
101
	}
102
103
	/**
104
	 * @dataProvider invalidSerializationProvider
105
	 */
106
	public function testUnserializeThrowsException( $serialization ) {
107
		$id = new PropertyId( 'P1' );
108
		$this->setExpectedException( 'InvalidArgumentException' );
109
		$id->unserialize( $serialization );
110
	}
111
112
	public function invalidSerializationProvider() {
113
		return array(
114
			array( '["string","P2"]', 'P2' ),
115
			array( '["","string"]', 'string' ),
116
			array( '["",""]', '' ),
117
			array( '["",2]', 2 ),
118
			array( '["",null]', null ),
119
			array( '', null ),
120
		);
121
	}
122
123
	/**
124
	 * @dataProvider numericIdProvider
125
	 */
126
	public function testNewFromNumber( $number ) {
127
		$id = PropertyId::newFromNumber( $number );
128
		$this->assertEquals( 'P' . $number, $id->getSerialization() );
129
	}
130
131
	public function numericIdProvider() {
132
		return array(
133
			array( 42 ),
134
			array( '42' ),
135
			array( 42.0 ),
136
			// Check for 32-bit integer overflow on 32-bit PHP systems.
137
			array( 2147483648 ),
138
			array( '2147483648' ),
139
		);
140
	}
141
142
	/**
143
	 * @dataProvider invalidNumericIdProvider
144
	 */
145
	public function testNewFromNumberWithInvalidNumericId( $number ) {
146
		$this->setExpectedException( 'InvalidArgumentException' );
147
		PropertyId::newFromNumber( $number );
148
	}
149
150
	public function invalidNumericIdProvider() {
151
		return array(
152
			array( 'P1' ),
153
			array( '42.1' ),
154
			array( 42.1 ),
155
			array( 2147483648.1 ),
156
		);
157
	}
158
159
}
160