Passed
Push — release330 ( 71793e...a62254 )
by no
02:46
created

testDeserializeWithDeserializableValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Deserializers\Tests\Phpunit\Deserializers;
4
5
use Deserializers\DispatchingDeserializer;
6
7
/**
8
 * @covers Deserializers\DispatchingDeserializer
9
 *
10
 * @license GPL-2.0+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class DispatchingDeserializerTest extends \PHPUnit_Framework_TestCase {
14
15
	public function testCanConstructWithNoDeserializers() {
16
		new DispatchingDeserializer( [] );
17
		$this->assertTrue( true );
18
	}
19
20
	public function testCannotConstructWithNonDeserializers() {
21
		$this->setExpectedException( 'InvalidArgumentException' );
22
		new DispatchingDeserializer( [ 42, 'foobar' ] );
0 ignored issues
show
Documentation introduced by
array(42, 'foobar') is of type array<integer,integer|st...integer","1":"string"}>, but the function expects a array<integer,object<Des...patchableDeserializer>>.

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...
23
	}
24
25
	public function testCanDeserialize() {
26
		$subDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' );
27
28
		$subDeserializer->expects( $this->exactly( 4 ) )
29
			->method( 'isDeserializerFor' )
30
			->will( $this->returnCallback( function( $value ) {
31
				return $value > 9000;
32
			} ) );
33
34
		$serializer = new DispatchingDeserializer( [ $subDeserializer ] );
35
36
		$this->assertFalse( $serializer->isDeserializerFor( 0 ) );
37
		$this->assertFalse( $serializer->isDeserializerFor( 42 ) );
38
		$this->assertTrue( $serializer->isDeserializerFor( 9001 ) );
39
		$this->assertTrue( $serializer->isDeserializerFor( 31337 ) );
40
	}
41
42
	public function testDeserializeWithDeserializableValues() {
43
		$subDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' );
44
45
		$subDeserializer->expects( $this->any() )
46
			->method( 'isDeserializerFor' )
47
			->will( $this->returnValue( true ) );
48
49
		$subDeserializer->expects( $this->any() )
50
			->method( 'deserialize' )
51
			->will( $this->returnValue( 42 ) );
52
53
		$serializer = new DispatchingDeserializer( [ $subDeserializer ] );
54
55
		$this->assertEquals( 42, $serializer->deserialize( 'foo' ) );
56
		$this->assertEquals( 42, $serializer->deserialize( null ) );
57
	}
58
59
	public function testSerializeWithUnserializableValue() {
60
		$subDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' );
61
62
		$subDeserializer->expects( $this->once() )
63
			->method( 'isDeserializerFor' )
64
			->will( $this->returnValue( false ) );
65
66
		$serializer = new DispatchingDeSerializer( [ $subDeserializer ] );
67
68
		$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' );
69
		$serializer->deserialize( 0 );
70
	}
71
72
	public function testSerializeWithMultipleSubSerializers() {
73
		$subDeserializer0 = $this->getMock( 'Deserializers\DispatchableDeserializer' );
74
75
		$subDeserializer0->expects( $this->any() )
76
			->method( 'isDeserializerFor' )
77
			->will( $this->returnValue( true ) );
78
79
		$subDeserializer0->expects( $this->any() )
80
			->method( 'deserialize' )
81
			->will( $this->returnValue( 42 ) );
82
83
		$subDeserializer1 = $this->getMock( 'Deserializers\DispatchableDeserializer' );
84
85
		$subDeserializer1->expects( $this->any() )
86
			->method( 'isDeserializerFor' )
87
			->will( $this->returnValue( false ) );
88
89
		$subDeserializer2 = clone $subDeserializer1;
90
91
		$serializer = new DispatchingDeserializer( [
92
			$subDeserializer1,
93
			$subDeserializer0,
94
			$subDeserializer2,
95
		] );
96
97
		$this->assertEquals( 42, $serializer->deserialize( 'foo' ) );
98
	}
99
100
	public function testAddSerializer() {
101
		$deserializer = new DispatchingDeserializer( [] );
102
103
		$subDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' );
104
105
		$subDeserializer->expects( $this->any() )
106
			->method( 'isDeserializerFor' )
107
			->will( $this->returnValue( true ) );
108
109
		$subDeserializer->expects( $this->any() )
110
			->method( 'deserialize' )
111
			->will( $this->returnValue( 42 ) );
112
113
		$deserializer->addDeserializer( $subDeserializer );
114
115
		$this->assertEquals(
116
			42,
117
			$deserializer->deserialize( null )
118
		);
119
	}
120
121
}
122