|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tests\MediaMonks\RestApi\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use MediaMonks\RestApi\Serializer\ChainSerializer; |
|
6
|
|
|
use Mockery as m; |
|
7
|
|
|
|
|
8
|
|
|
class ChainSerializerTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function test_supported_formats() |
|
11
|
|
|
{ |
|
12
|
|
|
$serializer = new ChainSerializer(); |
|
13
|
|
|
|
|
14
|
|
|
$jsonSerializer = m::mock('MediaMonks\RestApi\Serializer\JsonSerializer'); |
|
15
|
|
|
$jsonSerializer->shouldReceive('getSupportedFormats')->andReturn(['json']); |
|
16
|
|
|
$jsonSerializer->shouldReceive('getDefaultFormat')->andReturn('json'); |
|
17
|
|
|
|
|
18
|
|
|
$serializer->addSerializer($jsonSerializer); |
|
19
|
|
|
$this->assertEquals(['json'], $serializer->getSupportedFormats()); |
|
20
|
|
|
$this->assertEquals('json', $serializer->getDefaultFormat()); |
|
21
|
|
|
$this->assertTrue($serializer->supportsFormat('json')); |
|
22
|
|
|
$this->assertFalse($serializer->supportsFormat('xml')); |
|
23
|
|
|
|
|
24
|
|
|
$msgpackSerializer = m::mock('MediaMonks\RestApi\Serializer\MsgpackSerializer'); |
|
25
|
|
|
$msgpackSerializer->shouldReceive('getSupportedFormats')->andReturn(['msgpack']); |
|
26
|
|
|
$msgpackSerializer->shouldReceive('getDefaultFormat')->andReturn('msgpack'); |
|
27
|
|
|
|
|
28
|
|
|
$serializer->addSerializer($msgpackSerializer); |
|
29
|
|
|
$this->assertEquals(['json', 'msgpack'], $serializer->getSupportedFormats()); |
|
30
|
|
|
$this->assertEquals('json', $serializer->getDefaultFormat()); |
|
31
|
|
|
$this->assertTrue($serializer->supportsFormat('json')); |
|
32
|
|
|
$this->assertTrue($serializer->supportsFormat('msgpack')); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function test_supported_formats_without_serializer() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setExpectedException('MediaMonks\RestApi\Exception\SerializerException'); |
|
38
|
|
|
|
|
39
|
|
|
$serializer = new ChainSerializer(); |
|
40
|
|
|
$serializer->getSupportedFormats(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_default_format_without_serializer() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setExpectedException('MediaMonks\RestApi\Exception\SerializerException'); |
|
46
|
|
|
|
|
47
|
|
|
$serializer = new ChainSerializer(); |
|
48
|
|
|
$serializer->getDefaultFormat(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function test_serialize_json() |
|
52
|
|
|
{ |
|
53
|
|
|
$serializer = new ChainSerializer(); |
|
54
|
|
|
|
|
55
|
|
|
$jsonSerializer = m::mock('MediaMonks\RestApi\Serializer\JsonSerializer'); |
|
56
|
|
|
$jsonSerializer->shouldReceive('getSupportedFormats')->andReturn(['json']); |
|
57
|
|
|
$jsonSerializer->shouldReceive('getDefaultFormat')->andReturn('json'); |
|
58
|
|
|
$jsonSerializer->shouldReceive('serialize')->andReturn('json_output'); |
|
59
|
|
|
$jsonSerializer->shouldReceive('supportsFormat')->withArgs(['json'])->andReturn(true); |
|
60
|
|
|
$jsonSerializer->shouldReceive('supportsFormat')->withArgs(['msgpack'])->andReturn(false); |
|
61
|
|
|
|
|
62
|
|
|
$serializer->addSerializer($jsonSerializer); |
|
63
|
|
|
|
|
64
|
|
|
$msgpackSerializer = m::mock('MediaMonks\RestApi\Serializer\MsgpackSerializer'); |
|
65
|
|
|
$msgpackSerializer->shouldReceive('getSupportedFormats')->andReturn(['msgpack']); |
|
66
|
|
|
$msgpackSerializer->shouldReceive('getDefaultFormat')->andReturn('msgpack'); |
|
67
|
|
|
$msgpackSerializer->shouldReceive('serialize')->andReturn('msgpack_output'); |
|
68
|
|
|
$msgpackSerializer->shouldReceive('supportsFormat')->withArgs(['json'])->andReturn(false); |
|
69
|
|
|
$msgpackSerializer->shouldReceive('supportsFormat')->withArgs(['msgpack'])->andReturn(true); |
|
70
|
|
|
|
|
71
|
|
|
$serializer->addSerializer($msgpackSerializer); |
|
72
|
|
|
$this->assertEquals('json_output', $serializer->serialize('foo', 'json')); |
|
73
|
|
|
$this->assertEquals('msgpack_output', $serializer->serialize('foo', 'msgpack')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function test_serialize_without_serializer() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->setExpectedException('MediaMonks\RestApi\Exception\SerializerException'); |
|
79
|
|
|
|
|
80
|
|
|
$serializer = new ChainSerializer(); |
|
81
|
|
|
$serializer->serialize('foo', 'json'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function test_serialize_without_serializer_with_unsupported_format() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->setExpectedException('MediaMonks\RestApi\Exception\SerializerException'); |
|
87
|
|
|
|
|
88
|
|
|
$serializer = new ChainSerializer(); |
|
89
|
|
|
|
|
90
|
|
|
$jsonSerializer = m::mock('MediaMonks\RestApi\Serializer\JsonSerializer'); |
|
91
|
|
|
$jsonSerializer->shouldReceive('getSupportedFormats')->andReturn([]); |
|
92
|
|
|
$jsonSerializer->shouldReceive('supportsFormat')->andReturn(false); |
|
93
|
|
|
$serializer->addSerializer($jsonSerializer); |
|
94
|
|
|
|
|
95
|
|
|
$serializer->serialize('foo', 'xml'); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|