LegacyEntityIdDeserializerTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A invalidSerializationProvider() 0 8 1
A testGivenInvalidSerialization_deserializeThrowsException() 0 4 1
A expectDeserializationException() 0 3 1
A testGivenLegacyIdFormat_deserializationIsCorrect() 0 4 1
A legacyIdProvider() 0 13 1
A testGivenNewIdFormat_deserializationIsCorrect() 0 4 1
A newIdProvider() 0 8 1
A testGivenInvalidNewIdFormat_exceptionIsThrown() 0 4 1
A testGivenInvalidLegacyIdFormat_exceptionIsThrown() 0 4 1
A testGivenArrayWithTwoStringKeys_exceptionIsThrown() 0 7 1
A testGivenArrayWithWrongNumericKeys_exceptionIsThrown() 0 7 1
1
<?php
2
3
namespace Tests\Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Wikibase\DataModel\Entity\BasicEntityIdParser;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\InternalSerialization\Deserializers\LegacyEntityIdDeserializer;
12
13
/**
14
 * @covers Wikibase\InternalSerialization\Deserializers\LegacyEntityIdDeserializer
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class LegacyEntityIdDeserializerTest extends \PHPUnit\Framework\TestCase {
20
21
	/**
22
	 * @var Deserializer
23
	 */
24
	private $deserializer;
25
26
	protected function setUp() : void {
27
		$this->deserializer = new LegacyEntityIdDeserializer( new BasicEntityIdParser() );
28
	}
29
30
	public function invalidSerializationProvider() {
31
		return array(
32
			array( null ),
33
			array( 42 ),
34
			array( array() ),
35
			array( array( 'Q42' ) ),
36
		);
37
	}
38
39
	/**
40
	 * @dataProvider invalidSerializationProvider
41
	 */
42
	public function testGivenInvalidSerialization_deserializeThrowsException( $serialization ) {
43
		$this->expectDeserializationException();
44
		$this->deserializer->deserialize( $serialization );
45
	}
46
47
	private function expectDeserializationException() {
48
		$this->expectException( DeserializationException::class );
49
	}
50
51
	/**
52
	 * @dataProvider legacyIdProvider
53
	 */
54
	public function testGivenLegacyIdFormat_deserializationIsCorrect( EntityId $expectedId, array $legacyFormat ) {
55
		$actualId = $this->deserializer->deserialize( $legacyFormat );
56
		$this->assertEquals( $expectedId, $actualId );
57
	}
58
59
	public function legacyIdProvider() {
60
		return array(
61
			array(
62
				new ItemId( 'Q42' ),
63
				array( 'item', 42 )
64
			),
65
66
			array(
67
				new PropertyId( 'P1337' ),
68
				array( 'property', 1337 )
69
			),
70
		);
71
	}
72
73
	/**
74
	 * @dataProvider newIdProvider
75
	 */
76
	public function testGivenNewIdFormat_deserializationIsCorrect( EntityId $expectedId, $idSerialization ) {
77
		$actualId = $this->deserializer->deserialize( $idSerialization );
78
		$this->assertEquals( $expectedId, $actualId );
79
	}
80
81
	public function newIdProvider() {
82
		return array(
83
			array( new ItemId( 'Q1' ), 'Q1' ),
84
			array( new ItemId( 'Q42' ), 'q42' ),
85
			array( new PropertyId( 'P1337' ), 'P1337' ),
86
			array( new PropertyId( 'P23' ), 'p23' ),
87
		);
88
	}
89
90
	public function testGivenInvalidNewIdFormat_exceptionIsThrown() {
91
		$this->expectDeserializationException();
92
		$this->deserializer->deserialize( 'Q42spam' );
93
	}
94
95
	public function testGivenInvalidLegacyIdFormat_exceptionIsThrown() {
96
		$this->expectDeserializationException();
97
		$this->deserializer->deserialize( array( 'item', 'foobar' ) );
98
	}
99
100
	public function testGivenArrayWithTwoStringKeys_exceptionIsThrown() {
101
		$this->expectDeserializationException();
102
		$this->deserializer->deserialize( array(
103
			'foo' => 'item',
104
			'baz' => 42,
105
		) );
106
	}
107
108
	public function testGivenArrayWithWrongNumericKeys_exceptionIsThrown() {
109
		$this->expectDeserializationException();
110
		$this->deserializer->deserialize( array(
111
			42 => 'item',
112
			1337 => 42,
113
		) );
114
	}
115
116
}
117