nonDeserializableProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\Exceptions\InvalidAttributeException;
8
use Wikibase\DataModel\Deserializers\ReferenceDeserializer;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\Reference;
11
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
12
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
13
use Wikibase\DataModel\Snak\SnakList;
14
15
/**
16
 * @covers Wikibase\DataModel\Deserializers\ReferenceDeserializer
17
 *
18
 * @license GPL-2.0-or-later
19
 * @author Thomas Pellissier Tanon
20
 */
21
class ReferenceDeserializerTest extends DispatchableDeserializerTest {
22
23
	protected function buildDeserializer() {
24
		$snaksDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
25
		$snaksDeserializerMock->expects( $this->any() )
26
			->method( 'deserialize' )
27
			->with( $this->equalTo( [] ) )
28
			->will( $this->returnValue( new SnakList() ) );
29
30
		return new ReferenceDeserializer( $snaksDeserializerMock );
0 ignored issues
show
Documentation introduced by
$snaksDeserializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
	}
32
33
	public function deserializableProvider() {
34
		return [
35
			[
36
				[
37
					'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
38
					'snaks' => []
39
				]
40
			],
41
		];
42
	}
43
44
	public function nonDeserializableProvider() {
45
		return [
46
			[
47
				42
48
			],
49
			[
50
				[]
51
			],
52
		];
53
	}
54
55
	public function deserializationProvider() {
56
		return [
57
			[
58
				new Reference(),
59
				[
60
					'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
61
					'snaks' => []
62
				]
63
			],
64
			[
65
				new Reference(),
66
				[
67
					'snaks' => []
68
				]
69
			],
70
		];
71
	}
72
73
	public function testSnaksOrderDeserialization() {
74
		$snaksDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
75
		$snaksDeserializerMock->expects( $this->any() )
76
			->method( 'deserialize' )
77
			->with( $this->equalTo( [
78
					'P24' => [
79
						[
80
							'snaktype' => 'novalue',
81
							'property' => 'P24'
82
						]
83
					],
84
					'P42' => [
85
						[
86
							'snaktype' => 'somevalue',
87
							'property' => 'P42'
88
						],
89
						[
90
							'snaktype' => 'novalue',
91
							'property' => 'P42'
92
						]
93
					]
94
				]
95
			) )
96
			->will( $this->returnValue( new SnakList( [
97
				new PropertyNoValueSnak( new PropertyId( 'P24' ) ),
98
				new PropertySomeValueSnak( new PropertyId( 'P42' ) ),
99
				new PropertyNoValueSnak( new PropertyId( 'P42' ) )
100
			] ) ) );
101
102
		$referenceDeserializer = new ReferenceDeserializer( $snaksDeserializerMock );
0 ignored issues
show
Documentation introduced by
$snaksDeserializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104
		$reference = new Reference( new SnakList( [
105
			new PropertySomeValueSnak( new PropertyId( 'P42' ) ),
106
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
107
			new PropertyNoValueSnak( new PropertyId( 'P24' ) )
108
		] ) );
109
110
		$serialization = [
111
			'hash' => '20726a1e99eab73834c0f4a25f3c5c2561993e6e',
112
			'snaks' => [
113
				'P24' => [
114
					[
115
						'snaktype' => 'novalue',
116
						'property' => 'P24'
117
					]
118
				],
119
				'P42' => [
120
					[
121
						'snaktype' => 'somevalue',
122
						'property' => 'P42'
123
					],
124
					[
125
						'snaktype' => 'novalue',
126
						'property' => 'P42'
127
					]
128
				]
129
			],
130
			'snaks-order' => [
131
				'P42',
132
				'P24'
133
			]
134
		];
135
136
		$this->assertTrue( $reference->equals( $referenceDeserializer->deserialize( $serialization ) ) );
137
	}
138
139
	/**
140
	 * @dataProvider invalidDeserializationProvider
141
	 */
142
	public function testInvalidSerialization( $serialization ) {
143
		$this->expectException( DeserializationException::class );
144
		$this->buildDeserializer()->deserialize( $serialization );
145
	}
146
147
	public function invalidDeserializationProvider() {
148
		return [
149
			[
150
				'hash' => 'da',
151
				'snaks' => []
152
			],
153
		];
154
	}
155
156
	public function testGivenInvalidSnaksOrderAttribute_exceptionIsThrown() {
157
		$this->expectException( InvalidAttributeException::class );
158
		$this->buildDeserializer()->deserialize( [
159
			'hash' => 'foo',
160
			'snaks' => [],
161
			'snaks-order' => null
162
		] );
163
	}
164
165
}
166