testQualifiersOrderSerialization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 9.216
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Serializers;
4
5
use Serializers\Serializer;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Reference;
8
use Wikibase\DataModel\ReferenceList;
9
use Wikibase\DataModel\Serializers\StatementSerializer;
10
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
11
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
12
use Wikibase\DataModel\Snak\SnakList;
13
use Wikibase\DataModel\Statement\Statement;
14
15
/**
16
 * @covers Wikibase\DataModel\Serializers\StatementSerializer
17
 *
18
 * @license GPL-2.0-or-later
19
 * @author Thomas Pellissier Tanon
20
 */
21
class StatementSerializerTest extends DispatchableSerializerTest {
22
23
	protected function buildSerializer() {
24
		$snakSerializerFake = $this->getMockBuilder( Serializer::class )->getMock();
25
		$snakSerializerFake->expects( $this->any() )
26
			->method( 'serialize' )
27
			->will( $this->returnValue( [
28
				'snaktype' => 'novalue',
29
				'property' => "P42"
30
			] ) );
31
32
		$snaksSerializerFake = $this->getMockBuilder( Serializer::class )->getMock();
33
		$snaksSerializerFake->expects( $this->any() )
34
			->method( 'serialize' )
35
			->will( $this->returnValue( [
36
				'P42' => [
37
					[
38
						'snaktype' => 'novalue',
39
						'property' => 'P42'
40
					]
41
				]
42
			] ) );
43
44
		$referencesSerializerFake = $this->getMockBuilder( Serializer::class )->getMock();
45
		$referencesSerializerFake->expects( $this->any() )
46
			->method( 'serialize' )
47
			->will( $this->returnValue( [
48
				[
49
					'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
50
					'snaks' => []
51
				]
52
			] ) );
53
54
		return new StatementSerializer(
55
			$snakSerializerFake,
0 ignored issues
show
Documentation introduced by
$snakSerializerFake is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

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...
56
			$snaksSerializerFake,
0 ignored issues
show
Documentation introduced by
$snaksSerializerFake is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

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...
57
			$referencesSerializerFake
0 ignored issues
show
Documentation introduced by
$referencesSerializerFake is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

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...
58
		);
59
	}
60
61
	public function serializableProvider() {
62
		return [
63
			[
64
				new Statement( new PropertyNoValueSnak( 42 ) )
65
			],
66
		];
67
	}
68
69
	public function nonSerializableProvider() {
70
		return [
71
			[
72
				5
73
			],
74
			[
75
				[]
76
			],
77
			[
78
				new ItemId( 'Q42' )
79
			],
80
		];
81
	}
82
83
	public function serializationProvider() {
84
		$serializations = [];
85
86
		$serializations[] = [
87
			[
88
				'mainsnak' => [
89
					'snaktype' => 'novalue',
90
					'property' => 'P42'
91
				],
92
				'type' => 'statement',
93
				'rank' => 'normal'
94
			],
95
			new Statement( new PropertyNoValueSnak( 42 ) )
96
		];
97
98
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
99
		$statement->setGuid( 'q42' );
100
		$serializations[] = [
101
			[
102
				'mainsnak' => [
103
					'snaktype' => 'novalue',
104
					'property' => 'P42'
105
				],
106
				'type' => 'statement',
107
				'id' => 'q42',
108
				'rank' => 'normal'
109
			],
110
			$statement
111
		];
112
113
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
114
		$statement->setRank( Statement::RANK_PREFERRED );
115
		$serializations[] = [
116
			[
117
				'mainsnak' => [
118
					'snaktype' => 'novalue',
119
					'property' => 'P42'
120
				],
121
				'type' => 'statement',
122
				'rank' => 'preferred'
123
			],
124
			$statement
125
		];
126
127
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
128
		$statement->setRank( Statement::RANK_DEPRECATED );
129
		$serializations[] = [
130
			[
131
				'mainsnak' => [
132
					'snaktype' => 'novalue',
133
					'property' => 'P42'
134
				],
135
				'type' => 'statement',
136
				'rank' => 'deprecated'
137
			],
138
			$statement
139
		];
140
141
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
142
		$statement->setQualifiers( new SnakList( [] ) );
143
		$serializations[] = [
144
			[
145
				'mainsnak' => [
146
					'snaktype' => 'novalue',
147
					'property' => "P42"
148
				],
149
				'type' => 'statement',
150
				'rank' => 'normal'
151
			],
152
			$statement
153
		];
154
155
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
156
		$statement->setQualifiers( new SnakList( [
157
			new PropertyNoValueSnak( 42 )
158
		] ) );
159
		$serializations[] = [
160
			[
161
				'mainsnak' => [
162
					'snaktype' => 'novalue',
163
					'property' => "P42"
164
				],
165
				'type' => 'statement',
166
				'qualifiers' => [
167
					'P42' => [
168
						[
169
							'snaktype' => 'novalue',
170
							'property' => 'P42'
171
						]
172
					]
173
				],
174
				'qualifiers-order' => [
175
					'P42'
176
				],
177
				'rank' => 'normal'
178
			],
179
			$statement
180
		];
181
182
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
183
		$statement->setReferences( new ReferenceList( [
184
			new Reference( [ new PropertyNoValueSnak( 1 ) ] )
185
		] ) );
186
		$serializations[] = [
187
			[
188
				'mainsnak' => [
189
					'snaktype' => 'novalue',
190
					'property' => "P42"
191
				],
192
				'type' => 'statement',
193
				'rank' => 'normal',
194
				'references' => [
195
					[
196
						'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
197
						'snaks' => []
198
					]
199
				],
200
			],
201
			$statement
202
		];
203
204
		return $serializations;
205
	}
206
207
	public function testQualifiersOrderSerialization() {
208
		$snakSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
209
		$snakSerializerMock->expects( $this->any() )
210
			->method( 'serialize' )
211
			->will( $this->returnValue( [
212
				'snaktype' => 'novalue',
213
				'property' => 'P42'
214
			] ) );
215
216
		$snaksSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
217
		$snaksSerializerMock->expects( $this->any() )
218
			->method( 'serialize' )
219
			->will( $this->returnValue( [] ) );
220
221
		$referencesSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
222
		$statementSerializer = new StatementSerializer(
223
			$snakSerializerMock,
0 ignored issues
show
Documentation introduced by
$snakSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

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...
224
			$snaksSerializerMock,
0 ignored issues
show
Documentation introduced by
$snaksSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

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...
225
			$referencesSerializerMock
0 ignored issues
show
Documentation introduced by
$referencesSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

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...
226
		);
227
228
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
229
		$statement->setQualifiers( new SnakList( [
230
			new PropertyNoValueSnak( 42 ),
231
			new PropertySomeValueSnak( 24 ),
232
			new PropertyNoValueSnak( 24 )
233
		] ) );
234
		$this->assertEquals(
235
			[
236
				'mainsnak' => [
237
					'snaktype' => 'novalue',
238
					'property' => 'P42'
239
				],
240
				'qualifiers' => [],
241
				'qualifiers-order' => [
242
					'P42',
243
					'P24'
244
				],
245
				'type' => 'statement',
246
				'rank' => 'normal'
247
			],
248
			$statementSerializer->serialize( $statement )
249
		);
250
	}
251
252
}
253