CreateDirectoryTest::inputDataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace DmFilemanTest\InputFilter;
4
5
use DmFileman\InputFilter\CreateDirectory;
6
use DmFileman\Form\CreateDirectoryForm as Form;
7
8
class CreateDirectoryTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var CreateDirectory */
11
    protected $sut;
12
13
    protected function setUp()
14
    {
15
        $this->sut = new CreateDirectory();
16
17
        $this->sut->init();
18
    }
19
20
    /**
21
     * @return array
22
     */
23
    private function directoryDataProvider()
24
    {
25
        return [
26
            [
27
                null,
28
                ['isEmpty'],
29
                Form::DIRECTORY
30
            ],
31
            [
32
                false,
33
                ['isEmpty'],
34
                Form::DIRECTORY
35
            ],
36
            [
37
                '',
38
                ['isEmpty'],
39
                Form::DIRECTORY
40
            ],
41
            [
42
                'foo',
43
                [],
44
                Form::DIRECTORY
45
            ],
46
            [
47
                '1',
48
                [],
49
                Form::DIRECTORY
50
            ],
51
            [
52
                10,
53
                [],
54
                Form::DIRECTORY
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    private function securityDataProvider()
63
    {
64
        return [
65
            [
66
                null,
67
                ['isEmpty'],
68
                Form::SECURITY
69
            ],
70
            [
71
                false,
72
                ['isEmpty'],
73
                Form::SECURITY
74
            ],
75
            [
76
                '',
77
                ['isEmpty'],
78
                Form::SECURITY
79
            ],
80
            [
81
                'foo',
82
                [],
83
                Form::SECURITY
84
            ],
85
        ];
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function inputDataProvider()
92
    {
93
        return array_merge(
94
            $this->directoryDataProvider(),
95
            $this->securityDataProvider(),
96
            []
97
        );
98
    }
99
100
    /**
101
     * @covers       DmFileman\InputFilter\CreateDirectory
102
     * @dataProvider inputDataProvider
103
     *
104
     * @param mixed  $nameData
105
     * @param array  $expectedMessages
106
     * @param string $inputName
107
     */
108
    public function testValidation($nameData, array $expectedMessages, $inputName)
109
    {
110
        $this->sut->setData([$inputName => $nameData]);
111
112
        $this->sut->isValid();
113
114
        $actualMessages = $this->sut->getMessages();
115
116
        $this->assertInternalType('array', $actualMessages);
117
118
        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...
119
            $this->assertArrayHasKey($inputName, $actualMessages);
120
            $this->assertInternalType('array', $actualMessages[$inputName]);
121
            $message = 'Found message keys: ' . implode(', ', array_keys($actualMessages[$inputName]));
122
            foreach ($expectedMessages as $expectedMessage) {
123
                $this->assertArrayHasKey($expectedMessage, $actualMessages[$inputName], $message);
124
            }
125
        } else {
126
            $message = '';
127
            if (isset($actualMessages[$inputName])) {
128
                $message = 'Found message keys: ' . implode(', ', array_keys($actualMessages[$inputName]));
129
            }
130
            $this->assertArrayNotHasKey($inputName, $actualMessages, $message);
131
        }
132
    }
133
}
134