nonDeserializableProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use PHPUnit\Framework\TestCase;
8
use Wikibase\DataModel\Deserializers\SnakListDeserializer;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Snak\SnakList;
11
12
/**
13
 * @covers Wikibase\DataModel\Deserializers\SnakListDeserializer
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Thomas Pellissier Tanon
17
 */
18
class SnakListDeserializerTest extends TestCase {
19
20
	private function buildDeserializer() {
21
		$snakDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
22
23
		$snakDeserializerMock->expects( $this->any() )
24
			->method( 'deserialize' )
25
			->with( $this->equalTo( [
26
					'snaktype' => 'novalue',
27
					'property' => 'P42'
28
			] ) )
29
			->will( $this->returnValue( new PropertyNoValueSnak( 42 ) ) );
30
31
		return new SnakListDeserializer( $snakDeserializerMock );
0 ignored issues
show
Documentation introduced by
$snakDeserializerMock 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
	}
33
34
	/**
35
	 * @dataProvider nonDeserializableProvider
36
	 */
37
	public function testDeserializeThrowsDeserializationException( $nonDeserializable ) {
38
		$deserializer = $this->buildDeserializer();
39
40
		$this->expectException( DeserializationException::class );
41
		$deserializer->deserialize( $nonDeserializable );
42
	}
43
44
	public function nonDeserializableProvider() {
45
		return [
46
			[
47
				42
48
			],
49
			[
50
				[
51
					'id' => 'P10'
52
				]
53
			],
54
			[
55
				[
56
					'snaktype' => '42value'
57
				]
58
			],
59
		];
60
	}
61
62
	/**
63
	 * @dataProvider deserializationProvider
64
	 */
65
	public function testDeserialization( $object, $serialization ) {
66
		$this->assertEquals( $object, $this->buildDeserializer()->deserialize( $serialization ) );
67
	}
68
69
	public function deserializationProvider() {
70
		return [
71
			[
72
				new SnakList(),
73
				[]
74
			],
75
			[
76
				new SnakList( [
77
					new PropertyNoValueSnak( 42 )
78
				] ),
79
				[
80
					'P42' => [
81
						[
82
							'snaktype' => 'novalue',
83
							'property' => 'P42'
84
						]
85
					]
86
				]
87
			],
88
		];
89
	}
90
91
}
92