StatementListSerializerTest::buildSerializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
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\StatementListSerializer;
8
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
9
use Wikibase\DataModel\Statement\Statement;
10
use Wikibase\DataModel\Statement\StatementList;
11
12
/**
13
 * @covers Wikibase\DataModel\Serializers\StatementListSerializer
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Bene* < [email protected] >
17
 */
18
class StatementListSerializerTest extends DispatchableSerializerTest {
19
20
	protected function buildSerializer() {
21
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
22
		$statement->setGuid( 'test' );
23
24
		$statementSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
25
		$statementSerializerMock->expects( $this->any() )
26
			->method( 'serialize' )
27
			->with( $this->equalTo( $statement ) )
28
			->will( $this->returnValue( [
29
				'mainsnak' => [
30
					'snaktype' => 'novalue',
31
					'property' => 'P42'
32
				],
33
				'type' => 'statement',
34
				'rank' => 'normal'
35
			] ) );
36
37
		return new StatementListSerializer( $statementSerializerMock, false );
0 ignored issues
show
Documentation introduced by
$statementSerializerMock 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...
38
	}
39
40
	public function serializableProvider() {
41
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
42
		$statement->setGuid( 'test' );
43
44
		return [
45
			[
46
				new StatementList()
47
			],
48
			[
49
				new StatementList( [
50
					$statement
51
				] )
52
			],
53
		];
54
	}
55
56
	public function nonSerializableProvider() {
57
		return [
58
			[
59
				5
60
			],
61
			[
62
				[]
63
			],
64
			[
65
				new Statement( new PropertyNoValueSnak( 42 ) )
66
			],
67
		];
68
	}
69
70
	public function serializationProvider() {
71
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
72
		$statement->setGuid( 'test' );
73
74
		return [
75
			[
76
				[],
77
				new StatementList()
78
			],
79
			[
80
				[
81
					'P42' => [
82
						[
83
							'mainsnak' => [
84
								'snaktype' => 'novalue',
85
								'property' => 'P42'
86
							],
87
							'type' => 'statement',
88
							'rank' => 'normal'
89
						]
90
					]
91
				],
92
				new StatementList( [
93
					$statement
94
				] )
95
			],
96
		];
97
	}
98
99
	public function testStatementListSerializerWithOptionObjectsForMaps() {
100
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
101
		$statement->setGuid( 'test' );
102
		$statementSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
103
		$statementSerializerMock->expects( $this->any() )
104
			->method( 'serialize' )
105
			->with( $this->equalTo( $statement ) )
106
			->will( $this->returnValue( [
107
				'mockedsuff' => [],
108
				'type' => 'statement',
109
			] ) );
110
		$serializer = new StatementListSerializer( $statementSerializerMock, true );
0 ignored issues
show
Documentation introduced by
$statementSerializerMock 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...
111
112
		$statementList = new StatementList( [ $statement ] );
113
114
		$serial = new stdClass();
115
		$serial->P42 = [ [
116
			'mockedsuff' => [],
117
			'type' => 'statement',
118
		] ];
119
		$this->assertEquals( $serial, $serializer->serialize( $statementList ) );
120
	}
121
122
}
123