Passed
Push — entityIdInjection ( ecb9ce...21ce29 )
by no
06:02
created

PropertyIdTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 2
cbo 2
dl 0
loc 131
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testCannotConstructWithInvalidSerialization() 0 4 1
A testGetNumericId() 0 4 1
A testGetEntityType() 0 4 1
A testSerialize() 0 4 1
A testCanConstructId() 0 8 1
A idSerializationProvider() 0 11 1
A invalidIdSerializationProvider() 0 18 1
A testUnserialize() 0 5 1
A testUnserializeThrowsException() 0 5 1
A invalidSerializationProvider() 0 10 1
A testNewFromNumber() 0 4 1
A numericIdProvider() 0 10 1
A testNewFromNumberWithInvalidNumericId() 0 4 1
A invalidNumericIdProvider() 0 8 1
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
	public function testUnserialize() {
88
		$id = new PropertyId( 'P1' );
89
		$id->unserialize( '["property","P2"]' );
90
		$this->assertSame( 'P2', $id->getSerialization() );
91
	}
92
93
	/**
94
	 * @dataProvider invalidSerializationProvider
95
	 */
96
	public function testUnserializeThrowsException( $serialization ) {
97
		$id = new PropertyId( 'P1' );
98
		$this->setExpectedException( 'InvalidArgumentException' );
99
		$id->unserialize( $serialization );
100
	}
101
102
	public function invalidSerializationProvider() {
103
		return array(
104
			array( '["string","P2"]', 'P2' ),
105
			array( '["","string"]', 'string' ),
106
			array( '["",""]', '' ),
107
			array( '["",2]', 2 ),
108
			array( '["",null]', null ),
109
			array( '', null ),
110
		);
111
	}
112
113
	/**
114
	 * @dataProvider numericIdProvider
115
	 */
116
	public function testNewFromNumber( $number ) {
117
		$id = PropertyId::newFromNumber( $number );
118
		$this->assertEquals( 'P' . $number, $id->getSerialization() );
119
	}
120
121
	public function numericIdProvider() {
122
		return array(
123
			array( 42 ),
124
			array( '42' ),
125
			array( 42.0 ),
126
			// Check for 32-bit integer overflow on 32-bit PHP systems.
127
			array( 2147483648 ),
128
			array( '2147483648' ),
129
		);
130
	}
131
132
	/**
133
	 * @dataProvider invalidNumericIdProvider
134
	 */
135
	public function testNewFromNumberWithInvalidNumericId( $number ) {
136
		$this->setExpectedException( 'InvalidArgumentException' );
137
		PropertyId::newFromNumber( $number );
138
	}
139
140
	public function invalidNumericIdProvider() {
141
		return array(
142
			array( 'P1' ),
143
			array( '42.1' ),
144
			array( 42.1 ),
145
			array( 2147483648.1 ),
146
		);
147
	}
148
149
}
150