|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Serializers\Tests\Phpunit\Serializers; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Serializers\DispatchableSerializer; |
|
7
|
|
|
use Serializers\DispatchingSerializer; |
|
8
|
|
|
use Serializers\Exceptions\UnsupportedObjectException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers Serializers\DispatchingSerializer |
|
12
|
|
|
* |
|
13
|
|
|
* @group Serialization |
|
14
|
|
|
* |
|
15
|
|
|
* @license GPL-2.0-or-later |
|
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
17
|
|
|
*/ |
|
18
|
|
|
class DispatchingSerializerTest extends \PHPUnit\Framework\TestCase { |
|
19
|
|
|
|
|
20
|
|
|
public function testConstructWithNoSerializers() { |
|
21
|
|
|
$serializer = new DispatchingSerializer( [] ); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertFalse( $serializer->isSerializerFor( 'foo' ) ); |
|
24
|
|
|
$this->assertFalse( $serializer->isSerializerFor( null ) ); |
|
25
|
|
|
|
|
26
|
|
|
$this->expectException( UnsupportedObjectException::class ); |
|
27
|
|
|
|
|
28
|
|
|
$serializer->serialize( 'foo' ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testConstructWithInvalidArgumentsCausesException() { |
|
32
|
|
|
$this->expectException( InvalidArgumentException::class ); |
|
33
|
|
|
new DispatchingSerializer( [ new \stdClass() ] ); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testCanSerialize() { |
|
37
|
|
|
$subSerializer = $this->createMock( DispatchableSerializer::class ); |
|
38
|
|
|
|
|
39
|
|
|
$subSerializer->expects( $this->exactly( 4 ) ) |
|
40
|
|
|
->method( 'isSerializerFor' ) |
|
41
|
|
|
->will( $this->returnCallback( function ( $value ) { |
|
42
|
|
|
return $value > 9000; |
|
43
|
|
|
} ) ); |
|
44
|
|
|
|
|
45
|
|
|
$serializer = new DispatchingSerializer( [ $subSerializer ] ); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->assertFalse( $serializer->isSerializerFor( 0 ) ); |
|
48
|
|
|
$this->assertFalse( $serializer->isSerializerFor( 42 ) ); |
|
49
|
|
|
$this->assertTrue( $serializer->isSerializerFor( 9001 ) ); |
|
50
|
|
|
$this->assertTrue( $serializer->isSerializerFor( 31337 ) ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testSerializeWithSerializableValues() { |
|
54
|
|
|
$subSerializer = $this->createMock( DispatchableSerializer::class ); |
|
55
|
|
|
|
|
56
|
|
|
$subSerializer->expects( $this->any() ) |
|
57
|
|
|
->method( 'isSerializerFor' ) |
|
58
|
|
|
->will( $this->returnValue( true ) ); |
|
59
|
|
|
|
|
60
|
|
|
$subSerializer->expects( $this->any() ) |
|
61
|
|
|
->method( 'serialize' ) |
|
62
|
|
|
->will( $this->returnValue( 42 ) ); |
|
63
|
|
|
|
|
64
|
|
|
$serializer = new DispatchingSerializer( [ $subSerializer ] ); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals( 42, $serializer->serialize( 'foo' ) ); |
|
67
|
|
|
$this->assertEquals( 42, $serializer->serialize( null ) ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testSerializeWithUnserializableValue() { |
|
71
|
|
|
$subSerializer = $this->createMock( DispatchableSerializer::class ); |
|
72
|
|
|
|
|
73
|
|
|
$subSerializer->expects( $this->once() ) |
|
74
|
|
|
->method( 'isSerializerFor' ) |
|
75
|
|
|
->will( $this->returnValue( false ) ); |
|
76
|
|
|
|
|
77
|
|
|
$serializer = new DispatchingSerializer( [ $subSerializer ] ); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$this->expectException( UnsupportedObjectException::class ); |
|
80
|
|
|
$serializer->serialize( 0 ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testSerializeWithMultipleSubSerializers() { |
|
84
|
|
|
$subSerializer0 = $this->createMock( DispatchableSerializer::class ); |
|
85
|
|
|
|
|
86
|
|
|
$subSerializer0->expects( $this->any() ) |
|
87
|
|
|
->method( 'isSerializerFor' ) |
|
88
|
|
|
->will( $this->returnValue( true ) ); |
|
89
|
|
|
|
|
90
|
|
|
$subSerializer0->expects( $this->any() ) |
|
91
|
|
|
->method( 'serialize' ) |
|
92
|
|
|
->will( $this->returnValue( 42 ) ); |
|
93
|
|
|
|
|
94
|
|
|
$subSerializer1 = $this->createMock( DispatchableSerializer::class ); |
|
95
|
|
|
|
|
96
|
|
|
$subSerializer1->expects( $this->any() ) |
|
97
|
|
|
->method( 'isSerializerFor' ) |
|
98
|
|
|
->will( $this->returnValue( false ) ); |
|
99
|
|
|
|
|
100
|
|
|
$subSerializer2 = clone $subSerializer1; |
|
101
|
|
|
|
|
102
|
|
|
$serializer = new DispatchingSerializer( [ $subSerializer1, $subSerializer0, $subSerializer2 ] ); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$this->assertEquals( 42, $serializer->serialize( 'foo' ) ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testAddSerializer() { |
|
108
|
|
|
$serializer = new DispatchingSerializer( [] ); |
|
109
|
|
|
|
|
110
|
|
|
$subSerializer = $this->createMock( DispatchableSerializer::class ); |
|
111
|
|
|
|
|
112
|
|
|
$subSerializer->expects( $this->any() ) |
|
113
|
|
|
->method( 'isSerializerFor' ) |
|
114
|
|
|
->will( $this->returnValue( true ) ); |
|
115
|
|
|
|
|
116
|
|
|
$subSerializer->expects( $this->any() ) |
|
117
|
|
|
->method( 'serialize' ) |
|
118
|
|
|
->will( $this->returnValue( 42 ) ); |
|
119
|
|
|
|
|
120
|
|
|
$serializer->addSerializer( $subSerializer ); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
$this->assertEquals( |
|
123
|
|
|
42, |
|
124
|
|
|
$serializer->serialize( null ) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
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: