Passed
Push — jsonTests ( bfbd0f...4da890 )
by no
11:10 queued 13s
created

PropertyIdTest::jsonProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
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 11
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( '', null ),
99
			array( '["",null]', null ),
100
			array( '["",2]', 2 ),
101
			array( '["",""]', '' ),
102
			array( '["","string"]', 'string' ),
103
			array( '["string","P2"]', 'P2' ),
104
			array( '["property","P2"]', 'P2' ),
105
		);
106
	}
107
108
	/**
109
	 * @dataProvider numericIdProvider
110
	 */
111
	public function testNewFromNumber( $number ) {
112
		$id = PropertyId::newFromNumber( $number );
113
		$this->assertEquals( 'P' . $number, $id->getSerialization() );
114
	}
115
116
	public function numericIdProvider() {
117
		return array(
118
			array( 42 ),
119
			array( '42' ),
120
			array( 42.0 ),
121
			// Check for 32-bit integer overflow on 32-bit PHP systems.
122
			array( 2147483648 ),
123
			array( '2147483648' ),
124
		);
125
	}
126
127
	/**
128
	 * @dataProvider invalidNumericIdProvider
129
	 */
130
	public function testNewFromNumberWithInvalidNumericId( $number ) {
131
		$this->setExpectedException( 'InvalidArgumentException' );
132
		PropertyId::newFromNumber( $number );
133
	}
134
135
	public function invalidNumericIdProvider() {
136
		return array(
137
			array( 'P1' ),
138
			array( '42.1' ),
139
			array( 42.1 ),
140
			array( 2147483648.1 ),
141
		);
142
	}
143
144
}
145