CreateDirectoryControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 29 1
A testCreateActionAddsErrorMessageToFlashMessengerByDefault() 0 14 1
B testCreateActionAddsErrorMessageToFlashMessengerWhenFormIsNotValid() 0 27 1
B testCreateActionAddsSuccessMessageToFlashMessengerWhenFormIsValid() 0 35 1
1
<?php
2
3
namespace DmFilemanTest\Controller;
4
5
use DmFileman\Controller\CreateDirectoryController;
6
use DmTest\Controller\Plugin\MockFactory as PluginMockFactory;
7
8
class CreateDirectoryControllerTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var CreateDirectoryController */
11
    protected $sut;
12
13
    /** @var \PHPUnit_Framework_MockObject_MockObject */
14
    protected $fileManagerMock;
15
16
    /** @var \PHPUnit_Framework_MockObject_MockObject */
17
    protected $createDirFormMock;
18
19
    /** @var \PHPUnit_Framework_MockObject_MockObject */
20
    protected $uploadFileFormMock;
21
22
    /** @var \PHPUnit_Framework_MockObject_MockObject */
23
    protected $deleteFileFormMock;
24
25
    /** @var \PHPUnit_Framework_MockObject_MockObject */
26
    protected $thumbnailerMock;
27
28
    /** @var \PHPUnit_Framework_MockObject_MockObject */
29
    protected $userTextMock;
30
31
    /** @var PluginMockFactory */
32
    protected $mockFactory;
33
34
    public function setUp()
35
    {
36
        $this->fileManagerMock = $this->getMockBuilder('DmFileman\Service\FileManager\FileManager')
37
            ->setMethods(['setCurrentPath', 'create'])
38
            ->disableOriginalConstructor()
39
            ->getMock();
40
41
        $this->createDirFormMock = $this->getMockBuilder('DmFileman\Form\CreateDirectoryForm')
42
            ->setMethods(['setData', 'getInputFilter', 'getData', 'isValid'])
43
            ->getMock();
44
45
        $this->thumbnailerMock = $this->getMockBuilder('DmFileman\Service\Thumbnailer\Thumbnailer')
46
            ->setMethods(['resize'])
47
            ->disableOriginalConstructor()
48
            ->getMock();
49
50
        $this->userTextMock = $this->getMockBuilder('DmCommon\View\Helper\UserText')
51
            ->setMethods(['getMessage'])
52
            ->getMock();
53
54
        $this->sut = new CreateDirectoryController(
55
            $this->fileManagerMock,
56
            $this->createDirFormMock,
57
            $this->thumbnailerMock,
58
            $this->userTextMock
59
        );
60
61
        $this->mockFactory = new PluginMockFactory($this);
62
    }
63
64
    /**
65
     * @covers DmFileman\Controller\CreateDirectoryController
66
     */
67
    public function testCreateActionAddsErrorMessageToFlashMessengerByDefault()
68
    {
69
        $responseMock = $this->mockFactory->getResponseMock();
70
71
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 1);
72
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
73
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
74
75
        $this->sut->setPluginManager($pluginMock);
76
77
        $actualResult = $this->sut->createAction();
78
79
        $this->assertEquals($responseMock, $actualResult);
80
    }
81
82
    /**
83
     * @covers DmFileman\Controller\CreateDirectoryController
84
     */
85
    public function testCreateActionAddsErrorMessageToFlashMessengerWhenFormIsNotValid()
86
    {
87
        $responseMock = $this->mockFactory->getResponseMock();
88
89
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(0, 1);
90
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
91
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
92
93
        $this->sut->setPluginManager($pluginMock);
94
95
        $requestMock = $this->mockFactory->getRequestMock([]);
96
        $this->sut->setRequest($requestMock);
97
98
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init']);
99
        $this->createDirFormMock
100
            ->expects($this->once())
101
            ->method('getInputFilter')
102
            ->will($this->returnValue($inputFilterMock));
103
        $this->createDirFormMock
104
            ->expects($this->once())
105
            ->method('isValid')
106
            ->will($this->returnValue(false));
107
108
        $actualResult = $this->sut->createAction();
109
110
        $this->assertEquals($responseMock, $actualResult);
111
    }
112
113
    /**
114
     * @covers DmFileman\Controller\CreateDirectoryController
115
     */
116
    public function testCreateActionAddsSuccessMessageToFlashMessengerWhenFormIsValid()
117
    {
118
        $responseMock = $this->mockFactory->getResponseMock();
119
120
        $flashMessengerMock = $this->mockFactory->getFlashMessengerPluginMock(1, 0);
121
        $redirectMock       = $this->mockFactory->getRedirectPluginMock($responseMock);
122
        $pluginMock         = $this->mockFactory->getPluginMock($flashMessengerMock, $redirectMock);
123
124
        $this->sut->setPluginManager($pluginMock);
125
126
        $requestMock = $this->mockFactory->getRequestMock([]);
127
        $this->sut->setRequest($requestMock);
128
129
        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter', ['init']);
130
        $this->createDirFormMock
131
            ->expects($this->once())
132
            ->method('getInputFilter')
133
            ->will($this->returnValue($inputFilterMock));
134
        $this->createDirFormMock
135
            ->expects($this->once())
136
            ->method('isValid')
137
            ->will($this->returnValue(true));
138
        $this->createDirFormMock
139
            ->expects($this->once())
140
            ->method('getData')
141
            ->will($this->returnValue(['directoryName' => 'foo']));
142
143
        $this->fileManagerMock->expects($this->once())->method('create')->will($this->returnValue(true));
144
145
        $this->sut->setCurrentPath('');
146
147
        $actualResult = $this->sut->createAction();
148
149
        $this->assertEquals($responseMock, $actualResult);
150
    }
151
}
152