1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
5
|
|
|
* |
6
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bukashk0zzz\LiipImagineSerializationBundle\Tests\EventListener; |
13
|
|
|
|
14
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\UserPictures; |
15
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
16
|
|
|
use JMS\Serializer\DeserializationContext; |
17
|
|
|
use JMS\Serializer\SerializerBuilder; |
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
19
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
20
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
21
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
22
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Signer; |
23
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\User; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* JmsSerializeListenerTest |
27
|
|
|
* |
28
|
|
|
* @author Denis Golubovskiy <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class JmsSerializeListenerTest extends \PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var RequestContext $requestContext Request context |
34
|
|
|
*/ |
35
|
|
|
private $requestContext; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var CacheManager $cacheManager LiipImagineBundle Cache Manager |
39
|
|
|
*/ |
40
|
|
|
private $cacheManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var StorageInterface $storage Vich storage |
44
|
|
|
*/ |
45
|
|
|
private $vichStorage; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var JmsSerializeEventsManager $eventManager JMS Serialize test event manager |
49
|
|
|
*/ |
50
|
|
|
private $eventManager; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var DeserializationContext $context JMS context |
54
|
|
|
*/ |
55
|
|
|
private $context; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string $filePath Image file path |
59
|
|
|
*/ |
60
|
|
|
private $filePath; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function setUp() |
66
|
|
|
{ |
67
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
68
|
|
|
$this->filePath = (new User())->getCoverUrl(); |
69
|
|
|
$this->generateVichStorage(); |
70
|
|
|
$this->context = (new JmsSerializeContextGenerator())->generateContext(); |
71
|
|
|
$this->eventManager = new JmsSerializeEventsManager(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected function tearDown() |
78
|
|
|
{ |
79
|
|
|
$this->requestContext = null; |
80
|
|
|
$this->eventManager = null; |
81
|
|
|
$this->cacheManager = null; |
82
|
|
|
$this->vichStorage = null; |
83
|
|
|
$this->filePath = null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test virtualField serialization |
88
|
|
|
*/ |
89
|
|
|
public function testVirtualFieldSerialization() |
90
|
|
|
{ |
91
|
|
|
$user = new User(); |
92
|
|
|
$this->generateCacheManager(); |
93
|
|
|
$this->generateRequestContext(); |
94
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
95
|
|
|
$serializer = SerializerBuilder::create()->configureListeners(function (EventDispatcher $dispatcher) { |
96
|
|
|
$this->eventManager->addEvents($dispatcher, $this->requestContext, $this->cacheManager, $this->vichStorage); |
97
|
|
|
})->build(); |
98
|
|
|
$result = $serializer->serialize($user, 'json'); |
99
|
|
|
|
100
|
|
|
static::assertJson($result); |
101
|
|
|
$data = json_decode($result, true); |
102
|
|
|
static::assertEquals('http://a/path/to/an/image3.png', $data['imageThumb']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test serialization |
107
|
|
|
*/ |
108
|
|
|
public function testSerialization() |
109
|
|
|
{ |
110
|
|
|
$user = new User(); |
111
|
|
|
$this->generateCacheManager(); |
112
|
|
|
$this->generateRequestContext(); |
113
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
114
|
|
|
$this->eventManager->dispatchEvents($this->context, $user); |
115
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $user->getCoverUrl()); |
116
|
|
|
static::assertEquals('http://a/path/to/an/image2.png', $user->getPhotoName()); |
117
|
|
|
|
118
|
|
|
// Serialize same object second time (check cache) |
119
|
|
|
$this->context = (new JmsSerializeContextGenerator())->generateContext(); |
120
|
|
|
$this->eventManager->dispatchEvents($this->context, $user); |
121
|
|
|
|
122
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $user->getCoverUrl()); |
123
|
|
|
static::assertEquals('http://a/path/to/an/image2.png', $user->getPhotoName()); |
124
|
|
|
static::assertEquals($this->filePath, $user->getImageUrl()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Test serialization of proxy object and field with array of filters |
129
|
|
|
*/ |
130
|
|
|
public function testProxySerialization() |
131
|
|
|
{ |
132
|
|
|
$userPictures = new UserPictures(); |
133
|
|
|
$this->generateCacheManager(); |
134
|
|
|
$this->generateRequestContext(false, true); |
135
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
136
|
|
|
$data = $this->serializeObject($userPictures); |
137
|
|
|
|
138
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $data['cover']['big']); |
139
|
|
|
static::assertEquals('http://a/path/to/an/image2.png', $data['cover']['small']); |
140
|
|
|
static::assertEquals('http://example.com:8000/uploads/photo.jpg', $data['photo']); |
141
|
|
|
static::assertEquals('http://a/path/to/an/image3.png', $data['photoThumb']); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Test serialization with included http host and port in the URI and include original option "true" |
146
|
|
|
*/ |
147
|
|
|
public function testHttpsSerialization() |
148
|
|
|
{ |
149
|
|
|
$userPictures = new UserPictures(); |
150
|
|
|
$this->generateCacheManager(); |
151
|
|
|
$this->generateRequestContext(true, true); |
152
|
|
|
$data = $this->serializeObject($userPictures, [ |
153
|
|
|
'includeHost' => true, |
154
|
|
|
'vichUploaderSerialize' => true, |
155
|
|
|
'includeOriginal' => true, |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photo']); |
159
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $data['cover']['big']); |
160
|
|
|
static::assertEquals('http://a/path/to/an/image2.png', $data['cover']['small']); |
161
|
|
|
static::assertEquals('http://a/path/to/an/image3.png', $data['photoThumb']['thumb_filter']); |
162
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photoThumb']['original']); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param User|UserPictures $user |
167
|
|
|
* @param array $config JMS serializer listner config |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
protected function serializeObject($user, array $config = []) |
171
|
|
|
{ |
172
|
|
|
$serializer = SerializerBuilder::create()->configureListeners(function (EventDispatcher $dispatcher) use ($config) { |
173
|
|
|
$this->eventManager->addEvents($dispatcher, $this->requestContext, $this->cacheManager, $this->vichStorage, $config); |
174
|
|
|
})->build(); |
175
|
|
|
$result = $serializer->serialize($user, 'json'); |
176
|
|
|
|
177
|
|
|
static::assertJson($result); |
178
|
|
|
|
179
|
|
|
return json_decode($result, true); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param bool $https |
184
|
|
|
* @param bool $port |
185
|
|
|
*/ |
186
|
|
|
protected function generateRequestContext($https = false, $port = false) |
187
|
|
|
{ |
188
|
|
|
$this->requestContext = $this->getMockBuilder('Symfony\Component\Routing\RequestContext') |
189
|
|
|
->disableOriginalConstructor() |
190
|
|
|
->getMock(); |
191
|
|
|
|
192
|
|
|
$scheme = $https ? 'https':'http'; |
193
|
|
|
|
194
|
|
|
$this->requestContext->expects(static::any()) |
195
|
|
|
->method('getScheme') |
196
|
|
|
->willReturn($scheme); |
197
|
|
|
|
198
|
|
|
$this->requestContext->expects(static::any()) |
199
|
|
|
->method('getHost') |
200
|
|
|
->willReturn('example.com'); |
201
|
|
|
|
202
|
|
|
if ($port) { |
203
|
|
|
if ($https) { |
204
|
|
|
$this->requestContext->expects(static::any()) |
205
|
|
|
->method('getHttpsPort') |
206
|
|
|
->willReturn(8800); |
207
|
|
|
|
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->requestContext->expects(static::any()) |
212
|
|
|
->method('getHttpPort') |
213
|
|
|
->willReturn(8000); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Prepare mock of Liip cache manager |
219
|
|
|
*/ |
220
|
|
|
protected function generateCacheManager() |
221
|
|
|
{ |
222
|
|
|
$resolver = static::getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface'); |
223
|
|
|
$resolver |
224
|
|
|
->expects(static::any()) |
225
|
|
|
->method('isStored') |
226
|
|
|
->will(static::returnValue(true)) |
227
|
|
|
; |
228
|
|
|
$resolver |
229
|
|
|
->expects(static::any()) |
230
|
|
|
->method('resolve') |
231
|
|
|
->will(static::onConsecutiveCalls('http://a/path/to/an/image1.png', 'http://a/path/to/an/image2.png', 'http://a/path/to/an/image3.png')) |
232
|
|
|
; |
233
|
|
|
|
234
|
|
|
$config = static::getMock('Liip\ImagineBundle\Imagine\Filter\FilterConfiguration'); |
235
|
|
|
$config->expects(static::any()) |
236
|
|
|
->method('get') |
237
|
|
|
->will(static::returnValue(array( |
238
|
|
|
'size' => array(180, 180), |
239
|
|
|
'mode' => 'outbound', |
240
|
|
|
'cache' => null, |
241
|
|
|
))) |
242
|
|
|
; |
243
|
|
|
|
244
|
|
|
$router = static::getMock('Symfony\Component\Routing\RouterInterface'); |
245
|
|
|
$router->expects(static::never()) |
246
|
|
|
->method('generate') |
247
|
|
|
; |
248
|
|
|
|
249
|
|
|
$eventDispatcher = static::getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
250
|
|
|
|
251
|
|
|
/** @noinspection PhpParamsInspection */ |
252
|
|
|
$this->cacheManager = new CacheManager($config, $router, new Signer('secret'), $eventDispatcher); |
253
|
|
|
|
254
|
|
|
/** @noinspection PhpParamsInspection */ |
255
|
|
|
$this->cacheManager->addResolver('default', $resolver); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Generate vichStorage mock |
260
|
|
|
*/ |
261
|
|
|
protected function generateVichStorage() |
262
|
|
|
{ |
263
|
|
|
$this->vichStorage = $this->getMockBuilder('Vich\UploaderBundle\Storage\FileSystemStorage') |
264
|
|
|
->disableOriginalConstructor() |
265
|
|
|
->getMock(); |
266
|
|
|
$this->vichStorage->expects(static::any()) |
267
|
|
|
->method('resolveUri') |
268
|
|
|
->will(static::returnValue('/uploads/photo.jpg')); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|