FormTest::createValidatorFailureProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 65
rs 9.216
cc 1
nc 1
nop 0

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\Contact\Validation\Factory;
6
7
use AbterPhp\Admin\TestDouble\Validation\StubRulesFactory;
8
use AbterPhp\Framework\Validation\Rules\Forbidden;
9
use AbterPhp\Framework\Validation\Rules\Url;
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 FormTest extends TestCase
17
{
18
    /** @var Form - 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
                'url'       => new Url(),
34
            ]
35
        );
36
37
        $this->sut = new Form($this->rulesFactoryMock);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function createValidatorSuccessProvider(): array
44
    {
45
        return [
46
            'valid-data'                          => [
47
                [
48
                    'identifier'      => 'qux',
49
                    'name'            => 'foo',
50
                    'to_name'         => 'bar',
51
                    'to_email'        => '[email protected]',
52
                    'success_url'     => 'https://example.com/success',
53
                    'failure_url'     => 'https://failure.example.com/',
54
                    'max_body_length' => '255',
55
                ],
56
            ],
57
            'valid-data-missing-all-not-required' => [
58
                [
59
                    'name'            => 'foo',
60
                    'to_name'         => 'bar',
61
                    'to_email'        => '[email protected]',
62
                    'success_url'     => 'https://example.com/success',
63
                    'failure_url'     => 'https://failure.example.com/',
64
                    'max_body_length' => '255',
65
                ],
66
            ],
67
        ];
68
    }
69
70
    /**
71
     * @dataProvider createValidatorSuccessProvider
72
     *
73
     * @param array $data
74
     */
75
    public function testCreateValidatorSuccess(array $data)
76
    {
77
        $this->runTestCreateValidator($data, true);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function createValidatorFailureProvider(): array
84
    {
85
        return [
86
            'invalid-data-missing-name'            => [
87
                [
88
                    'to_name'         => 'bar',
89
                    'to_email'        => '[email protected]',
90
                    'success_url'     => 'https://example.com/success',
91
                    'failure_url'     => 'https://failure.example.com/',
92
                    'max_body_length' => '255',
93
                ],
94
            ],
95
            'invalid-data-missing-to_name'         => [
96
                [
97
                    'name'            => 'foo',
98
                    'to_email'        => '[email protected]',
99
                    'success_url'     => 'https://example.com/success',
100
                    'failure_url'     => 'https://failure.example.com/',
101
                    'max_body_length' => '255',
102
                ],
103
            ],
104
            'invalid-data-missing-to_email'        => [
105
                [
106
                    'name'            => 'foo',
107
                    'to_name'         => 'bar',
108
                    'success_url'     => 'https://example.com/success',
109
                    'failure_url'     => 'https://failure.example.com/',
110
                    'max_body_length' => '255',
111
                ],
112
            ],
113
            'invalid-data-missing-success_url'     => [
114
                [
115
                    'name'            => 'foo',
116
                    'to_name'         => 'bar',
117
                    'to_email'        => '[email protected]',
118
                    'failure_url'     => 'https://failure.example.com/',
119
                    'max_body_length' => '255',
120
                ],
121
            ],
122
            'invalid-data-missing-failure_url'     => [
123
                [
124
                    'name'            => 'foo',
125
                    'to_name'         => 'bar',
126
                    'to_email'        => '[email protected]',
127
                    'success_url'     => 'https://example.com/success',
128
                    'max_body_length' => '255',
129
                ],
130
            ],
131
            'invalid-data-missing-max_body_length' => [
132
                [
133
                    'name'        => 'foo',
134
                    'to_name'     => 'bar',
135
                    'to_email'    => '[email protected]',
136
                    'success_url' => 'https://example.com/success',
137
                    'failure_url' => 'https://failure.example.com/',
138
                ],
139
            ],
140
            'invalid-data-non-numeric-body_length' => [
141
                [
142
                    'name'            => 'foo',
143
                    'to_name'         => 'bar',
144
                    'to_email'        => '[email protected]',
145
                    'success_url'     => 'https://example.com/success',
146
                    'failure_url'     => 'https://failure.example.com/',
147
                    'max_body_length' => 'abc',
148
                ],
149
            ],
150
        ];
151
    }
152
153
    /**
154
     * @dataProvider createValidatorFailureProvider
155
     *
156
     * @param array $data
157
     */
158
    public function testCreateValidatorFailure(array $data)
159
    {
160
        $this->runTestCreateValidator($data, false);
161
    }
162
163
    /**
164
     * @param array $data
165
     * @param bool  $expectedResult
166
     */
167
    public function runTestCreateValidator(array $data, bool $expectedResult)
168
    {
169
        $validator = $this->sut->createValidator();
170
171
        $this->assertInstanceOf(IValidator::class, $validator);
172
173
        $actualResult = $validator->isValid($data);
174
175
        $this->assertSame($expectedResult, $actualResult);
176
    }
177
}
178