|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\BadgesControlBundle\Tests\Event; |
|
12
|
|
|
|
|
13
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
14
|
|
|
use JMS\Serializer\EventDispatcher\PreSerializeEvent; |
|
15
|
|
|
use LoginCidadao\APIBundle\Service\VersionService; |
|
16
|
|
|
use LoginCidadao\BadgesControlBundle\Event\SerializationSubscriber; |
|
17
|
|
|
use LoginCidadao\BadgesControlBundle\Handler\BadgesHandler; |
|
18
|
|
|
use LoginCidadao\BadgesControlBundle\Model\Badge; |
|
19
|
|
|
|
|
20
|
|
|
class SerializationSubscriberTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|BadgesHandler |
|
24
|
|
|
*/ |
|
25
|
|
|
private function getBadgesHandler() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->getMockBuilder('LoginCidadao\BadgesControlBundle\Handler\BadgesHandler') |
|
28
|
|
|
->disableOriginalConstructor()->getMock(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|VersionService |
|
33
|
|
|
*/ |
|
34
|
|
|
private function getVersionService() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getMockBuilder('LoginCidadao\APIBundle\Service\VersionService') |
|
37
|
|
|
->disableOriginalConstructor()->getMock(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testGetSubscribedEvents() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertEquals([ |
|
43
|
|
|
[ |
|
44
|
|
|
'event' => 'serializer.pre_serialize', |
|
45
|
|
|
'method' => 'onPreSerialize', |
|
46
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
47
|
|
|
], |
|
48
|
|
|
[ |
|
49
|
|
|
'event' => 'serializer.post_serialize', |
|
50
|
|
|
'method' => 'onPostSerialize', |
|
51
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
52
|
|
|
], |
|
53
|
|
|
], SerializationSubscriber::getSubscribedEvents()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testOnPreSerializePerson() |
|
57
|
|
|
{ |
|
58
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
59
|
|
|
|
|
60
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|PreSerializeEvent $event */ |
|
61
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\PreSerializeEvent') |
|
62
|
|
|
->disableOriginalConstructor()->getMock(); |
|
63
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($person); |
|
64
|
|
|
|
|
65
|
|
|
$handler = $this->getBadgesHandler(); |
|
66
|
|
|
$handler->expects($this->once())->method('evaluate')->with($person); |
|
67
|
|
|
|
|
68
|
|
|
$subscriber = new SerializationSubscriber($handler, $this->getVersionService()); |
|
69
|
|
|
$subscriber->onPreSerialize($event); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testOnPreSerializeNonPerson() |
|
73
|
|
|
{ |
|
74
|
|
|
$object = new \stdClass(); |
|
75
|
|
|
|
|
76
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|PreSerializeEvent $event */ |
|
77
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\PreSerializeEvent') |
|
78
|
|
|
->disableOriginalConstructor()->getMock(); |
|
79
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($object); |
|
80
|
|
|
|
|
81
|
|
|
$handler = $this->getBadgesHandler(); |
|
82
|
|
|
$handler->expects($this->never())->method('evaluate'); |
|
83
|
|
|
|
|
84
|
|
|
$subscriber = new SerializationSubscriber($handler, $this->getVersionService()); |
|
85
|
|
|
$subscriber->onPreSerialize($event); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testOnPostSerializePersonV1() |
|
89
|
|
|
{ |
|
90
|
|
|
$badges = [ |
|
91
|
|
|
new Badge('namespace', 'name', 'data'), |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$badgesSerialized = [ |
|
95
|
|
|
'namespace.name' => 'data', |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
99
|
|
|
$person->expects($this->once())->method('getBadges')->willReturn($badges); |
|
100
|
|
|
|
|
101
|
|
|
$visitor = $this->getMockBuilder('JMS\Serializer\GenericSerializationVisitor') |
|
102
|
|
|
->disableOriginalConstructor()->getMock(); |
|
103
|
|
|
$visitor->expects($this->once())->method('addData')->with('badges', $badgesSerialized); |
|
104
|
|
|
|
|
105
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ObjectEvent $event */ |
|
106
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\ObjectEvent') |
|
107
|
|
|
->disableOriginalConstructor()->getMock(); |
|
108
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($person); |
|
109
|
|
|
$event->expects($this->once())->method('getVisitor')->willReturn($visitor); |
|
110
|
|
|
|
|
111
|
|
|
$versionService = $this->getVersionService(); |
|
112
|
|
|
$versionService->expects($this->once())->method('getVersionFromRequest')->willReturn([1, 0, 0]); |
|
113
|
|
|
$versionService->expects($this->once())->method('getString')->willReturn('1.0.0'); |
|
114
|
|
|
|
|
115
|
|
|
$subscriber = new SerializationSubscriber($this->getBadgesHandler(), $versionService); |
|
116
|
|
|
$subscriber->onPostSerialize($event); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testOnPostSerializePersonV2() |
|
120
|
|
|
{ |
|
121
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
122
|
|
|
|
|
123
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ObjectEvent $event */ |
|
124
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\ObjectEvent') |
|
125
|
|
|
->disableOriginalConstructor()->getMock(); |
|
126
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($person); |
|
127
|
|
|
$event->expects($this->never())->method('getVisitor'); |
|
128
|
|
|
|
|
129
|
|
|
$versionService = $this->getVersionService(); |
|
130
|
|
|
$versionService->expects($this->once())->method('getVersionFromRequest')->willReturn([2, 0, 0]); |
|
131
|
|
|
$versionService->expects($this->once())->method('getString')->willReturn('2.0.0'); |
|
132
|
|
|
|
|
133
|
|
|
$subscriber = new SerializationSubscriber($this->getBadgesHandler(), $versionService); |
|
134
|
|
|
$subscriber->onPostSerialize($event); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testOnPostSerializeNonPerson() |
|
138
|
|
|
{ |
|
139
|
|
|
$object = new \stdClass(); |
|
140
|
|
|
|
|
141
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ObjectEvent $event */ |
|
142
|
|
|
$event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\ObjectEvent') |
|
143
|
|
|
->disableOriginalConstructor()->getMock(); |
|
144
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($object); |
|
145
|
|
|
$event->expects($this->never())->method('getVisitor'); |
|
146
|
|
|
|
|
147
|
|
|
$subscriber = new SerializationSubscriber($this->getBadgesHandler(), $this->getVersionService()); |
|
148
|
|
|
$subscriber->onPostSerialize($event); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|