SiteLinkDeserializerTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 106
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildDeserializer() 0 9 1
A testDeserializeThrowsDeserializationException() 0 6 1
A nonDeserializableProvider() 0 25 1
A testDeserialization() 0 3 1
A deserializationProvider() 0 21 1
A testDeserializeItemIdFilterPropertyId() 0 15 1
A testAssertBadgesIsArray() 0 11 1
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\Exceptions\InvalidAttributeException;
8
use PHPUnit\Framework\TestCase;
9
use Wikibase\DataModel\Deserializers\SiteLinkDeserializer;
10
use Wikibase\DataModel\Entity\ItemId;
11
use Wikibase\DataModel\Entity\PropertyId;
12
use Wikibase\DataModel\SiteLink;
13
14
/**
15
 * @covers Wikibase\DataModel\Deserializers\SiteLinkDeserializer
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Thomas Pellissier Tanon
19
 */
20
class SiteLinkDeserializerTest extends TestCase {
21
22
	private function buildDeserializer() {
23
		$entityIdDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
24
		$entityIdDeserializerMock->expects( $this->any() )
25
			->method( 'deserialize' )
26
			->with( $this->equalTo( 'Q42' ) )
27
			->will( $this->returnValue( new ItemId( 'Q42' ) ) );
28
29
		return new SiteLinkDeserializer( $entityIdDeserializerMock );
0 ignored issues
show
Documentation introduced by
$entityIdDeserializerMock 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...
30
	}
31
32
	/**
33
	 * @dataProvider nonDeserializableProvider
34
	 */
35
	public function testDeserializeThrowsDeserializationException( $nonDeserializable ) {
36
		$deserializer = $this->buildDeserializer();
37
38
		$this->expectException( DeserializationException::class );
39
		$deserializer->deserialize( $nonDeserializable );
40
	}
41
42
	public function nonDeserializableProvider() {
43
		return [
44
			[
45
				42
46
			],
47
			[
48
				[]
49
			],
50
			[
51
				[
52
					'id' => 'P10'
53
				]
54
			],
55
			[
56
				[
57
					'site' => '42value'
58
				]
59
			],
60
			[
61
				[
62
					'title' => '42value'
63
				]
64
			],
65
		];
66
	}
67
68
	/**
69
	 * @dataProvider deserializationProvider
70
	 */
71
	public function testDeserialization( $object, $serialization ) {
72
		$this->assertEquals( $object, $this->buildDeserializer()->deserialize( $serialization ) );
73
	}
74
75
	public function deserializationProvider() {
76
		return [
77
			[
78
				new SiteLink( 'enwiki', 'Nyan Cat' ),
79
				[
80
					'site' => 'enwiki',
81
					'title' => 'Nyan Cat'
82
				]
83
			],
84
			[
85
				new SiteLink( 'enwiki', 'Nyan Cat', [
86
					new ItemId( 'Q42' )
87
				] ),
88
				[
89
					'site' => 'enwiki',
90
					'title' => 'Nyan Cat',
91
					'badges' => [ 'Q42' ]
92
				]
93
			],
94
		];
95
	}
96
97
	public function testDeserializeItemIdFilterPropertyId() {
98
		$entityIdDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
99
		$entityIdDeserializerMock->expects( $this->any() )
100
			->method( 'deserialize' )
101
			->with( $this->equalTo( 'P42' ) )
102
			->will( $this->returnValue( new PropertyId( 'P42' ) ) );
103
		$deserializer = new SiteLinkDeserializer( $entityIdDeserializerMock );
0 ignored issues
show
Documentation introduced by
$entityIdDeserializerMock 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...
104
105
		$this->expectException( InvalidAttributeException::class );
106
		$deserializer->deserialize( [
107
			'site' => 'frwikisource',
108
			'title' => 'Nyan Cat',
109
			'badges' => [ 'P42' ]
110
		] );
111
	}
112
113
	public function testAssertBadgesIsArray() {
114
		$entityIdDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
115
		$deserializer = new SiteLinkDeserializer( $entityIdDeserializerMock );
0 ignored issues
show
Documentation introduced by
$entityIdDeserializerMock 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...
116
117
		$this->expectException( InvalidAttributeException::class );
118
		$deserializer->deserialize( [
119
			'site' => 'frwikisource',
120
			'title' => 'Nyan Cat',
121
			'badges' => 'Q42'
122
		] );
123
	}
124
125
}
126