assertReferenceListEquals()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\ReferenceListDeserializer;
9
use Wikibase\DataModel\Reference;
10
use Wikibase\DataModel\ReferenceList;
11
12
/**
13
 * @covers Wikibase\DataModel\Deserializers\ReferenceListDeserializer
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Thomas Pellissier Tanon
17
 */
18
class ReferenceListDeserializerTest extends TestCase {
19
20
	private function buildDeserializer() {
21
		$referenceDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock();
22
23
		$referenceDeserializerMock->expects( $this->any() )
24
			->method( 'deserialize' )
25
			->with( $this->equalTo( [
26
				'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
27
				'snaks' => []
28
			] ) )
29
			->will( $this->returnValue( new Reference() ) );
30
31
		return new ReferenceListDeserializer( $referenceDeserializerMock );
0 ignored issues
show
Documentation introduced by
$referenceDeserializerMock 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
52
	/**
53
	 * @dataProvider deserializationProvider
54
	 */
55
	public function testDeserialization( $object, $serialization ) {
56
		$this->assertReferenceListEquals(
57
			$object,
58
			$this->buildDeserializer()->deserialize( $serialization )
59
		);
60
	}
61
62
	public function deserializationProvider() {
63
		return [
64
			[
65
				new ReferenceList(),
66
				[]
67
			],
68
			[
69
				new ReferenceList( [
70
					new Reference()
71
				] ),
72
				[
73
					[
74
						'hash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
75
						'snaks' => []
76
					]
77
				]
78
			],
79
		];
80
	}
81
82
	public function assertReferenceListEquals( ReferenceList $expected, ReferenceList $actual ) {
83
		$this->assertTrue( $actual->equals( $expected ), 'The two ReferenceList are different' );
84
	}
85
86
}
87