1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\InternalSerialization\Deserializers; |
4
|
|
|
|
5
|
|
|
use DataValues\Deserializers\DataValueDeserializer; |
6
|
|
|
use DataValues\StringValue; |
7
|
|
|
use DataValues\UnDeserializableValue; |
8
|
|
|
use Deserializers\Deserializer; |
9
|
|
|
use Deserializers\Exceptions\DeserializationException; |
10
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
11
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
12
|
|
|
use Wikibase\DataModel\Snak\PropertySomeValueSnak; |
13
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
14
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0-or-later |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class LegacySnakDeserializerTest extends \PHPUnit\Framework\TestCase { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Deserializer |
26
|
|
|
*/ |
27
|
|
|
private $deserializer; |
28
|
|
|
|
29
|
|
|
protected function setUp() : void { |
30
|
|
|
$dataValueDeserializer = $this->createMock( Deserializer::class ); |
31
|
|
|
|
32
|
|
|
$dataValueDeserializer->expects( $this->any() ) |
33
|
|
|
->method( 'deserialize' ) |
34
|
|
|
->with( $this->equalTo( array( 'type' => 'string', 'value' => 'foo' ) ) ) |
35
|
|
|
->will( $this->returnValue( new StringValue( 'foo' ) ) ); |
36
|
|
|
|
37
|
|
|
$this->deserializer = new LegacySnakDeserializer( $dataValueDeserializer ); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function invalidSerializationProvider() { |
41
|
|
|
return array( |
42
|
|
|
array( null ), |
43
|
|
|
array( array() ), |
44
|
|
|
array( array( 'novalue' ) ), |
45
|
|
|
array( array( 1337, 'novalue' ) ), |
46
|
|
|
array( array( 'spam', 1337 ) ), |
47
|
|
|
array( array( 'novalue', 'daah' ) ), |
48
|
|
|
array( array( 'novalue', 0 ) ), |
49
|
|
|
array( array( 'novalue', -1337 ) ), |
50
|
|
|
array( array( 'novalue', 1337, 'spam' ) ), |
51
|
|
|
array( array( 'value', 1337 ) ), |
52
|
|
|
array( array( 'value', 1337, 'data-value-type' ) ), |
53
|
|
|
array( array( 'value', 1337, 'data-value-type', 'data-value-value', 'spam' ) ), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider invalidSerializationProvider |
59
|
|
|
*/ |
60
|
|
|
public function testGivenInvalidSerialization_deserializeThrowsException( $serialization ) { |
61
|
|
|
$this->expectException( DeserializationException::class ); |
62
|
|
|
$this->deserializer->deserialize( $serialization ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testNoValueSnakDeserialization() { |
66
|
|
|
$this->assertEquals( |
67
|
|
|
new PropertyNoValueSnak( 42 ), |
68
|
|
|
$this->deserializer->deserialize( array( |
69
|
|
|
'novalue', |
70
|
|
|
42, |
71
|
|
|
) ) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testSomeValueSnakDeserialization() { |
76
|
|
|
$this->assertEquals( |
77
|
|
|
new PropertySomeValueSnak( 42 ), |
78
|
|
|
$this->deserializer->deserialize( array( |
79
|
|
|
'somevalue', |
80
|
|
|
42, |
81
|
|
|
) ) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testValueSnakDeserialization() { |
86
|
|
|
$this->assertEquals( |
87
|
|
|
new PropertyValueSnak( 42, new StringValue( 'foo' ) ), |
88
|
|
|
$this->deserializer->deserialize( array( |
89
|
|
|
'value', |
90
|
|
|
42, |
91
|
|
|
'string', |
92
|
|
|
'foo' |
93
|
|
|
) ) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGivenInvalidDataValue_unDerializableValueIsConstructed() { |
98
|
|
|
$dataValueDeserializer = new DataValueDeserializer( array( |
99
|
|
|
'string' => StringValue::class |
100
|
|
|
) ); |
101
|
|
|
|
102
|
|
|
$deserializer = new LegacySnakDeserializer( $dataValueDeserializer ); |
103
|
|
|
|
104
|
|
|
$snak = $deserializer->deserialize( array( |
105
|
|
|
'value', |
106
|
|
|
42, |
107
|
|
|
'string', |
108
|
|
|
1337 |
109
|
|
|
) ); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf( PropertyValueSnak::class, $snak ); |
112
|
|
|
$this->assertSnakHasUnDeseriableValue( $snak ); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function assertSnakHasUnDeseriableValue( PropertyValueSnak $snak ) { |
116
|
|
|
$this->assertEquals( new PropertyId( 'P42' ), $snak->getPropertyId() ); |
117
|
|
|
|
118
|
|
|
$dataValue = $snak->getDataValue(); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var UnDeserializableValue $dataValue |
122
|
|
|
*/ |
123
|
|
|
$this->assertInstanceOf( UnDeserializableValue::class, $dataValue ); |
124
|
|
|
|
125
|
|
|
$this->assertEquals( $dataValue->getTargetType(), 'string' ); |
126
|
|
|
$this->assertEquals( $dataValue->getValue(), 1337 ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: