testDeleteActionAddsErrorMessageToFlashMessengerWhenFormIsNotValid()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace DmFilemanTest\Controller;
4
5
use DmFileman\Controller\DeleteFileController;
6
use DmTest\Controller\Plugin\MockFactory as PluginMockFactory;
7
8
class DeleteFileControllerTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var DeleteFileController */
11
    protected $sut;
12
13
    /** @var \PHPUnit_Framework_MockObject_MockObject */
14
    protected $fileManagerMock;
15
16
    /** @var \PHPUnit_Framework_MockObject_MockObject */
17
    protected $deleteFileFormMock;
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
    /**
29
     * @covers DmFileman\Controller\DeleteFileController
30
     */
31
    public function setUp()
32
    {
33
        $this->fileManagerMock = $this->getMockBuilder('DmFileman\Service\FileManager\FileManager')
34
            ->setMethods(['setCurrentPath', 'delete'])
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
38
        $this->deleteFileFormMock = $this->getMockBuilder('DmFileman\Form\DeleteFileForm')
39
            ->setMethods(['build', 'getInputFilter', 'isValid', 'getData'])
40
            ->getMock();
41
42
        $this->thumbnailerMock = $this->getMockBuilder('DmFileman\Service\Thumbnailer\Thumbnailer')
43
            ->setMethods(['resize'])
44
            ->disableOriginalConstructor()
45
            ->getMock();
46
47
        $this->userTextMock = $this->getMockBuilder('DmCommon\View\Helper\UserText')
48
            ->setMethods(['getMessage'])
49
            ->getMock();
50
51
        $this->sut = new DeleteFileController(
52
            $this->fileManagerMock,
53
            $this->deleteFileFormMock,
54
            $this->thumbnailerMock,
55
            $this->userTextMock
56
        );
57
58
        $this->mockFactory = new PluginMockFactory($this);
59
    }
60
61
    /**
62
     * @covers DmFileman\Controller\DeleteFileController
63
     */
64
    public function testDeleteActionAddsErrorMessageToFlashMessengerByDefault()
65
    {
66
        $responseMock = $this->mockFactory->getResponseMock();
67
68
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 1);
69
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
70
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
71
72
        $this->sut->setPluginManager($pluginMock);
73
74
        $actualResult = $this->sut->deleteAction();
75
76
        $this->assertEquals($responseMock, $actualResult);
77
    }
78
79
    /**
80
     * @covers DmFileman\Controller\DeleteFileController
81
     */
82
    public function testDeleteActionAddsErrorMessageToFlashMessengerWhenFormIsNotValid()
83
    {
84
        $responseMock = $this->mockFactory->getResponseMock();
85
86
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 1);
87
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
88
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
89
90
        $this->sut->setPluginManager($pluginMock);
91
92
        $requestMock = $this->mockFactory->getRequestMock([]);
93
        $this->sut->setRequest($requestMock);
94
95
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init']);
96
        $this->deleteFileFormMock
97
            ->expects($this->once())
98
            ->method('getInputFilter')
99
            ->will($this->returnValue($inputFilterMock));
100
        $this->deleteFileFormMock
101
            ->expects($this->once())
102
            ->method('isValid')
103
            ->will($this->returnValue(false));
104
105
        $actualResult = $this->sut->deleteAction();
106
107
        $this->assertEquals($responseMock, $actualResult);
108
    }
109
110
    /**
111
     * @covers DmFileman\Controller\DeleteFileController
112
     */
113
    public function testDeleteActionAddsSuccessMessageToFlashMessengerWhenFormIsValid()
114
    {
115
        $responseMock = $this->mockFactory->getResponseMock();
116
117
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(1, 0);
118
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
119
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
120
121
        $this->sut->setPluginManager($pluginMock);
122
123
        $requestMock = $this->mockFactory->getRequestMock([]);
124
        $this->sut->setRequest($requestMock);
125
126
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init']);
127
        $this->deleteFileFormMock
128
            ->expects($this->once())
129
            ->method('getInputFilter')
130
            ->will($this->returnValue($inputFilterMock));
131
        $this->deleteFileFormMock
132
            ->expects($this->once())
133
            ->method('isValid')
134
            ->will($this->returnValue(true));
135
        $this->deleteFileFormMock
136
            ->expects($this->once())
137
            ->method('getData')
138
            ->will($this->returnValue(['name' => 'foo']));
139
140
        $this->fileManagerMock->expects($this->once())->method('delete')->will($this->returnValue(true));
141
142
        $this->sut->setCurrentPath('');
143
144
        $actualResult = $this->sut->deleteAction();
145
146
        $this->assertEquals($responseMock, $actualResult);
147
    }
148
}
149