Passed
Push — fixTravisHHVM ( 29be00...81ae42 )
by Marius
07:29 queued 04:42
created

testLegacySerializationWithoutId_legacyIsDetected()   A

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\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() {
24
		$this->deserializer = new EntityDeserializer(
25
			$this->getStubLegacyDeserializer(),
26
			$this->getStubCurrentDeserializer()
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_legacyIsDetected() {
92
		$deserializer = new EntityDeserializer(
93
			$this->getStubLegacyDeserializer(),
94
			$this->getThrowingDeserializer()
95
		);
96
97
		$returnValue = $deserializer->deserialize( $this->getLegacyItemSerializationWithoutId() );
98
99
		$this->assertEquals( 'legacy', $returnValue );
100
	}
101
102
	private function getLegacyItemSerializationWithoutId() {
103
		return array( 'aliases' => array(
104
			'en' => array( 'foo', 'bar' )
105
		) );
106
	}
107
108
	public function testCurrentSerializationWithoutId_currentIsDetected() {
109
		$deserializer = new EntityDeserializer(
110
			$this->getThrowingDeserializer(),
111
			$this->getStubCurrentDeserializer()
112
		);
113
114
		$returnValue = $deserializer->deserialize( $this->getCurrentItemSerializationWithoutId() );
115
116
		$this->assertEquals( 'current', $returnValue );
117
	}
118
119
	private function getCurrentItemSerializationWithoutId() {
120
		return array( 'aliases' => array(
121
			'en' => array(
122
				array(
123
					'language' => 'en',
124
					'value' => 'foo',
125
				),
126
				array(
127
					'language' => 'en',
128
					'value' => 'bar',
129
				),
130
			)
131
		) );
132
	}
133
134
	/**
135
	 * @dataProvider invalidSerializationProvider
136
	 */
137
	public function testGivenInvalidSerialization_exceptionIsThrown( $serialization ) {
138
		$deserializer = new EntityDeserializer(
139
			$this->getThrowingDeserializer(),
140
			$this->getThrowingDeserializer()
141
		);
142
143
		$this->setExpectedException( DeserializationException::class );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
144
		$deserializer->deserialize( $serialization );
145
	}
146
147
	public function invalidSerializationProvider() {
148
		return array(
149
			array( null ),
150
			array( 5 ),
151
			array( array() ),
152
			array( array( 'entity' => 'P42', 'datatype' => null ) ),
153
		);
154
	}
155
156
}
157