MessageTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 84
c 2
b 0
f 0
dl 0
loc 169
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B createValidatorProvider() 0 123 1
A testCreateValidator() 0 9 1
A setUp() 0 13 1
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\MaxLength;
9
use AbterPhp\Framework\Validation\Rules\Uuid;
10
use Opulence\Validation\IValidator;
11
use Opulence\Validation\Rules\Factories\RulesFactory;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
15
class MessageTest extends TestCase
16
{
17
    /** @var Message - System Under Test */
18
    protected $sut;
19
20
    /** @var RulesFactory|MockObject */
21
    protected $rulesFactoryMock;
22
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->rulesFactoryMock = StubRulesFactory::createRulesFactory(
28
            $this,
29
            [
30
                'maxLength' => new MaxLength(),
31
                'uuid'      => new Uuid(),
32
            ]
33
        );
34
35
        $this->sut = new Message($this->rulesFactoryMock);
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function createValidatorProvider(): array
42
    {
43
        return [
44
            'empty-data'                          => [
45
                [],
46
                0,
47
                false,
48
            ],
49
            'valid-data'                          => [
50
                [
51
                    'from_name'  => 'foo',
52
                    'from_email' => '[email protected]',
53
                    'subject'    => 'bar',
54
                    'body'       => 'baz',
55
                ],
56
                0,
57
                true,
58
            ],
59
            'valid-data-missing-all-not-required' => [
60
                [
61
                    'from_name'  => 'foo',
62
                    'from_email' => '[email protected]',
63
                    'subject'    => 'bar',
64
                    'body'       => 'baz',
65
                ],
66
                0,
67
                true,
68
            ],
69
            'invalid-name-missing'                => [
70
                [
71
                    'from_email' => '[email protected]',
72
                    'subject'    => 'bar',
73
                    'body'       => 'baz',
74
                ],
75
                0,
76
                false,
77
            ],
78
            'invalid-name-empty'                  => [
79
                [
80
                    'from_name'  => '',
81
                    'from_email' => '[email protected]',
82
                    'subject'    => 'bar',
83
                    'body'       => 'baz',
84
                ],
85
                0,
86
                false,
87
            ],
88
            'invalid-email-missing'               => [
89
                [
90
                    'from_name' => 'foo',
91
                    'subject'   => 'bar',
92
                    'body'      => 'baz',
93
                ],
94
                0,
95
                false,
96
            ],
97
            'invalid-email-empty'                 => [
98
                [
99
                    'from_name'  => 'foo',
100
                    'from_email' => '',
101
                    'subject'    => 'bar',
102
                    'body'       => 'baz',
103
                ],
104
                0,
105
                false,
106
            ],
107
            'invalid-email-not-valid-email'       => [
108
                [
109
                    'from_name'  => 'foo',
110
                    'from_email' => 'jane@@example.com',
111
                    'subject'    => 'bar',
112
                    'body'       => 'baz',
113
                ],
114
                0,
115
                false,
116
            ],
117
            'invalid-subject-missing'             => [
118
                [
119
                    'from_name'  => 'foo',
120
                    'from_email' => '[email protected]',
121
                    'body'       => 'baz',
122
                ],
123
                0,
124
                false,
125
            ],
126
            'invalid-subject-empty'               => [
127
                [
128
                    'from_name'  => 'foo',
129
                    'from_email' => '[email protected]',
130
                    'subject'    => '',
131
                    'body'       => 'baz',
132
                ],
133
                0,
134
                false,
135
            ],
136
            'invalid-body-missing'                => [
137
                [
138
                    'from_name'  => 'foo',
139
                    'from_email' => '[email protected]',
140
                    'subject'    => 'bar',
141
                ],
142
                0,
143
                false,
144
            ],
145
            'invalid-body-empty'                  => [
146
                [
147
                    'from_name'  => 'foo',
148
                    'from_email' => '[email protected]',
149
                    'subject'    => 'bar',
150
                    'body'       => '',
151
                ],
152
                0,
153
                false,
154
            ],
155
            'invalid-body-too-long'               => [
156
                [
157
                    'from_name'  => 'foo',
158
                    'from_email' => '[email protected]',
159
                    'subject'    => 'bar',
160
                    'body'       => 'baz',
161
                ],
162
                2,
163
                false,
164
            ],
165
        ];
166
    }
167
168
    /**
169
     * @dataProvider createValidatorProvider
170
     *
171
     * @param array $data
172
     * @param int   $maxBodyLength
173
     * @param bool  $expectedResult
174
     */
175
    public function testCreateValidator(array $data, int $maxBodyLength, bool $expectedResult)
176
    {
177
        $validator = $this->sut->setMaxBodyLength($maxBodyLength)->createValidator();
178
179
        $this->assertInstanceOf(IValidator::class, $validator);
180
181
        $actualResult = $validator->isValid($data);
182
183
        $this->assertSame($expectedResult, $actualResult);
184
    }
185
}
186