ReferenceSerializerTest::serializationProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
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\PropertyId;
7
use Wikibase\DataModel\Reference;
8
use Wikibase\DataModel\Serializers\ReferenceSerializer;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
11
use Wikibase\DataModel\Snak\SnakList;
12
13
/**
14
 * @covers Wikibase\DataModel\Serializers\ReferenceSerializer
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Thomas Pellissier Tanon
18
 */
19
class ReferenceSerializerTest extends DispatchableSerializerTest {
20
21
	protected function buildSerializer() {
22
		$snakListSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
23
		$snakListSerializerMock->expects( $this->any() )
24
			->method( 'serialize' )
25
			->with( $this->equalTo( new SnakList( [] ) ) )
26
			->will( $this->returnValue( [] ) );
27
28
		return new ReferenceSerializer( $snakListSerializerMock );
0 ignored issues
show
Documentation introduced by
$snakListSerializerMock 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...
29
	}
30
31
	public function serializableProvider() {
32
		return [
33
			[
34
				new Reference()
35
			],
36
			[
37
				new Reference( new SnakList( [
38
					new PropertyNoValueSnak( 42 )
39
				] ) )
40
			],
41
		];
42
	}
43
44
	public function nonSerializableProvider() {
45
		return [
46
			[
47
				5
48
			],
49
			[
50
				[]
51
			],
52
			[
53
				new SnakList( [] )
54
			],
55
		];
56
	}
57
58
	public function serializationProvider() {
59
		return [
60
			[
61
				[
62
					'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
63
					'snaks' => [],
64
					'snaks-order' => []
65
				],
66
				new Reference()
67
			],
68
		];
69
	}
70
71
	public function testSnaksOrderSerialization() {
72
		$snakListSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
73
		$snakListSerializerMock->expects( $this->any() )
74
			->method( 'serialize' )
75
			->with( $this->equalTo( new SnakList( [
76
				new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
77
				new PropertySomeValueSnak( new PropertyId( 'P24' ) ),
78
				new PropertyNoValueSnak( new PropertyId( 'P24' ) )
79
			] ) ) )
80
			->will( $this->returnValue( [] ) );
81
82
		$referenceSerializer = new ReferenceSerializer( $snakListSerializerMock );
0 ignored issues
show
Documentation introduced by
$snakListSerializerMock 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...
83
84
		$reference = new Reference( new SnakList( [
85
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
86
			new PropertySomeValueSnak( new PropertyId( 'P24' ) ),
87
			new PropertyNoValueSnak( new PropertyId( 'P24' ) )
88
		] ) );
89
90
		$this->assertEquals(
91
			[
92
				'hash' => $reference->getHash(),
93
				'snaks' => [],
94
				'snaks-order' => [
95
					'P42',
96
					'P24'
97
				]
98
			],
99
			$referenceSerializer->serialize( $reference )
100
		);
101
	}
102
103
}
104