EntityDeserializerTest::getThrowingDeserializer()   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\DispatchableDeserializer;
7
use Deserializers\Exceptions\DeserializationException;
8
use Wikibase\InternalSerialization\Deserializers\EntityDeserializer;
9
10
/**
11
 * @covers Wikibase\InternalSerialization\Deserializers\EntityDeserializer
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class EntityDeserializerTest extends \PHPUnit\Framework\TestCase {
17
18
	/**
19
	 * @var Deserializer
20
	 */
21
	private $deserializer;
22
23
	protected function setUp() : void {
24
		$this->deserializer = new EntityDeserializer(
25
			$this->getStubLegacyDeserializer(),
0 ignored issues
show
Documentation introduced by
$this->getStubLegacyDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
26
			$this->getStubCurrentDeserializer()
0 ignored issues
show
Documentation introduced by
$this->getStubCurrentDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
27
		);
28
	}
29
30
	/**
31
	 * @return DispatchableDeserializer
32
	 */
33
	private function getStubLegacyDeserializer() {
34
		$legacyDeserializer = $this->createMock( DispatchableDeserializer::class );
35
36
		$legacyDeserializer->expects( $this->any() )
37
			->method( 'isDeserializerFor' )
38
			->will( $this->returnCallback( function( $serialization ) {
39
				return array_key_exists( 'entity', $serialization );
40
			} ) );
41
42
		$legacyDeserializer->expects( $this->any() )
43
			->method( 'deserialize' )
44
			->will( $this->returnValue( 'legacy' ) );
45
46
		return $legacyDeserializer;
47
	}
48
49
	/**
50
	 * @return DispatchableDeserializer
51
	 */
52
	private function getStubCurrentDeserializer() {
53
		$currentDeserializer = $this->createMock( DispatchableDeserializer::class );
54
55
		$currentDeserializer->expects( $this->any() )
56
			->method( 'isDeserializerFor' )
57
			->will( $this->returnCallback( function( $serialization ) {
58
				return array_key_exists( 'id', $serialization );
59
			} ) );
60
61
		$currentDeserializer->expects( $this->any() )
62
			->method( 'deserialize' )
63
			->will( $this->returnValue( 'current' ) );
64
65
		return $currentDeserializer;
66
	}
67
68
	public function testGivenLegacySerialization_legacyIsDetected() {
69
		$returnValue = $this->deserializer->deserialize( array( 'entity' => array( 'item', 1 ) ) );
70
		$this->assertEquals( 'legacy', $returnValue );
71
	}
72
73
	public function testGivenCurrentSerialization_currentIsDetected() {
74
		$returnValue = $this->deserializer->deserialize( array( 'id' => 'Q1' ) );
75
		$this->assertEquals( 'current', $returnValue );
76
	}
77
78
	/**
79
	 * @return DispatchableDeserializer
80
	 */
81
	private function getThrowingDeserializer() {
82
		$currentDeserializer = $this->createMock( DispatchableDeserializer::class );
83
84
		$currentDeserializer->expects( $this->any() )
85
			->method( 'deserialize' )
86
			->will( $this->throwException( new DeserializationException() ) );
87
88
		return $currentDeserializer;
89
	}
90
91
	public function testLegacySerializationWithoutId_exceptionIsThrown() {
92
		$deserializer = new EntityDeserializer(
93
			$this->getStubLegacyDeserializer(),
0 ignored issues
show
Documentation introduced by
$this->getStubLegacyDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
94
			$this->getThrowingDeserializer()
0 ignored issues
show
Documentation introduced by
$this->getThrowingDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
95
		);
96
97
		$this->expectException( 'Deserializers\Exceptions\DeserializationException' );
98
		$deserializer->deserialize( $this->getLegacyItemSerializationWithoutId() );
99
	}
100
101
	private function getLegacyItemSerializationWithoutId() {
102
		return array( 'aliases' => array(
103
			'en' => array( 'foo', 'bar' )
104
		) );
105
	}
106
107
	public function testCurrentSerializationWithoutId_exceptionIsThrown() {
108
		$deserializer = new EntityDeserializer(
109
			$this->getThrowingDeserializer(),
0 ignored issues
show
Documentation introduced by
$this->getThrowingDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
110
			$this->getStubCurrentDeserializer()
0 ignored issues
show
Documentation introduced by
$this->getStubCurrentDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
111
		);
112
113
		$this->expectException( 'Deserializers\Exceptions\DeserializationException' );
114
		$deserializer->deserialize( $this->getCurrentItemSerializationWithoutId() );
115
	}
116
117
	private function getCurrentItemSerializationWithoutId() {
118
		return array( 'aliases' => array(
119
			'en' => array(
120
				array(
121
					'language' => 'en',
122
					'value' => 'foo',
123
				),
124
				array(
125
					'language' => 'en',
126
					'value' => 'bar',
127
				),
128
			)
129
		) );
130
	}
131
132
	/**
133
	 * @dataProvider invalidSerializationProvider
134
	 */
135
	public function testGivenInvalidSerialization_exceptionIsThrown( $serialization ) {
136
		$deserializer = new EntityDeserializer(
137
			$this->getThrowingDeserializer(),
0 ignored issues
show
Documentation introduced by
$this->getThrowingDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
138
			$this->getThrowingDeserializer()
0 ignored issues
show
Documentation introduced by
$this->getThrowingDeserializer() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
139
		);
140
141
		$this->expectException( DeserializationException::class );
142
		$deserializer->deserialize( $serialization );
143
	}
144
145
	public function invalidSerializationProvider() {
146
		return array(
147
			array( null ),
148
			array( 5 ),
149
			array( array() ),
150
			array( array( 'entity' => 'P42', 'datatype' => null ) ),
151
		);
152
	}
153
154
}
155