UploadFileControllerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 1
cbo 5
dl 0
loc 241
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 29 1
A testUploadActionAddsErrorMessageToFlashMessengerByDefault() 0 21 1
B testUploadActionAddsErrorMessageToFlashMessengerWhenFormIsNotValid() 0 35 1
B testUploadActionAddsUploadErrorsToFlashMessenger() 0 40 1
B testUploadActionAddsSuccessMessageToFlashMessengerWhenFormIsValid() 0 35 1
B testUploadActionCreatesThumbnail() 0 40 1
1
<?php
2
3
namespace DmFilemanTest\Controller;
4
5
use DmFileman\Controller\UploadFileController;
6
use DmTest\Controller\Plugin\MockFactory as PluginMockFactory;
7
8
class UploadFileControllerTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var UploadFileController */
11
    protected $sut;
12
13
    /** @var \PHPUnit_Framework_MockObject_MockObject */
14
    protected $fileManagerMock;
15
16
    /** @var \PHPUnit_Framework_MockObject_MockObject */
17
    protected $uploadFileFormMock;
18
19
    /** @var \PHPUnit_Framework_MockObject_MockObject */
20
    protected $thumbnailerMock;
21
22
    /** @var \PHPUnit_Framework_MockObject_MockObject */
23
    protected $userTextMock;
24
25
    /** @var PluginMockFactory */
26
    protected $mockFactory;
27
28
    public function setUp()
29
    {
30
        $this->fileManagerMock = $this->getMockBuilder('DmFileman\Service\FileManager\FileManager')
31
            ->setMethods(['setCurrentPath', 'getOrigDir', 'getList'])
32
            ->disableOriginalConstructor()
33
            ->getMock();
34
35
        $this->uploadFileFormMock = $this->getMockBuilder('DmFileman\Form\UploadFileForm')
36
            ->setMethods(['build', 'getMessages', 'getInputFilter', 'isValid', 'getData'])
37
            ->getMock();
38
39
        $this->thumbnailerMock = $this->getMockBuilder('DmFileman\Service\Thumbnailer\Thumbnailer')
40
            ->setMethods(['resizeOrigImage'])
41
            ->disableOriginalConstructor()
42
            ->getMock();
43
44
        $this->userTextMock = $this->getMockBuilder('DmCommon\View\Helper\UserText')
45
            ->setMethods(['getMessage'])
46
            ->getMock();
47
48
        $this->sut = new UploadFileController(
49
            $this->fileManagerMock,
50
            $this->uploadFileFormMock,
51
            $this->thumbnailerMock,
52
            $this->userTextMock
53
        );
54
55
        $this->mockFactory = new PluginMockFactory($this);
56
    }
57
58
    /**
59
     * @covers DmFileman\Controller\UploadFileController
60
     */
61
    public function testUploadActionAddsErrorMessageToFlashMessengerByDefault()
62
    {
63
        $responseMock = $this->mockFactory->getResponseMock();
64
65
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 1);
66
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
67
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
68
69
        $this->sut->setPluginManager($pluginMock);
70
71
        $this->uploadFileFormMock
72
            ->expects($this->once())
73
            ->method('getMessages')
74
            ->will($this->returnValue([]));
75
76
        $this->sut->setCurrentPath('');
77
78
        $actualResult = $this->sut->uploadAction();
79
80
        $this->assertEquals($responseMock, $actualResult);
81
    }
82
83
    /**
84
     * @covers DmFileman\Controller\UploadFileController
85
     */
86
    public function testUploadActionAddsErrorMessageToFlashMessengerWhenFormIsNotValid()
87
    {
88
        $responseMock = $this->mockFactory->getResponseMock();
89
90
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 1);
91
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
92
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
93
94
        $this->sut->setPluginManager($pluginMock);
95
96
        $requestMock = $this->mockFactory->getRequestMock(new \SplFixedArray(0), new \SplFixedArray(0));
0 ignored issues
show
Documentation introduced by
new \SplFixedArray(0) is of type object<SplFixedArray>, but the function expects a array|object<PHPUnit_Fra...Object_MockObject>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
        $this->sut->setRequest($requestMock);
98
99
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init', 'setCurrentDir']);
100
        $inputFilterMock->expects($this->any())->method('setCurrentDir')->will($this->returnSelf());
101
102
        $this->uploadFileFormMock
103
            ->expects($this->once())
104
            ->method('getInputFilter')
105
            ->will($this->returnValue($inputFilterMock));
106
        $this->uploadFileFormMock
107
            ->expects($this->once())
108
            ->method('isValid')
109
            ->will($this->returnValue(false));
110
        $this->uploadFileFormMock
111
            ->expects($this->once())
112
            ->method('getMessages')
113
            ->will($this->returnValue([]));
114
115
        $this->sut->setCurrentPath('');
116
117
        $actualResult = $this->sut->uploadAction();
118
119
        $this->assertEquals($responseMock, $actualResult);
120
    }
121
122
    /**
123
     * @covers DmFileman\Controller\UploadFileController
124
     */
125
    public function testUploadActionAddsUploadErrorsToFlashMessenger()
126
    {
127
        $responseMock = $this->mockFactory->getResponseMock();
128
129
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 3);
130
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
131
        $pluginMock         = $this->mockFactory->getPluginMock(
132
            $flashMessengerMock,
133
            $flashMessengerMock,
134
            $flashMessengerMock,
135
            $redirectMock
136
        );
137
138
        $this->sut->setPluginManager($pluginMock);
139
140
        $requestMock = $this->mockFactory->getRequestMock(new \SplFixedArray(0), new \SplFixedArray(0));
0 ignored issues
show
Documentation introduced by
new \SplFixedArray(0) is of type object<SplFixedArray>, but the function expects a array|object<PHPUnit_Fra...Object_MockObject>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
        $this->sut->setRequest($requestMock);
142
143
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init', 'setCurrentDir']);
144
        $inputFilterMock->expects($this->any())->method('setCurrentDir')->will($this->returnSelf());
145
146
        $this->uploadFileFormMock
147
            ->expects($this->once())
148
            ->method('getInputFilter')
149
            ->will($this->returnValue($inputFilterMock));
150
        $this->uploadFileFormMock
151
            ->expects($this->once())
152
            ->method('isValid')
153
            ->will($this->returnValue(false));
154
        $this->uploadFileFormMock
155
            ->expects($this->once())
156
            ->method('getMessages')
157
            ->will($this->returnValue([['foo'], ['bar']]));
158
159
        $this->sut->setCurrentPath('');
160
161
        $actualResult = $this->sut->uploadAction();
162
163
        $this->assertEquals($responseMock, $actualResult);
164
    }
165
166
    /**
167
     * @covers DmFileman\Controller\UploadFileController
168
     */
169
    public function testUploadActionAddsSuccessMessageToFlashMessengerWhenFormIsValid()
170
    {
171
        $responseMock = $this->mockFactory->getResponseMock();
172
173
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(1, 0);
174
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
175
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
176
177
        $this->sut->setPluginManager($pluginMock);
178
179
        $requestMock = $this->mockFactory->getRequestMock(new \SplFixedArray(0), new \SplFixedArray(0));
0 ignored issues
show
Documentation introduced by
new \SplFixedArray(0) is of type object<SplFixedArray>, but the function expects a array|object<PHPUnit_Fra...Object_MockObject>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
        $this->sut->setRequest($requestMock);
181
182
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init', 'setCurrentDir']);
183
        $inputFilterMock->expects($this->any())->method('setCurrentDir')->will($this->returnSelf());
184
185
        $this->uploadFileFormMock
186
            ->expects($this->once())
187
            ->method('getInputFilter')
188
            ->will($this->returnValue($inputFilterMock));
189
        $this->uploadFileFormMock
190
            ->expects($this->once())
191
            ->method('isValid')
192
            ->will($this->returnValue(true));
193
        $this->uploadFileFormMock
194
            ->expects($this->once())
195
            ->method('getData')
196
            ->will($this->returnValue(['file' => ['type' => '']]));
197
198
        $this->sut->setCurrentPath('');
199
200
        $actualResult = $this->sut->uploadAction();
201
202
        $this->assertEquals($responseMock, $actualResult);
203
    }
204
205
    /**
206
     * @covers DmFileman\Controller\UploadFileController
207
     */
208
    public function testUploadActionCreatesThumbnail()
209
    {
210
        $responseMock = $this->mockFactory->getResponseMock();
211
212
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(1, 0);
213
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
214
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
215
216
        $this->sut->setPluginManager($pluginMock);
217
218
        $requestMock = $this->mockFactory->getRequestMock(new \SplFixedArray(0), new \SplFixedArray(0));
0 ignored issues
show
Documentation introduced by
new \SplFixedArray(0) is of type object<SplFixedArray>, but the function expects a array|object<PHPUnit_Fra...Object_MockObject>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
219
        $this->sut->setRequest($requestMock);
220
221
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init', 'setCurrentDir']);
222
        $inputFilterMock->expects($this->any())->method('setCurrentDir')->will($this->returnSelf());
223
224
        $this->uploadFileFormMock
225
            ->expects($this->once())
226
            ->method('getInputFilter')
227
            ->will($this->returnValue($inputFilterMock));
228
        $this->uploadFileFormMock
229
            ->expects($this->once())
230
            ->method('isValid')
231
            ->will($this->returnValue(true));
232
        $this->uploadFileFormMock
233
            ->expects($this->once())
234
            ->method('getData')
235
            ->will($this->returnValue(['file' => ['type' => 'image/jpeg', 'tmp_name' => '']]));
236
237
        $this->thumbnailerMock
238
            ->expects($this->once())
239
            ->method('resizeOrigImage')
240
            ->will($this->returnValue(true));
241
242
        $this->sut->setCurrentPath('');
243
244
        $actualResult = $this->sut->uploadAction();
245
246
        $this->assertEquals($responseMock, $actualResult);
247
    }
248
}
249