FileTest::createValidatorProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 76
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 112
rs 8.5236

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Validation\Factory\Api;
6
7
use AbterPhp\Admin\TestDouble\Validation\StubRulesFactory;
8
use AbterPhp\Framework\Validation\Rules\Base64;
9
use AbterPhp\Framework\Validation\Rules\Forbidden;
10
use AbterPhp\Framework\Validation\Rules\Uuid;
11
use Opulence\Validation\IValidator;
12
use Opulence\Validation\Rules\Factories\RulesFactory;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
class FileTest extends TestCase
17
{
18
    /** @var File - System Under Test */
19
    protected $sut;
20
21
    /** @var RulesFactory|MockObject */
22
    protected $rulesFactoryMock;
23
24
    public function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->rulesFactoryMock = StubRulesFactory::createRulesFactory(
29
            $this,
30
            [
31
                'forbidden' => new Forbidden(),
32
                'uuid'      => new Uuid(),
33
                'base64'    => new Base64(),
34
            ]
35
        );
36
37
        $this->sut = new File($this->rulesFactoryMock);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function createValidatorProvider(): array
44
    {
45
        return [
46
            'empty-data'                          => [
47
                [],
48
                false,
49
            ],
50
            'valid-data'                          => [
51
                [
52
                    'description' => 'foo',
53
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
54
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
55
                    'name'        => 'bar',
56
                    'mime'        => 'text/plain',
57
                ],
58
                true,
59
            ],
60
            'valid-data-missing-all-not-required' => [
61
                [
62
                    'description' => 'foo',
63
                    'name'        => 'bar',
64
                    'mime'        => 'text/plain',
65
                ],
66
                true,
67
            ],
68
            'invalid-id-present'                  => [
69
                [
70
                    'id'          => '465c91df-9cc7-47e2-a2ef-8fe645753148',
71
                    'description' => 'foo',
72
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
73
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
74
                    'name'        => 'bar',
75
                    'mime'        => 'text/plain',
76
                ],
77
                false,
78
            ],
79
            'invalid-description-missing'         => [
80
                [
81
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
82
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
83
                    'name'        => 'bar',
84
                    'mime'        => 'text/plain',
85
                ],
86
                false,
87
            ],
88
            'invalid-description-empty'           => [
89
                [
90
                    'description' => '',
91
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
92
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
93
                    'name'        => 'bar',
94
                    'mime'        => 'text/plain',
95
                ],
96
                false,
97
            ],
98
            'invalid-category-not-uuid'           => [
99
                [
100
                    'description' => 'foo',
101
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b05',
102
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
103
                    'name'        => 'bar',
104
                    'mime'        => 'text/plain',
105
                ],
106
                false,
107
            ],
108
            'invalid-data'                        => [
109
                [
110
                    'description' => 'foo',
111
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
112
                    'data'        => '()*!@#$%)',
113
                    'name'        => 'bar',
114
                    'mime'        => 'text/plain',
115
                ],
116
                false,
117
            ],
118
            'invalid-name-missing'                => [
119
                [
120
                    'description' => 'foo',
121
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
122
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
123
                    'mime'        => 'text/plain',
124
                ],
125
                false,
126
            ],
127
            'invalid-name-empty'                  => [
128
                [
129
                    'description' => 'foo',
130
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
131
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
132
                    'name'        => '',
133
                    'mime'        => 'text/plain',
134
                ],
135
                false,
136
            ],
137
            'invalid-mime-missing'                => [
138
                [
139
                    'description' => 'foo',
140
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
141
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
142
                    'name'        => 'bar',
143
                ],
144
                false,
145
            ],
146
            'invalid-mime-empty'                  => [
147
                [
148
                    'description' => 'foo',
149
                    'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051',
150
                    'data'        => 'aGVsbG8sIHdvcmxkIQ==',
151
                    'name'        => 'bar',
152
                    'mime'        => '',
153
                ],
154
                false,
155
            ],
156
        ];
157
    }
158
159
    /**
160
     * @dataProvider createValidatorProvider
161
     *
162
     * @param array $data
163
     * @param bool  $expectedResult
164
     */
165
    public function testCreateValidator(array $data, bool $expectedResult)
166
    {
167
        $validator = $this->sut->createValidator();
168
169
        $this->assertInstanceOf(IValidator::class, $validator);
170
171
        $actualResult = $validator->isValid($data);
172
173
        $this->assertSame($expectedResult, $actualResult);
174
    }
175
}
176