UploadFileTest::testValidation()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 18
nc 4
nop 3
1
<?php
2
3
namespace DmFilemanTest\InputFilter;
4
5
use DmFileman\InputFilter\UploadFile;
6
use DmFileman\Form\UploadFileForm as Form;
7
8
class UploadFileTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var UploadFile */
11
    protected $sut;
12
13
    protected function setUp()
14
    {
15
        $this->sut = new UploadFile();
16
    }
17
18
    /**
19
     * @return array
20
     */
21
    private function securityDataProvider()
22
    {
23
        return [
24
            [
25
                null,
26
                ['isEmpty'],
27
                Form::SECURITY
28
            ],
29
            [
30
                false,
31
                ['isEmpty'],
32
                Form::SECURITY
33
            ],
34
            [
35
                '',
36
                ['isEmpty'],
37
                Form::SECURITY
38
            ],
39
            [
40
                'foo',
41
                [],
42
                Form::SECURITY
43
            ],
44
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function inputDataProvider()
51
    {
52
        return array_merge(
53
            $this->securityDataProvider(),
54
            []
55
        );
56
    }
57
58
    /**
59
     * @covers       DmFileman\InputFilter\UploadFile
60
     * @dataProvider inputDataProvider
61
     *
62
     * @param mixed  $nameData
63
     * @param array  $expectedMessages
64
     * @param string $inputName
65
     */
66
    public function testValidation($nameData, array $expectedMessages, $inputName)
67
    {
68
        $this->sut->setCurrentDir('');
69
70
        $this->sut->init();
71
72
        $this->sut->setData([$inputName => $nameData]);
73
74
        $this->sut->isValid();
75
76
        $actualMessages = $this->sut->getMessages();
77
78
        $this->assertInternalType('array', $actualMessages);
79
80
        if ($expectedMessages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedMessages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            $this->assertArrayHasKey($inputName, $actualMessages);
82
            $this->assertInternalType('array', $actualMessages[$inputName]);
83
            $message = 'Found message keys: ' . implode(', ', array_keys($actualMessages[$inputName]));
84
            foreach ($expectedMessages as $expectedMessage) {
85
                $this->assertArrayHasKey($expectedMessage, $actualMessages[$inputName], $message);
86
            }
87
        } else {
88
            $message = '';
89
            if (isset($actualMessages[$inputName])) {
90
                $message = 'Found message keys: ' . implode(', ', array_keys($actualMessages[$inputName]));
91
            }
92
            $this->assertArrayNotHasKey($inputName, $actualMessages, $message);
93
        }
94
    }
95
96
    /**
97
     * @covers DmFileman\InputFilter\UploadFile
98
     */
99
    public function testFileValidatorSAreSet()
100
    {
101
        $validatorChainMock = $this->getMockBuilder('Zend\Validator\ValidatorChain')
102
            ->setMethods(['attachByName'])
103
            ->disableOriginalConstructor()
104
            ->getMock();
105
        $filterChainMock    = $this->getMockBuilder('Zend\Filter\FilterChain')
106
            ->setMethods(['attachByName'])
107
            ->disableOriginalConstructor()
108
            ->getMock();
109
        $fileInputMock      = $this->getMockBuilder('Zend\InputFilter\FileInput')
110
            ->setMethods(['getValidatorChain', 'getFilterChain'])
111
            ->disableOriginalConstructor()
112
            ->getMock();
113
114
        $fileInputMock
115
            ->expects($this->exactly(2))
116
            ->method('getValidatorChain')
117
            ->will($this->returnValue($validatorChainMock));
118
        $validatorChainMock
119
            ->expects($this->exactly(2))
120
            ->method('attachByName')
121
            ->will($this->returnValue($validatorChainMock));
122
123
        $fileInputMock
124
            ->expects($this->never())
125
            ->method('getFilterChain')
126
            ->will($this->returnValue($filterChainMock));
127
        $filterChainMock
128
            ->expects($this->never())
129
            ->method('attachByName')
130
            ->will($this->returnValue($filterChainMock));
131
132
        $this->sut->setFileInput($fileInputMock);
133
134
        $this->sut->setMaxSize(1000);
135
        $this->sut->setExtensions(['jpg']);
136
137
        $this->sut->init();
138
    }
139
140
    /**
141
     * @covers DmFileman\InputFilter\UploadFile
142
     */
143
    public function testRenameFilterIsSet()
144
    {
145
        $validatorChainMock = $this->getMockBuilder('Zend\Validator\ValidatorChain')
146
            ->setMethods(['attachByName'])
147
            ->disableOriginalConstructor()
148
            ->getMock();
149
        $filterChainMock    = $this->getMockBuilder('Zend\Filter\FilterChain')
150
            ->setMethods(['attachByName'])
151
            ->disableOriginalConstructor()
152
            ->getMock();
153
        $fileInputMock      = $this->getMockBuilder('Zend\InputFilter\FileInput')
154
            ->setMethods(['getValidatorChain', 'getFilterChain', 'setName', 'setRequired'])
155
            ->disableOriginalConstructor()
156
            ->getMock();
157
158
        $fileInputMock
159
            ->expects($this->never())
160
            ->method('getValidatorChain')
161
            ->will($this->returnValue($validatorChainMock));
162
        $validatorChainMock
163
            ->expects($this->never())
164
            ->method('attachByName')
165
            ->will($this->returnValue($validatorChainMock));
166
167
        $fileInputMock
168
            ->expects($this->exactly(1))
169
            ->method('getFilterChain')
170
            ->will($this->returnValue($filterChainMock));
171
        $filterChainMock
172
            ->expects($this->exactly(1))
173
            ->method('attachByName')
174
            ->will($this->returnValue($filterChainMock));
175
176
        $this->sut->setFileInput($fileInputMock);
177
178
        $this->sut->setCurrentDir('');
179
180
        $this->sut->init();
181
    }
182
}
183