1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshVichUploaderSerializationBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Genvald <[email protected]> |
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 Fresh\VichUploaderSerializationBundle\Tests\EventListener; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
14
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
15
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
16
|
|
|
use Fresh\VichUploaderSerializationBundle\EventListener\JmsPreSerializeListener; |
17
|
|
|
use Fresh\VichUploaderSerializationBundle\Tests\Fixtures; |
18
|
|
|
use JMS\Serializer\DeserializationContext; |
19
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
20
|
|
|
use JMS\Serializer\EventDispatcher\Events as JmsEvents; |
21
|
|
|
use Monolog\Logger; |
22
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use Symfony\Component\Routing\RequestContext; |
24
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
25
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* JmsPreSerializeListenerTest |
29
|
|
|
* |
30
|
|
|
* @author Artem Genvald <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class JmsPreSerializeListenerTest extends \PHPUnit_Framework_TestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var EventDispatcherInterface $dispatcher Dispatcher |
36
|
|
|
*/ |
37
|
|
|
private $dispatcher; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var StorageInterface $storage Vich storage |
41
|
|
|
*/ |
42
|
|
|
private $storage; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var RequestContext $requestContext Request context |
46
|
|
|
*/ |
47
|
|
|
private $requestContext; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var CachedReader $annotationReader Cached annotation reader |
51
|
|
|
*/ |
52
|
|
|
private $annotationReader; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Logger $logger Logger |
56
|
|
|
*/ |
57
|
|
|
private $logger; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
protected function setUp() |
63
|
|
|
{ |
64
|
|
|
$this->storage = $this->getMockBuilder('Vich\UploaderBundle\Storage\FileSystemStorage') |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMock(); |
67
|
|
|
$this->storage->expects($this->any()) |
68
|
|
|
->method('resolveUri') |
69
|
|
|
->will($this->onConsecutiveCalls('/uploads/photo.jpg', '/uploads/cover.jpg')); |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->requestContext = $this->getMockBuilder('Symfony\Component\Routing\RequestContext') |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->getMock(); |
75
|
|
|
$this->requestContext->expects($this->any())->method('getScheme')->willReturn('http'); |
76
|
|
|
$this->requestContext->expects($this->any())->method('getHost')->willReturn('example.com'); |
77
|
|
|
|
78
|
|
|
$this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
79
|
|
|
|
80
|
|
|
$this->logger = $this->getMockBuilder('Monolog\Logger') |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->getMock(); |
83
|
|
|
$this->logger->expects($this->any())->method('debug'); |
84
|
|
|
|
85
|
|
|
$this->dispatcher = new EventDispatcher(); |
86
|
|
|
$listener = new JmsPreSerializeListener($this->storage, $this->requestContext, $this->annotationReader, $this->logger); |
87
|
|
|
|
88
|
|
|
$this->dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$listener, 'onPreSerialize']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
protected function tearDown() |
95
|
|
|
{ |
96
|
|
|
$this->dispatcher = null; |
97
|
|
|
$this->storage = null; |
98
|
|
|
$this->requestContext = null; |
99
|
|
|
$this->annotationReader = null; |
100
|
|
|
$this->logger = null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test serialization with included host in the URI |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function testSerializationWithIncludedHost() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$user = (new Fixtures\UserA()) |
109
|
|
|
->setPhotoName('photo.jpg') |
110
|
|
|
->setCoverName('cover.jpg'); |
111
|
|
|
|
112
|
|
|
$context = DeserializationContext::create(); |
113
|
|
|
$event = new ObjectEvent($context, $user, []); |
114
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event); |
115
|
|
|
|
116
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
117
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Test serialization without included host in the URI |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function testSerializationWithoutIncludedHost() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$user = (new Fixtures\UserB()) |
126
|
|
|
->setPhotoName('photo.jpg') |
127
|
|
|
->setCoverName('cover.jpg'); |
128
|
|
|
|
129
|
|
|
$context = DeserializationContext::create(); |
130
|
|
|
$event = new ObjectEvent($context, $user, []); |
131
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserB::class, $context->getFormat(), $event); |
132
|
|
|
|
133
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
134
|
|
|
$this->assertEquals('/uploads/cover.jpg', $user->getCoverName()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test serialization without included host in the URI |
139
|
|
|
* |
140
|
|
|
* @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function testExceptionForIncompatibleAnnotations() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$user = (new Fixtures\UserC()) |
145
|
|
|
->setPhotoName('photo.jpg') |
146
|
|
|
->setCoverName('cover.jpg'); |
147
|
|
|
|
148
|
|
|
$context = DeserializationContext::create(); |
149
|
|
|
$event = new ObjectEvent($context, $user, []); |
150
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserC::class, $context->getFormat(), $event); |
151
|
|
|
|
152
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
153
|
|
|
$this->assertEquals('/uploads/cover.jpg', $user->getCoverName()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.