newStatementSerialization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
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\Entity\BasicEntityIdParser;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Statement\Statement;
11
use Wikibase\InternalSerialization\Deserializers\LegacyEntityIdDeserializer;
12
use Wikibase\InternalSerialization\Deserializers\LegacyFingerprintDeserializer;
13
use Wikibase\InternalSerialization\Deserializers\LegacyItemDeserializer;
14
use Wikibase\InternalSerialization\Deserializers\LegacySiteLinkListDeserializer;
15
use Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer;
16
use Wikibase\InternalSerialization\Deserializers\LegacySnakListDeserializer;
17
use Wikibase\InternalSerialization\Deserializers\LegacyStatementDeserializer;
18
19
/**
20
 * @covers Wikibase\InternalSerialization\Deserializers\LegacyItemDeserializer
21
 *
22
 * @license GPL-2.0-or-later
23
 * @author Jeroen De Dauw < [email protected] >
24
 */
25
class LegacyItemDeserializerTest extends \PHPUnit\Framework\TestCase {
26
27
	/**
28
	 * @var Deserializer
29
	 */
30
	private $deserializer;
31
32
	protected function setUp() : void {
33
		$idDeserializer = new LegacyEntityIdDeserializer( new BasicEntityIdParser() );
34
35
		$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...
36
37
		$statementDeserializer = new LegacyStatementDeserializer(
38
			$snakDeserializer,
39
			new LegacySnakListDeserializer( $snakDeserializer )
40
		);
41
42
		$this->deserializer = new LegacyItemDeserializer(
43
			$idDeserializer,
44
			new LegacySiteLinkListDeserializer(),
45
			$statementDeserializer,
46
			new LegacyFingerprintDeserializer()
47
		);
48
	}
49
50
	public function invalidSerializationProvider() {
51
		return array(
52
			array( null ),
53
54
			array( array(
55
				'links' => array( null )
56
			) ),
57
58
			array( array(
59
				'claims' => null
60
			) ),
61
62
			array( array(
63
				'claims' => array( null )
64
			) ),
65
66
			array( array(
67
				'entity' => 42
68
			) ),
69
		);
70
	}
71
72
	/**
73
	 * @dataProvider invalidSerializationProvider
74
	 */
75
	public function testGivenInvalidSerialization_deserializeThrowsException( $serialization ) {
76
		$this->expectDeserializationException();
77
		$this->deserializer->deserialize( $serialization );
78
	}
79
80
	private function expectDeserializationException() {
81
		$this->expectException( DeserializationException::class );
82
	}
83
84
	public function testGivenEmptyArray_emptyItemIsReturned() {
85
		$this->assertEquals(
86
			new Item(),
87
			$this->deserializer->deserialize( array() )
88
		);
89
	}
90
91
	public function testGivenLinks_itemHasSiteLinks() {
92
		$item = new Item();
93
94
		$item->getSiteLinkList()->addNewSiteLink( 'foo', 'bar' );
95
		$item->getSiteLinkList()->addNewSiteLink( 'baz', 'bah' );
96
97
		$this->assertDeserialization(
98
			array(
99
				'links' => array(
100
					'foo' => 'bar',
101
					'baz' => 'bah',
102
				)
103
			),
104
			$item
105
		);
106
	}
107
108
	private function assertDeserialization( $serialization, Item $expectedItem ) {
109
		$newItem = $this->itemFromSerialization( $serialization );
110
111
		$this->assertTrue(
112
			$expectedItem->equals( $newItem ),
113
			'Deserialized Item should match expected Item'
114
		);
115
	}
116
117
	/**
118
	 * @param string $serialization
119
	 *
120
	 * @return Item
121
	 */
122
	private function itemFromSerialization( $serialization ) {
123
		$item = $this->deserializer->deserialize( $serialization );
124
		$this->assertInstanceOf( Item::class, $item );
125
		return $item;
126
	}
127
128
	public function testGivenStatement_itemHasStatement() {
129
		$item = new Item();
130
		$item->getStatements()->addStatement( $this->newStatement() );
131
132
		$this->assertDeserialization(
133
			array(
134
				'claims' => array(
135
					$this->newStatementSerialization()
136
				)
137
			),
138
			$item
139
		);
140
	}
141
142
	private function newStatement() {
143
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
144
		$statement->setGuid( 'foo' );
145
		return $statement;
146
	}
147
148
	private function newStatementSerialization() {
149
		return array(
150
			'm' => array( 'novalue', 42 ),
151
			'q' => array(),
152
			'g' => 'foo',
153
			'rank' => Statement::RANK_NORMAL,
154
			'refs' => array()
155
		);
156
	}
157
158
	public function testGivenStatementWithLegacyKey_itemHasStatement() {
159
		$item = new Item();
160
		$item->getStatements()->addStatement( $this->newStatement() );
161
162
		$this->assertDeserialization(
163
			array(
164
				'statements' => array(
165
					$this->newStatementSerialization()
166
				)
167
			),
168
			$item
169
		);
170
	}
171
172
	/**
173
	 * @dataProvider TermListProvider
174
	 */
175
	public function testGivenLabels_getLabelsReturnsThem( array $labels ) {
176
		$item = $this->itemFromSerialization( array( 'label' => $labels ) );
0 ignored issues
show
Documentation introduced by
array('label' => $labels) is of type array<string,array,{"label":"array"}>, but the function expects a string.

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...
177
178
		$this->assertEquals( $labels, $item->getFingerprint()->getLabels()->toTextArray() );
179
	}
180
181
	public function TermListProvider() {
182
		return array(
183
			array( array() ),
184
185
			array( array(
186
				'en' => 'foo',
187
				'de' => 'bar',
188
			) ),
189
		);
190
	}
191
192
	public function testGivenInvalidLabels_exceptionIsThrown() {
193
		$this->expectDeserializationException();
194
		$this->deserializer->deserialize( array( 'label' => null ) );
195
	}
196
197
	/**
198
	 * @dataProvider TermListProvider
199
	 */
200
	public function testGivenDescriptions_getDescriptionsReturnsThem( array $descriptions ) {
201
		$item = $this->itemFromSerialization( array( 'description' => $descriptions ) );
0 ignored issues
show
Documentation introduced by
array('description' => $descriptions) is of type array<string,array,{"description":"array"}>, but the function expects a string.

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...
202
203
		$this->assertEquals( $descriptions, $item->getFingerprint()->getDescriptions()->toTextArray() );
204
	}
205
206
	public function testGivenInvalidAliases_exceptionIsThrown() {
207
		$this->expectDeserializationException();
208
		$this->deserializer->deserialize( array( 'aliases' => null ) );
209
	}
210
211
	/**
212
	 * @dataProvider aliasesListProvider
213
	 */
214
	public function testGivenAliases_getAliasesReturnsThem( array $aliases ) {
215
		$item = $this->itemFromSerialization( array( 'aliases' => $aliases ) );
0 ignored issues
show
Documentation introduced by
array('aliases' => $aliases) is of type array<string,array,{"aliases":"array"}>, but the function expects a string.

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...
216
217
		$this->assertEquals( $aliases, $item->getFingerprint()->getAliasGroups()->toTextArray() );
218
	}
219
220
	public function aliasesListProvider() {
221
		return array(
222
			array( array() ),
223
224
			array( array(
225
				'en' => array( 'foo', 'bar' ),
226
				'de' => array( 'foo', 'bar', 'baz' ),
227
				'nl' => array( 'bah' ),
228
			) ),
229
		);
230
	}
231
232
}
233