LegacySnakDeserializerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 12
dl 0
loc 108
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A invalidSerializationProvider() 0 16 1
A testGivenInvalidSerialization_deserializeThrowsException() 0 4 1
A testNoValueSnakDeserialization() 0 9 1
A testSomeValueSnakDeserialization() 0 9 1
A testValueSnakDeserialization() 0 11 1
A testGivenInvalidDataValue_unDerializableValueIsConstructed() 0 17 1
A assertSnakHasUnDeseriableValue() 0 13 1
1
<?php
2
3
namespace Tests\Wikibase\InternalSerialization\Deserializers;
4
5
use DataValues\Deserializers\DataValueDeserializer;
6
use DataValues\StringValue;
7
use DataValues\UnDeserializableValue;
8
use Deserializers\Deserializer;
9
use Deserializers\Exceptions\DeserializationException;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
12
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
13
use Wikibase\DataModel\Snak\PropertyValueSnak;
14
use Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer;
15
16
/**
17
 * @covers Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer
18
 *
19
 * @license GPL-2.0-or-later
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class LegacySnakDeserializerTest extends \PHPUnit\Framework\TestCase {
23
24
	/**
25
	 * @var Deserializer
26
	 */
27
	private $deserializer;
28
29
	protected function setUp() : void {
30
		$dataValueDeserializer = $this->createMock( Deserializer::class );
31
32
		$dataValueDeserializer->expects( $this->any() )
33
			->method( 'deserialize' )
34
			->with( $this->equalTo( array( 'type' => 'string', 'value' => 'foo' ) ) )
35
			->will( $this->returnValue( new StringValue( 'foo' ) ) );
36
37
		$this->deserializer = new LegacySnakDeserializer( $dataValueDeserializer );
0 ignored issues
show
Documentation introduced by
$dataValueDeserializer 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...
38
	}
39
40
	public function invalidSerializationProvider() {
41
		return array(
42
			array( null ),
43
			array( array() ),
44
			array( array( 'novalue' ) ),
45
			array( array( 1337, 'novalue' ) ),
46
			array( array( 'spam', 1337 ) ),
47
			array( array( 'novalue', 'daah' ) ),
48
			array( array( 'novalue', 0 ) ),
49
			array( array( 'novalue', -1337 ) ),
50
			array( array( 'novalue', 1337, 'spam' ) ),
51
			array( array( 'value', 1337 ) ),
52
			array( array( 'value', 1337, 'data-value-type' ) ),
53
			array( array( 'value', 1337, 'data-value-type', 'data-value-value', 'spam' ) ),
54
		);
55
	}
56
57
	/**
58
	 * @dataProvider invalidSerializationProvider
59
	 */
60
	public function testGivenInvalidSerialization_deserializeThrowsException( $serialization ) {
61
		$this->expectException( DeserializationException::class );
62
		$this->deserializer->deserialize( $serialization );
63
	}
64
65
	public function testNoValueSnakDeserialization() {
66
		$this->assertEquals(
67
			new PropertyNoValueSnak( 42 ),
68
			$this->deserializer->deserialize( array(
69
				'novalue',
70
				42,
71
			) )
72
		);
73
	}
74
75
	public function testSomeValueSnakDeserialization() {
76
		$this->assertEquals(
77
			new PropertySomeValueSnak( 42 ),
78
			$this->deserializer->deserialize( array(
79
				'somevalue',
80
				42,
81
			) )
82
		);
83
	}
84
85
	public function testValueSnakDeserialization() {
86
		$this->assertEquals(
87
			new PropertyValueSnak( 42, new StringValue( 'foo' ) ),
88
			$this->deserializer->deserialize( array(
89
				'value',
90
				42,
91
				'string',
92
				'foo'
93
			) )
94
		);
95
	}
96
97
	public function testGivenInvalidDataValue_unDerializableValueIsConstructed() {
98
		$dataValueDeserializer = new DataValueDeserializer( array(
99
			'string' => StringValue::class
100
		) );
101
102
		$deserializer = new LegacySnakDeserializer( $dataValueDeserializer );
103
104
		$snak = $deserializer->deserialize( array(
105
			'value',
106
			42,
107
			'string',
108
			1337
109
		) );
110
111
		$this->assertInstanceOf( PropertyValueSnak::class, $snak );
112
		$this->assertSnakHasUnDeseriableValue( $snak );
0 ignored issues
show
Compatibility introduced by
$snak of type object<Wikibase\DataModel\Snak\Snak> is not a sub-type of object<Wikibase\DataModel\Snak\PropertyValueSnak>. It seems like you assume a concrete implementation of the interface Wikibase\DataModel\Snak\Snak to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
113
	}
114
115
	private function assertSnakHasUnDeseriableValue( PropertyValueSnak $snak ) {
116
		$this->assertEquals( new PropertyId( 'P42' ), $snak->getPropertyId() );
117
118
		$dataValue = $snak->getDataValue();
119
120
		/**
121
		 * @var UnDeserializableValue $dataValue
122
		 */
123
		$this->assertInstanceOf( UnDeserializableValue::class, $dataValue );
124
125
		$this->assertEquals( $dataValue->getTargetType(), 'string' );
126
		$this->assertEquals( $dataValue->getValue(), 1337 );
127
	}
128
129
}
130