nonDeserializableProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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\Exceptions\DeserializationException;
6
use PHPUnit\Framework\TestCase;
7
use Wikibase\DataModel\Deserializers\EntityIdDeserializer;
8
use Wikibase\DataModel\Entity\EntityIdParser;
9
use Wikibase\DataModel\Entity\EntityIdParsingException;
10
use Wikibase\DataModel\Entity\ItemId;
11
12
/**
13
 * @covers Wikibase\DataModel\Deserializers\EntityIdDeserializer
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Thomas Pellissier Tanon
17
 */
18
class EntityIdDeserializerTest extends TestCase {
19
20
	private function buildDeserializer() {
21
		$entityIdParserMock = $this->getMockBuilder( EntityIdParser::class )->getMock();
22
		$entityIdParserMock->expects( $this->any() )
23
			->method( 'parse' )
24
			->with( $this->equalTo( 'Q42' ) )
25
			->will( $this->returnValue( new ItemId( 'Q42' ) ) );
26
27
		return new EntityIdDeserializer( $entityIdParserMock );
0 ignored issues
show
Documentation introduced by
$entityIdParserMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataModel\Entity\EntityIdParser>.

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...
28
	}
29
30
	/**
31
	 * @dataProvider nonDeserializableProvider
32
	 */
33
	public function testDeserializeThrowsDeserializationException( $nonDeserializable ) {
34
		$deserializer = $this->buildDeserializer();
35
36
		$this->expectException( DeserializationException::class );
37
		$deserializer->deserialize( $nonDeserializable );
38
	}
39
40
	public function nonDeserializableProvider() {
41
		return [
42
			[
43
				42
44
			],
45
			[
46
				[]
47
			],
48
		];
49
	}
50
51
	/**
52
	 * @dataProvider deserializationProvider
53
	 */
54
	public function testDeserialization( $object, $serialization ) {
55
		$deserializer = $this->buildDeserializer();
56
		$this->assertEquals( $object, $deserializer->deserialize( $serialization ) );
57
	}
58
59
	public function deserializationProvider() {
60
		return [
61
			[
62
				new ItemId( 'Q42' ),
63
				'Q42'
64
			],
65
		];
66
	}
67
68
	public function testDeserializeWithEntityIdParsingException() {
69
		$entityIdParserMock = $this->getMockBuilder( EntityIdParser::class )->getMock();
70
		$entityIdParserMock->expects( $this->any() )
71
			->method( 'parse' )
72
			->will( $this->throwException( new EntityIdParsingException() ) );
73
		$entityIdDeserializer = new EntityIdDeserializer( $entityIdParserMock );
0 ignored issues
show
Documentation introduced by
$entityIdParserMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataModel\Entity\EntityIdParser>.

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...
74
75
		$this->expectException( DeserializationException::class );
76
		$entityIdDeserializer->deserialize( 'test' );
77
	}
78
79
}
80