|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Serializer\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Tests\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractEventSubscriberTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
public function testOnPostSerialize() |
|
10
|
|
|
{ |
|
11
|
|
|
$embeddeds = array( |
|
12
|
|
|
$this->prophesize('Hateoas\Model\Embedded')->reveal(), |
|
13
|
|
|
); |
|
14
|
|
|
$links = array( |
|
15
|
|
|
$this->prophesize('Hateoas\Model\Link')->reveal(), |
|
16
|
|
|
); |
|
17
|
|
|
$object = new \StdClass(); |
|
18
|
|
|
$context = $this->prophesize('JMS\Serializer\SerializationContext')->reveal(); |
|
19
|
|
|
|
|
20
|
|
|
$serializationVisitor = $this->mockSerializationVisitor(); |
|
21
|
|
|
|
|
22
|
|
|
$serializerProphecy = $this->prophesizeSerializer(); |
|
23
|
|
|
$serializerProphecy |
|
24
|
|
|
->serializeEmbeddeds($embeddeds, $serializationVisitor, $context) |
|
25
|
|
|
->shouldBeCalledTimes(1) |
|
26
|
|
|
; |
|
27
|
|
|
$serializerProphecy |
|
28
|
|
|
->serializeLinks($links, $serializationVisitor, $context) |
|
29
|
|
|
->shouldBeCalledTimes(1) |
|
30
|
|
|
; |
|
31
|
|
|
|
|
32
|
|
|
$linksFactoryProphecy = $this->prophesize('Hateoas\Factory\LinksFactory'); |
|
33
|
|
|
$linksFactoryProphecy |
|
34
|
|
|
->create($object, $context) |
|
35
|
|
|
->willReturn($links) |
|
36
|
|
|
->shouldBeCalledTimes(1) |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
$embeddedsFactoryProphecy = $this->prophesize('Hateoas\Factory\EmbeddedsFactory'); |
|
40
|
|
|
$embeddedsFactoryProphecy |
|
41
|
|
|
->create($object, $context) |
|
42
|
|
|
->willReturn($embeddeds) |
|
43
|
|
|
->shouldBeCalledTimes(1) |
|
44
|
|
|
; |
|
45
|
|
|
|
|
46
|
|
|
$eventProphecy = $this->mockEvent($object, $serializationVisitor, $context); |
|
47
|
|
|
|
|
48
|
|
|
$embeddedEventSubscriber = $this->createEventSubscriber( |
|
49
|
|
|
$serializerProphecy->reveal(), |
|
50
|
|
|
$linksFactoryProphecy->reveal(), |
|
51
|
|
|
$embeddedsFactoryProphecy->reveal() |
|
52
|
|
|
); |
|
53
|
|
|
$embeddedEventSubscriber->onPostSerialize($eventProphecy->reveal()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testOnPostSerializeWithNoLinksEmbeddeds() |
|
57
|
|
|
{ |
|
58
|
|
|
$embeddeds = array(); |
|
59
|
|
|
$links = array(); |
|
60
|
|
|
$object = new \StdClass(); |
|
61
|
|
|
$context = $this->prophesize('JMS\Serializer\SerializationContext')->reveal(); |
|
62
|
|
|
|
|
63
|
|
|
$serializationVisitor = $this->mockSerializationVisitor(); |
|
64
|
|
|
|
|
65
|
|
|
$serializerProphecy = $this->prophesizeSerializer(); |
|
66
|
|
|
$serializerProphecy |
|
67
|
|
|
->serializeEmbeddeds($embeddeds, $serializationVisitor, $context) |
|
68
|
|
|
->shouldNotBeCalled() |
|
69
|
|
|
; |
|
70
|
|
|
$serializerProphecy |
|
71
|
|
|
->serializeLinks($links, $serializationVisitor) |
|
72
|
|
|
->shouldNotBeCalled() |
|
73
|
|
|
; |
|
74
|
|
|
|
|
75
|
|
|
$linksFactoryProphecy = $this->prophesize('Hateoas\Factory\LinksFactory'); |
|
76
|
|
|
$linksFactoryProphecy |
|
77
|
|
|
->create($object, $context) |
|
78
|
|
|
->willReturn($links) |
|
79
|
|
|
->shouldBeCalledTimes(1) |
|
80
|
|
|
; |
|
81
|
|
|
|
|
82
|
|
|
$embeddedsFactoryProphecy = $this->prophesize('Hateoas\Factory\EmbeddedsFactory'); |
|
83
|
|
|
$embeddedsFactoryProphecy |
|
84
|
|
|
->create($object, $context) |
|
85
|
|
|
->willReturn($embeddeds) |
|
86
|
|
|
->shouldBeCalledTimes(1) |
|
87
|
|
|
; |
|
88
|
|
|
|
|
89
|
|
|
$eventProphecy = $this->mockEvent($object, $serializationVisitor, $context); |
|
90
|
|
|
|
|
91
|
|
|
$embeddedEventSubscriber = $this->createEventSubscriber( |
|
92
|
|
|
$serializerProphecy->reveal(), |
|
93
|
|
|
$linksFactoryProphecy->reveal(), |
|
94
|
|
|
$embeddedsFactoryProphecy->reveal() |
|
95
|
|
|
); |
|
96
|
|
|
$embeddedEventSubscriber->onPostSerialize($eventProphecy->reveal()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
abstract protected function createEventSubscriber($serializer, $linksFactory, $embeddedsFactory); |
|
100
|
|
|
|
|
101
|
|
|
abstract protected function prophesizeSerializer(); |
|
102
|
|
|
|
|
103
|
|
|
abstract protected function mockSerializationVisitor(); |
|
104
|
|
|
|
|
105
|
|
|
private function mockEvent($object, $serializationVisitor, $context) |
|
106
|
|
|
{ |
|
107
|
|
|
$eventProphecy = $this->prophesize('JMS\Serializer\EventDispatcher\ObjectEvent'); |
|
108
|
|
|
$eventProphecy->getObject()->willreturn($object); |
|
109
|
|
|
$eventProphecy->getVisitor()->willreturn($serializationVisitor); |
|
110
|
|
|
$eventProphecy->getContext()->willreturn($context); |
|
111
|
|
|
|
|
112
|
|
|
return $eventProphecy; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|