Passed
Push — master ( 35b0b4...a423cc )
by Marius
01:13
created

unit/Deserializers/EntityDeserializerTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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_exceptionIsThrown() {
92
		$deserializer = new EntityDeserializer(
93
			$this->getStubLegacyDeserializer(),
94
			$this->getThrowingDeserializer()
95
		);
96
97
		$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' );
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...
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(),
110
			$this->getStubCurrentDeserializer()
111
		);
112
113
		$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' );
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...
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(),
138
			$this->getThrowingDeserializer()
139
		);
140
141
		$this->setExpectedException( 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