|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\InternalSerialization\Deserializers; |
|
4
|
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
|
6
|
|
|
use Deserializers\Exceptions\DeserializationException; |
|
7
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser; |
|
8
|
|
|
use Wikibase\DataModel\Entity\Property; |
|
9
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
10
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyEntityIdDeserializer; |
|
11
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyFingerprintDeserializer; |
|
12
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyPropertyDeserializer; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @covers Wikibase\InternalSerialization\Deserializers\LegacyPropertyDeserializer |
|
16
|
|
|
* |
|
17
|
|
|
* @license GPL-2.0-or-later |
|
18
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
19
|
|
|
*/ |
|
20
|
|
|
class LegacyPropertyDeserializerTest extends \PHPUnit\Framework\TestCase { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Deserializer |
|
24
|
|
|
*/ |
|
25
|
|
|
private $deserializer; |
|
26
|
|
|
|
|
27
|
|
|
protected function setUp() : void { |
|
28
|
|
|
$this->deserializer = new LegacyPropertyDeserializer( |
|
29
|
|
|
new LegacyEntityIdDeserializer( new BasicEntityIdParser() ), |
|
30
|
|
|
new LegacyFingerprintDeserializer() |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testGivenNonArraySerialization_exceptionIsThrown() { |
|
35
|
|
|
$this->expectDeserializationException(); |
|
36
|
|
|
$this->deserializer->deserialize( null ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function expectDeserializationException() { |
|
40
|
|
|
$this->expectException( DeserializationException::class ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testGivenNoDataType_exceptionIsThrown() { |
|
44
|
|
|
$this->expectDeserializationException(); |
|
45
|
|
|
$this->deserializer->deserialize( array() ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testGivenNonStringDataType_exceptionIsThrown() { |
|
49
|
|
|
$this->expectDeserializationException(); |
|
50
|
|
|
$this->deserializer->deserialize( array( 'datatype' => null ) ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testGivenValidDataType_dataTypeIsSet() { |
|
54
|
|
|
$property = $this->deserializer->deserialize( array( 'datatype' => 'foo' ) ); |
|
55
|
|
|
$this->assertEquals( 'foo', $property->getDataTypeId() ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testGivenInvalidEntityId_exceptionIsThrown() { |
|
59
|
|
|
$this->expectDeserializationException(); |
|
60
|
|
|
$this->deserializer->deserialize( array( |
|
61
|
|
|
'datatype' => 'foo', |
|
62
|
|
|
'entity' => 'spam spam spam' |
|
63
|
|
|
) ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testGivenNonPropertyEntityId_exceptionIsThrown() { |
|
67
|
|
|
$this->expectDeserializationException(); |
|
68
|
|
|
$this->deserializer->deserialize( array( |
|
69
|
|
|
'datatype' => 'foo', |
|
70
|
|
|
'entity' => 'q42' |
|
71
|
|
|
) ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGivenNoPropertyIdId_noPropertyIdIsSet() { |
|
75
|
|
|
$property = $this->deserializer->deserialize( array( 'datatype' => 'foo' ) ); |
|
76
|
|
|
$this->assertNull( $property->getId() ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testGivenValidPropertyIdId_propertyIdIsSet() { |
|
80
|
|
|
$property = $this->deserializer->deserialize( array( |
|
81
|
|
|
'datatype' => 'foo', |
|
82
|
|
|
'entity' => 'p42' |
|
83
|
|
|
) ); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals( new PropertyId( 'p42' ), $property->getId() ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @dataProvider TermListProvider |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testGivenLabels_getLabelsReturnsThem( array $labels ) { |
|
92
|
|
|
/** @var Property $property */ |
|
93
|
|
|
$property = $this->deserializer->deserialize( array( |
|
94
|
|
|
'datatype' => 'foo', |
|
95
|
|
|
'label' => $labels |
|
96
|
|
|
) ); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals( $labels, $property->getFingerprint()->getLabels()->toTextArray() ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function TermListProvider() { |
|
102
|
|
|
return array( |
|
103
|
|
|
array( array() ), |
|
104
|
|
|
|
|
105
|
|
|
array( array( |
|
106
|
|
|
'en' => 'foo', |
|
107
|
|
|
'de' => 'bar', |
|
108
|
|
|
) ), |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testGivenInvalidLabels_exceptionIsThrown() { |
|
113
|
|
|
$this->expectDeserializationException(); |
|
114
|
|
|
$this->deserializer->deserialize( array( 'label' => null ) ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @dataProvider TermListProvider |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testGivenDescriptions_getDescriptionsReturnsThem( array $descriptions ) { |
|
121
|
|
|
/** @var Property $property */ |
|
122
|
|
|
$property = $this->deserializer->deserialize( array( |
|
123
|
|
|
'datatype' => 'foo', |
|
124
|
|
|
'description' => $descriptions |
|
125
|
|
|
) ); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals( $descriptions, $property->getFingerprint()->getDescriptions()->toTextArray() ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testGivenInvalidAliases_exceptionIsThrown() { |
|
131
|
|
|
$this->expectDeserializationException(); |
|
132
|
|
|
$this->deserializer->deserialize( array( 'aliases' => null ) ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @dataProvider aliasesListProvider |
|
137
|
|
|
*/ |
|
138
|
|
|
public function testGivenAliases_getAliasesReturnsThem( array $aliases ) { |
|
139
|
|
|
/** @var Property $property */ |
|
140
|
|
|
$property = $this->deserializer->deserialize( array( |
|
141
|
|
|
'datatype' => 'foo', |
|
142
|
|
|
'aliases' => $aliases |
|
143
|
|
|
) ); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertEquals( $aliases, $property->getFingerprint()->getAliasGroups()->toTextArray() ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function aliasesListProvider() { |
|
149
|
|
|
return array( |
|
150
|
|
|
array( array() ), |
|
151
|
|
|
|
|
152
|
|
|
array( array( |
|
153
|
|
|
'en' => array( 'foo', 'bar' ), |
|
154
|
|
|
'de' => array( 'foo', 'bar', 'baz' ), |
|
155
|
|
|
'nl' => array( 'bah' ), |
|
156
|
|
|
) ), |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|