1
|
|
|
<?php |
2
|
|
|
namespace HtImgModuleTest\Controller; |
3
|
|
|
|
4
|
|
|
use HtImgModule\Controller\ImageController; |
5
|
|
|
use HtImgModule\Exception; |
6
|
|
|
use Zend\Mvc\MvcEvent; |
7
|
|
|
use Phine\Test\Property; |
8
|
|
|
use Zend\Mvc\Controller\PluginManager; |
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
10
|
|
|
|
11
|
|
|
class ImageControllerTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
public function testGet404WhenImageIsNotFound() |
14
|
|
|
{ |
15
|
|
|
$imageService = $this->createMock('HtImgModule\Service\ImageServiceInterface'); |
16
|
|
|
|
17
|
|
|
$controller = new ImageController($imageService); |
18
|
|
|
|
19
|
|
|
$relativePath = 'relative/path/of/image'; |
20
|
|
|
$filter = 'awesome_filter'; |
21
|
|
|
$container = $this->createMock(ServiceLocatorInterface::class); |
22
|
|
|
if (!method_exists(PluginManager::class, 'configure')) { |
23
|
|
|
$pluginManager = new PluginManager(); |
24
|
|
|
} else { |
25
|
|
|
$pluginManager = new PluginManager($container); |
26
|
|
|
} |
27
|
|
|
$controller->setPluginManager($pluginManager); |
28
|
|
|
$paramsPlugin = $this->createMock('Zend\Mvc\Controller\Plugin\Params'); |
29
|
|
|
$pluginManager->setService('params', $paramsPlugin); |
30
|
|
|
|
31
|
|
|
$paramsPlugin->expects($this->exactly(1)) |
32
|
|
|
->method('fromQuery') |
33
|
|
|
->with('relativePath', null) |
34
|
|
|
->will($this->returnValue($relativePath)); |
35
|
|
|
|
36
|
|
|
$paramsPlugin->expects($this->exactly(1)) |
37
|
|
|
->method('fromRoute') |
38
|
|
|
->with('filter', null) |
39
|
|
|
->will($this->returnValue($filter)); |
40
|
|
|
|
41
|
|
|
$imageService->expects($this->once()) |
42
|
|
|
->method('getImage') |
43
|
|
|
->with($relativePath, $filter) |
44
|
|
|
->will($this->throwException(new Exception\ImageNotFoundException())); |
45
|
|
|
$this->expect404($controller); |
46
|
|
|
$controller->displayAction(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGet404WhenFilterIsNotFound() |
50
|
|
|
{ |
51
|
|
|
$imageService = $this->createMock('HtImgModule\Service\ImageServiceInterface'); |
52
|
|
|
|
53
|
|
|
$controller = new ImageController($imageService); |
54
|
|
|
|
55
|
|
|
$relativePath = 'relative/path/of/image'; |
56
|
|
|
$filter = 'awesome_filter'; |
57
|
|
|
|
58
|
|
|
$container = $this->createMock(ServiceLocatorInterface::class); |
59
|
|
|
if (!method_exists(PluginManager::class, 'configure')) { |
60
|
|
|
$pluginManager = new PluginManager(); |
61
|
|
|
} else { |
62
|
|
|
$pluginManager = new PluginManager($container); |
63
|
|
|
} |
64
|
|
|
$controller->setPluginManager($pluginManager); |
65
|
|
|
$paramsPlugin = $this->createMock('Zend\Mvc\Controller\Plugin\Params'); |
66
|
|
|
$pluginManager->setService('params', $paramsPlugin); |
67
|
|
|
|
68
|
|
|
$paramsPlugin->expects($this->exactly(1)) |
69
|
|
|
->method('fromQuery') |
70
|
|
|
->with('relativePath', null) |
71
|
|
|
->will($this->returnValue($relativePath)); |
72
|
|
|
|
73
|
|
|
$paramsPlugin->expects($this->exactly(1)) |
74
|
|
|
->method('fromRoute') |
75
|
|
|
->with('filter', null) |
76
|
|
|
->will($this->returnValue($filter)); |
77
|
|
|
|
78
|
|
|
$imageService->expects($this->once()) |
79
|
|
|
->method('getImage') |
80
|
|
|
->with($relativePath, $filter) |
81
|
|
|
->will($this->throwException(new Exception\FilterNotFoundException())); |
82
|
|
|
$this->expect404($controller); |
83
|
|
|
$controller->displayAction(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testDisplayImage() |
87
|
|
|
{ |
88
|
|
|
$imageService = $this->createMock('HtImgModule\Service\ImageServiceInterface'); |
89
|
|
|
|
90
|
|
|
$controller = new ImageController($imageService); |
91
|
|
|
|
92
|
|
|
$relativePath = 'relative/path/of/image'; |
93
|
|
|
$filter = 'awesome_filter'; |
94
|
|
|
|
95
|
|
|
$container = $this->createMock(ServiceLocatorInterface::class); |
96
|
|
|
if (!method_exists(PluginManager::class, 'configure')) { |
97
|
|
|
$pluginManager = new PluginManager(); |
98
|
|
|
} else { |
99
|
|
|
$pluginManager = new PluginManager($container); |
100
|
|
|
} |
101
|
|
|
$controller->setPluginManager($pluginManager); |
102
|
|
|
$paramsPlugin = $this->createMock('Zend\Mvc\Controller\Plugin\Params'); |
103
|
|
|
$pluginManager->setService('params', $paramsPlugin); |
104
|
|
|
|
105
|
|
|
$paramsPlugin->expects($this->exactly(1)) |
106
|
|
|
->method('fromQuery') |
107
|
|
|
->with('relativePath', null) |
108
|
|
|
->will($this->returnValue($relativePath)); |
109
|
|
|
|
110
|
|
|
$paramsPlugin->expects($this->exactly(1)) |
111
|
|
|
->method('fromRoute') |
112
|
|
|
->with('filter', null) |
113
|
|
|
->will($this->returnValue($filter)); |
114
|
|
|
|
115
|
|
|
$image = $this->createMock('Imagine\Image\ImageInterface'); |
116
|
|
|
$format = 'gif'; |
117
|
|
|
$imageData = ['image' => $image, 'format' => $format, 'imageOutputOptions' => ['quality' => 57]]; |
118
|
|
|
|
119
|
|
|
$imageService->expects($this->once()) |
120
|
|
|
->method('getImage') |
121
|
|
|
->with($relativePath, $filter) |
122
|
|
|
->will($this->returnValue($imageData)); |
123
|
|
|
|
124
|
|
|
$imageModel = $controller->displayAction(); |
125
|
|
|
$this->assertInstanceOf('HtImgModule\View\Model\ImageModel', $imageModel); |
126
|
|
|
$this->assertEquals($image, $imageModel->getImage()); |
127
|
|
|
$this->assertEquals($format, $imageModel->getFormat()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function expect404(ImageController $controller) |
131
|
|
|
{ |
132
|
|
|
$event = new MvcEvent(); |
133
|
|
|
$controller->setEvent($event); |
134
|
|
|
$mockRouteMatchClass = 'Zend\Mvc\Router\RouteMatch'; |
135
|
|
|
if (class_exists(\Zend\Router\RouteMatch::class)) { |
136
|
|
|
$mockRouteMatchClass = \Zend\Router\RouteMatch::class; |
137
|
|
|
} |
138
|
|
|
$routeMatch = $this->getMockBuilder($mockRouteMatchClass) |
139
|
|
|
->disableOriginalConstructor() |
140
|
|
|
->getMock(); |
141
|
|
|
$event->setRouteMatch($routeMatch); |
142
|
|
|
$response = $this->createMock('Zend\Http\Response'); |
143
|
|
|
$response->expects($this->once()) |
144
|
|
|
->method('setStatusCode') |
145
|
|
|
->with(404); |
146
|
|
|
$event->setResponse($response); |
147
|
|
|
Property::set($controller, 'response', $response); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|