Completed
Push — master ( 422056...68667e )
by Leszek
02:49
created

PropertyValueSnakTest::provideDataToSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Snak;
4
5
use DataValues\DataValue;
6
use DataValues\StringValue;
7
use InvalidArgumentException;
8
use PHPUnit_Framework_TestCase;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\DataModel\Snak\PropertyValueSnak;
12
use Wikibase\DataModel\Snak\Snak;
13
14
/**
15
 * @covers Wikibase\DataModel\Snak\PropertyValueSnak
16
 * @covers Wikibase\DataModel\Snak\SnakObject
17
 * @uses DataValues\StringValue
18
 * @uses Wikibase\DataModel\Entity\EntityId
19
 * @uses Wikibase\DataModel\Entity\PropertyId
20
 * @uses DataValues\DataValueObject
21
 *
22
 * @group Wikibase
23
 * @group WikibaseDataModel
24
 * @group WikibaseSnak
25
 *
26
 * @license GPL-2.0+
27
 * @author Jeroen De Dauw < [email protected] >
28
 * @author Thiemo Mättig
29
 */
30
class PropertyValueSnakTest extends PHPUnit_Framework_TestCase {
31
32
	/**
33
	 * @dataProvider validConstructorArgumentsProvider
34
	 */
35
	public function testConstructor( $propertyId, DataValue $dataValue ) {
36
		$snak = new PropertyValueSnak( $propertyId, $dataValue );
37
		$this->assertInstanceOf( 'Wikibase\DataModel\Snak\PropertyValueSnak', $snak );
38
	}
39
40
	public function validConstructorArgumentsProvider() {
41
		return [
42
			[ 1, new StringValue( 'a' ) ],
43
			[ new PropertyId( 'P1' ), new StringValue( 'a' ) ],
44
			[ new PropertyId( 'P9001' ), new StringValue( 'bc' ) ],
45
		];
46
	}
47
48
	/**
49
	 * @dataProvider invalidConstructorArgumentsProvider
50
	 * @expectedException InvalidArgumentException
51
	 */
52
	public function testGivenInvalidConstructorArguments_constructorThrowsException(
53
		$propertyId,
54
		DataValue $dataValue
55
	) {
56
		new PropertyValueSnak( $propertyId, $dataValue );
57
	}
58
59
	public function invalidConstructorArgumentsProvider() {
60
		$stringValue = new StringValue( 'a' );
61
62
		return [
63
			[ null, $stringValue ],
64
			[ 0.1, $stringValue ],
65
			[ 'Q1', $stringValue ],
66
			[ new ItemId( 'Q1' ), $stringValue ],
67
		];
68
	}
69
70
	public function testGetPropertyId() {
71
		$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
72
		$propertyId = $snak->getPropertyId();
73
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\PropertyId', $propertyId );
74
	}
75
76
	public function testGetHash() {
77
		$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
78
		$hash = $snak->getHash();
79
		$this->assertInternalType( 'string', $hash );
80
		$this->assertEquals( 40, strlen( $hash ) );
81
	}
82
83
	/**
84
	 * This test is a safeguard to make sure hashes are not changed unintentionally.
85
	 */
86
	public function testHashStability() {
87
		$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
88
		$hash = $snak->getHash();
89
90
		// @codingStandardsIgnoreStart
91
		$expected = sha1( 'C:41:"Wikibase\DataModel\Snak\PropertyValueSnak":58:{a:2:{i:0;s:2:"P1";i:1;C:22:"DataValues\StringValue":1:{a}}}' );
92
		// @codingStandardsIgnoreEnd
93
		$this->assertSame( $expected, $hash );
94
	}
95
96
	public function testEquals() {
97
		$snak1 = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
98
		$snak2 = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
99
		$this->assertTrue( $snak1->equals( $snak2 ) );
100
		$this->assertTrue( $snak2->equals( $snak1 ) );
101
	}
102
103
	/**
104
	 * @dataProvider notEqualsProvider
105
	 */
106
	public function testGivenDifferentSnaks_EqualsReturnsFalse( Snak $snak1, Snak $snak2 ) {
107
		$this->assertFalse( $snak1->equals( $snak2 ) );
108
		$this->assertFalse( $snak2->equals( $snak1 ) );
109
	}
110
111
	public function notEqualsProvider() {
112
		return [
113
			[
114
				new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ),
115
				new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) )
116
			],
117
			[
118
				new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ),
119
				new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'b' ) )
120
			],
121
		];
122
	}
123
124
	public function provideDataToSerialize() {
125
		$p2 = new PropertyId( 'P2' );
126
		$p2foo = new PropertyId( 'foo:P2' );
127
		$value = new StringValue( 'b' );
128
129
		return [
130
			'string' => [
131
				'a:2:{i:0;s:2:"P2";i:1;C:22:"DataValues\StringValue":1:{b}}',
132
				new PropertyValueSnak( $p2, $value ),
133
			],
134
			'foreign' => [
135
				'a:2:{i:0;s:6:"foo:P2";i:1;C:22:"DataValues\StringValue":1:{b}}',
136
				new PropertyValueSnak( $p2foo, $value ),
137
			],
138
		];
139
	}
140
141
	/**
142
	 * @dataProvider provideDataToSerialize
143
	 */
144
	public function testSerialize( $expected, Snak $snak ) {
145
		$serialized = $snak->serialize();
146
		$this->assertSame( $expected, $serialized );
147
148
		$snak2 = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
149
		$snak2->unserialize( $serialized );
150
		$this->assertTrue( $snak->equals( $snak2 ), 'round trip' );
151
	}
152
153
	public function provideDataToUnserialize() {
154
		$p2 = new PropertyId( 'P2' );
155
		$p2foo = new PropertyId( 'foo:P2' );
156
		$value = new StringValue( 'b' );
157
158
		return [
159
			'legacy' => [
160
				new PropertyValueSnak( $p2, $value ),
161
				'a:2:{i:0;i:2;i:1;C:22:"DataValues\StringValue":1:{b}}'
162
			],
163
			'current' => [
164
				new PropertyValueSnak( $p2, $value ),
165
				'a:2:{i:0;s:2:"P2";i:1;C:22:"DataValues\StringValue":1:{b}}'
166
			],
167
			'foreign' => [
168
				new PropertyValueSnak( $p2foo, $value ),
169
				'a:2:{i:0;s:6:"foo:P2";i:1;C:22:"DataValues\StringValue":1:{b}}'
170
			],
171
		];
172
	}
173
174
	/**
175
	 * @dataProvider provideDataToUnserialize
176
	 */
177
	public function testUnserialize( $expected, $serialized ) {
178
		$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
179
		$snak->unserialize( $serialized );
180
		$this->assertTrue( $snak->equals( $expected ) );
181
	}
182
183
	public function testGetDataValue() {
184
		$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) );
185
		$dataValue = $snak->getDataValue();
186
		$this->assertInstanceOf( 'DataValues\DataValue', $dataValue );
187
		$this->assertTrue( $dataValue->equals( new StringValue( 'a' ) ) );
188
	}
189
190
}
191