SnakListSerializerTest::nonSerializableProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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