DispatchingDeserializerTest::testAddSerializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Deserializers\Tests\Phpunit\Deserializers;
4
5
use Deserializers\DispatchableDeserializer;
6
use Deserializers\DispatchingDeserializer;
7
use Deserializers\Exceptions\DeserializationException;
8
use InvalidArgumentException;
9
10
/**
11
 * @covers Deserializers\DispatchingDeserializer
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class DispatchingDeserializerTest extends \PHPUnit\Framework\TestCase {
17
18
	public function testCanConstructWithNoDeserializers() {
19
		new DispatchingDeserializer( [] );
20
		$this->assertTrue( true );
21
	}
22
23
	public function testCannotConstructWithNonDeserializers() {
24
		$this->expectException( InvalidArgumentException::class );
25
		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...
26
	}
27
28
	public function testCanDeserialize() {
29
		$subDeserializer = $this->createMock( DispatchableDeserializer::class );
30
31
		$subDeserializer->expects( $this->exactly( 4 ) )
32
			->method( 'isDeserializerFor' )
33
			->will( $this->returnCallback( function ( $value ) {
34
				return $value > 9000;
35
			} ) );
36
37
		$serializer = new DispatchingDeserializer( [ $subDeserializer ] );
0 ignored issues
show
Documentation introduced by
array($subDeserializer) is of type array<integer,object<PHP...kObject\\MockObject>"}>, 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...
38
39
		$this->assertFalse( $serializer->isDeserializerFor( 0 ) );
40
		$this->assertFalse( $serializer->isDeserializerFor( 42 ) );
41
		$this->assertTrue( $serializer->isDeserializerFor( 9001 ) );
42
		$this->assertTrue( $serializer->isDeserializerFor( 31337 ) );
43
	}
44
45
	public function testDeserializeWithDeserializableValues() {
46
		$subDeserializer = $this->createMock( DispatchableDeserializer::class );
47
48
		$subDeserializer->expects( $this->any() )
49
			->method( 'isDeserializerFor' )
50
			->will( $this->returnValue( true ) );
51
52
		$subDeserializer->expects( $this->any() )
53
			->method( 'deserialize' )
54
			->will( $this->returnValue( 42 ) );
55
56
		$serializer = new DispatchingDeserializer( [ $subDeserializer ] );
0 ignored issues
show
Documentation introduced by
array($subDeserializer) is of type array<integer,object<PHP...kObject\\MockObject>"}>, 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...
57
58
		$this->assertEquals( 42, $serializer->deserialize( 'foo' ) );
59
		$this->assertEquals( 42, $serializer->deserialize( null ) );
60
	}
61
62
	public function testSerializeWithUnserializableValue() {
63
		$subDeserializer = $this->createMock( DispatchableDeserializer::class );
64
65
		$subDeserializer->expects( $this->once() )
66
			->method( 'isDeserializerFor' )
67
			->will( $this->returnValue( false ) );
68
69
		$serializer = new DispatchingDeserializer( [ $subDeserializer ] );
0 ignored issues
show
Documentation introduced by
array($subDeserializer) is of type array<integer,object<PHP...kObject\\MockObject>"}>, 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...
70
71
		$this->expectException( DeserializationException::class );
72
		$serializer->deserialize( 0 );
73
	}
74
75
	public function testSerializeWithMultipleSubSerializers() {
76
		$subDeserializer0 = $this->createMock( DispatchableDeserializer::class );
77
78
		$subDeserializer0->expects( $this->any() )
79
			->method( 'isDeserializerFor' )
80
			->will( $this->returnValue( true ) );
81
82
		$subDeserializer0->expects( $this->any() )
83
			->method( 'deserialize' )
84
			->will( $this->returnValue( 42 ) );
85
86
		$subDeserializer1 = $this->createMock( DispatchableDeserializer::class );
87
88
		$subDeserializer1->expects( $this->any() )
89
			->method( 'isDeserializerFor' )
90
			->will( $this->returnValue( false ) );
91
92
		$subDeserializer2 = clone $subDeserializer1;
93
94
		$serializer = new DispatchingDeserializer( [
0 ignored issues
show
Documentation introduced by
array($subDeserializer1,...er0, $subDeserializer2) is of type array<integer,object<PHP...kObject\\MockObject>"}>, 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...
95
			$subDeserializer1,
96
			$subDeserializer0,
97
			$subDeserializer2,
98
		] );
99
100
		$this->assertEquals( 42, $serializer->deserialize( 'foo' ) );
101
	}
102
103
	public function testAddSerializer() {
104
		$deserializer = new DispatchingDeserializer( [] );
105
106
		$subDeserializer = $this->createMock( DispatchableDeserializer::class );
107
108
		$subDeserializer->expects( $this->any() )
109
			->method( 'isDeserializerFor' )
110
			->will( $this->returnValue( true ) );
111
112
		$subDeserializer->expects( $this->any() )
113
			->method( 'deserialize' )
114
			->will( $this->returnValue( 42 ) );
115
116
		$deserializer->addDeserializer( $subDeserializer );
0 ignored issues
show
Documentation introduced by
$subDeserializer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\DispatchableDeserializer>.

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...
117
118
		$this->assertEquals(
119
			42,
120
			$deserializer->deserialize( null )
121
		);
122
	}
123
124
}
125