invalidSerializationProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Wikibase\DataModel\Reference;
8
use Wikibase\DataModel\ReferenceList;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Snak\SnakList;
11
use Wikibase\DataModel\Statement\Statement;
12
use Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer;
13
use Wikibase\InternalSerialization\Deserializers\LegacySnakListDeserializer;
14
use Wikibase\InternalSerialization\Deserializers\LegacyStatementDeserializer;
15
16
/**
17
 * @covers Wikibase\InternalSerialization\Deserializers\LegacyStatementDeserializer
18
 *
19
 * @license GPL-2.0-or-later
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author Katie Filbert < [email protected] >
22
 */
23
class LegacyStatementDeserializerTest extends \PHPUnit\Framework\TestCase {
24
25
	/**
26
	 * @var Deserializer
27
	 */
28
	private $deserializer;
29
30
	protected function setUp() : void {
31
		$snakDeserializer = new LegacySnakDeserializer( $this->createMock( Deserializer::class ) );
0 ignored issues
show
Documentation introduced by
$this->createMock(\Deser...rs\Deserializer::class) 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...
32
		$qualifiersDeserializer = new LegacySnakListDeserializer( $snakDeserializer );
33
34
		$this->deserializer = new LegacyStatementDeserializer( $snakDeserializer, $qualifiersDeserializer );
35
	}
36
37
	public function invalidSerializationProvider() {
38
		return array(
39
			array( null ),
40
			array( array() ),
41
			array( array( 'm' => array( 'novalue', 42 ) ) ),
42
			array( array( 'm' => array( 'novalue', 42 ), 'q' => array() ) ),
43
			array( array( 'm' => array( 'novalue', 42 ), 'q' => array( null ), 'g' => null ) ),
44
			array( array( 'm' => array( 'novalue', 42 ), 'q' => array(), 'g' => 'kittens' ) ),
45
			array( array(
46
				'm' => array( 'novalue', 42 ),
47
				'q' => array(),
48
				'g' => 9001,
49
				'refs' => array(),
50
				'rank' => Statement::RANK_PREFERRED
51
			) ),
52
			array( array(
53
				'm' => array( 'novalue', 42 ),
54
				'q' => array(),
55
				'g' => null,
56
				'refs' => array(),
57
				'rank' => 'not a rank',
58
			) ),
59
		);
60
	}
61
62
	/**
63
	 * @dataProvider invalidSerializationProvider
64
	 */
65
	public function testGivenInvalidSerialization_deserializeThrowsException( $serialization ) {
66
		$this->expectException( DeserializationException::class );
67
		$this->deserializer->deserialize( $serialization );
68
	}
69
70
	public function testGivenValidSerialization_deserializeReturnsStatement() {
71
		$statement = new Statement(
72
			new PropertyNoValueSnak( 42 )
73
		);
74
75
		$serialization = array(
76
			'm' => array( 'novalue', 42 ),
77
			'q' => array(),
78
			'g' => null,
79
			'rank' => Statement::RANK_NORMAL,
80
			'refs' => array()
81
		);
82
83
		$this->assertEquals(
84
			$statement,
85
			$this->deserializer->deserialize( $serialization )
86
		);
87
	}
88
89
	public function testGivenValidSerialization_deserializeReturnsStatementWithQualifiers() {
90
		$statement = new Statement(
91
			new PropertyNoValueSnak( 42 ),
92
			new SnakList( array(
93
				new PropertyNoValueSnak( 23 ),
94
				new PropertyNoValueSnak( 1337 ),
95
			) )
96
		);
97
98
		$statement->setGuid( 'foo bar baz' );
99
100
		$serialization = array(
101
			'm' => array( 'novalue', 42 ),
102
			'q' => array(
103
				array( 'novalue', 23 ),
104
				array( 'novalue', 1337 )
105
			),
106
			'g' => 'foo bar baz',
107
			'rank' => Statement::RANK_NORMAL,
108
			'refs' => array()
109
		);
110
111
		$this->assertEquals(
112
			$statement,
113
			$this->deserializer->deserialize( $serialization )
114
		);
115
	}
116
117
	public function testGivenValidSerialization_deserializeReturnsStatementWithReferences() {
118
		$statement = new Statement(
119
			new PropertyNoValueSnak( 42 ),
120
			new SnakList( array(
121
				new PropertyNoValueSnak( 23 ),
122
				new PropertyNoValueSnak( 1337 ),
123
			) ),
124
			new ReferenceList( array(
125
				new Reference(
126
					new SnakList( array(
127
						new PropertyNoValueSnak( 1 ),
128
						new PropertyNoValueSnak( 2 ),
129
					) )
130
				)
131
			) )
132
		);
133
134
		$statement->setGuid( 'foo bar baz' );
135
		$statement->setRank( Statement::RANK_PREFERRED );
136
137
		$serialization = array(
138
			'm' => array( 'novalue', 42 ),
139
			'q' => array(
140
				array( 'novalue', 23 ),
141
				array( 'novalue', 1337 )
142
			),
143
			'g' => 'foo bar baz',
144
			'rank' => Statement::RANK_PREFERRED,
145
			'refs' => array(
146
				array(
147
					array( 'novalue', 1 ),
148
					array( 'novalue', 2 )
149
				)
150
			)
151
		);
152
153
		$deserialized = $this->deserializer->deserialize( $serialization );
154
155
		$this->assertEquals(
156
			$statement->getHash(),
157
			$deserialized->getHash()
158
		);
159
	}
160
161
}
162