FirstnameTest::invalidFirstnameProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 26
rs 9.552
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Hryvinskyi\QuoteAddressValidator\Test\Unit\Model\Validation;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\ConfigInterface;
6
use Hryvinskyi\QuoteAddressValidator\Model\Validation\Firstname;
7
use Magento\Framework\Exception\LocalizedException;
8
use Magento\Quote\Api\Data\AddressInterface;
9
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class FirstnameTest extends TestCase
12
{
13
    private $configMock;
14
    private $addressMock;
15
    private $firstnameValidator;
16
17
    public const FIRSTNAME_REGEX = '/^([\p{L}0-9&#$€£¥¢%&?!()@_:;,.*+\s\-\']{1,50})$/u';
18
    public const FIRSTNAME_STOPW = '{{,}},gettemplate,base64_,afterfiltercall,.filter(';
19
    public const FIRSTNAME_ERROR = 'First Name "%1" is not valid, please use only letters, spaces, hyphens (-), apostrophes (\'). First Name must be between 2 and 50 characters.';
20
21
    protected function setUp(): void
22
    {
23
        $this->configMock = $this->createMock(ConfigInterface::class);
24
        $this->addressMock = $this->createMock(AddressInterface::class);
25
        $this->firstnameValidator = new Firstname($this->configMock);
26
27
        $this->configMock->method('getFirstnameRegex')->willReturn(static::FIRSTNAME_REGEX);
28
        $this->configMock->method('getFirstnameStopwords')->willReturn(explode(',', static::FIRSTNAME_STOPW));
29
        $this->configMock->method('getFirstnameErrorMessage')->willReturn(static::FIRSTNAME_ERROR);
30
    }
31
32
    public function testExecuteValidationDisabled()
33
    {
34
        $this->configMock->method('isEnabledFirstname')->willReturn(false);
35
        $this->addressMock->method('getLastname')->willReturn('{{template}} &#$€£¥¢%&?!()@_:;,.*+');
36
        $result = $this->firstnameValidator->execute($this->addressMock);
37
        $this->assertTrue($result);
38
    }
39
40
    /**
41
     * @dataProvider validFirstnameProvider
42
     */
43
    public function testExecuteValidFirstnameRegexOnly($firstname)
44
    {
45
        $this->configMock->method('getValidationType')->willReturn(1);
46
        $this->configMock->method('isEnabledFirstname')->willReturn(true);
47
        $this->addressMock->method('getFirstname')->willReturn($firstname);
48
49
        $result = $this->firstnameValidator->execute($this->addressMock);
50
        $this->assertTrue($result);
51
    }
52
53
    /**
54
     * @dataProvider validFirstnameProvider
55
     */
56
    public function testExecuteValidFirstnameStopWordsOnly($firstname)
57
    {
58
        $this->configMock->method('getValidationType')->willReturn(2);
59
        $this->configMock->method('isEnabledFirstname')->willReturn(true);
60
        $this->addressMock->method('getFirstname')->willReturn($firstname);
61
62
        $result = $this->firstnameValidator->execute($this->addressMock);
63
        $this->assertTrue($result);
64
    }
65
66
    /**
67
     * @dataProvider validFirstnameProvider
68
     */
69
    public function testExecuteValidFirstnameRegexAndStopWords($firstname)
70
    {
71
        $this->configMock->method('getValidationType')->willReturn(3);
72
        $this->configMock->method('isEnabledFirstname')->willReturn(true);
73
        $this->addressMock->method('getFirstname')->willReturn($firstname);
74
75
        $result = $this->firstnameValidator->execute($this->addressMock);
76
        $this->assertTrue($result);
77
    }
78
79
    public function validFirstnameProvider()
80
    {
81
        return [
82
            [null],
83
            [''],
84
            ['Reithmeier'],
85
            ['Wolfgang'],
86
            ['Reithmeier-Wolfgang'],
87
            ['O\'Connor'],
88
            ['Gerigk & Smiejczak GbR'],
89
            ['12345'],
90
            ['José'],
91
            ['Nguyễn'],
92
            ['Zhang Wei'],
93
            ['El-Masry'],
94
            ['&#$€£¥¢%&?!()@_:;,.*+'],
95
            ['&'],
96
            ['#'],
97
            ['$'],
98
            ['€'],
99
            ['£'],
100
            ['¥'],
101
            ['¢'],
102
            ['%'],
103
            ['&'],
104
            ['?'],
105
            ['!'],
106
            ['('],
107
            [')'],
108
            ['@'],
109
            ['_'],
110
            [':'],
111
            [';'],
112
            [','],
113
            ['.'],
114
            ['*'],
115
            ['+'],
116
            ['B2B'],
117
            ['José'],
118
            ["O'Connor"],
119
            ['. Gerigk & Smiejczak GbR'],
120
            ['Order #1234'],
121
            ['Order #1234+']
122
        ];
123
    }
124
125
    /**
126
     * @dataProvider invalidFirstnameProvider
127
     */
128
    public function testExecuteInvalidFirstnameRegexOnly($firstname, $expectedMessage)
129
    {
130
        $this->configMock->method('getValidationType')->willReturn(1);
131
        $this->configMock->method('isEnabledFirstname')->willReturn(true);
132
        $this->addressMock->method('getFirstname')->willReturn($firstname);
133
134
        $this->expectException(LocalizedException::class);
135
        $this->expectExceptionMessage($expectedMessage);
136
137
        $this->firstnameValidator->execute($this->addressMock);
138
    }
139
140
    /**
141
     * @dataProvider invalidFirstnameStopWordProvider
142
     */
143
    public function testExecuteInvalidFirstnameStopWordOnly($firstname, $expectedMessage)
144
    {
145
        $this->configMock->method('getValidationType')->willReturn(2);
146
        $this->configMock->method('isEnabledFirstname')->willReturn(true);
147
        $this->addressMock->method('getFirstname')->willReturn($firstname);
148
149
        $this->expectException(LocalizedException::class);
150
        $this->expectExceptionMessage($expectedMessage);
151
152
        $this->firstnameValidator->execute($this->addressMock);
153
    }
154
155
    /**
156
     * @dataProvider invalidFirstnameStopWordRegexProvider
157
     */
158
    public function testExecuteInvalidFirstnameRegexAndStopWord($firstname, $expectedMessage)
159
    {
160
        $this->configMock->method('getValidationType')->willReturn(3);
161
        $this->configMock->method('isEnabledFirstname')->willReturn(true);
162
        $this->addressMock->method('getFirstname')->willReturn($firstname);
163
164
        $this->expectException(LocalizedException::class);
165
        $this->expectExceptionMessage($expectedMessage);
166
167
        $this->firstnameValidator->execute($this->addressMock);
168
    }
169
170
    public function invalidFirstnameStopWordProvider()
171
    {
172
        return [
173
            ['gettemplate', str_replace('%1', 'gettemplate', static::FIRSTNAME_ERROR)],
174
            ['base64_', str_replace('%1', 'base64_', static::FIRSTNAME_ERROR)],
175
            ['afterfiltercall', str_replace('%1', 'afterfiltercall', static::FIRSTNAME_ERROR)],
176
            ['.filter(', str_replace('%1', '.filter(', static::FIRSTNAME_ERROR)],
177
            ['{{', str_replace('%1', '{{', static::FIRSTNAME_ERROR)],
178
            ['}}', str_replace('%1', '}}', static::FIRSTNAME_ERROR)],
179
        ];
180
    }
181
182
    public function invalidFirstnameProvider()
183
    {
184
        return [
185
            ['{{template}}', str_replace('%1', '{{template}}', static::FIRSTNAME_ERROR)],
186
            ['{{var this.getTemplateFilter().filter(lastname)}}', str_replace('%1', '{{var this.getTemplateFilter().filter(lastname)}}', static::FIRSTNAME_ERROR)],
187
            ['<invalid>', str_replace('%1', '<invalid>', static::FIRSTNAME_ERROR)],
188
            ['[brackets]', str_replace('%1', '[brackets]', static::FIRSTNAME_ERROR)],
189
            ['{}', str_replace('%1', '{}', static::FIRSTNAME_ERROR)],
190
            ['=equals', str_replace('%1', '=equals', static::FIRSTNAME_ERROR)],
191
            ['^caret', str_replace('%1', '^caret', static::FIRSTNAME_ERROR)],
192
            ['~tilde', str_replace('%1', '~tilde', static::FIRSTNAME_ERROR)],
193
            ['invalid~name', str_replace('%1', 'invalid~name', static::FIRSTNAME_ERROR)],
194
            ['{{', str_replace('%1', '{{', static::FIRSTNAME_ERROR)],
195
            ['}}', str_replace('%1', '}}', static::FIRSTNAME_ERROR)],
196
            ['{', str_replace('%1', '{', static::FIRSTNAME_ERROR)],
197
            ['}', str_replace('%1', '}', static::FIRSTNAME_ERROR)],
198
            ['[', str_replace('%1', '[', static::FIRSTNAME_ERROR)],
199
            [']', str_replace('%1', ']', static::FIRSTNAME_ERROR)],
200
            ['<', str_replace('%1', '<', static::FIRSTNAME_ERROR)],
201
            ['>', str_replace('%1', '>', static::FIRSTNAME_ERROR)],
202
            ['=', str_replace('%1', '=', static::FIRSTNAME_ERROR)],
203
            ['^', str_replace('%1', '^', static::FIRSTNAME_ERROR)],
204
            ['~', str_replace('%1', '~', static::FIRSTNAME_ERROR)],
205
            [
206
                'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.',
207
                str_replace('%1', 'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.', static::FIRSTNAME_ERROR)
208
            ],
209
        ];
210
    }
211
212
    public function invalidFirstnameStopWordRegexProvider()
213
    {
214
        return [
215
            ['gettemplate', str_replace('%1', 'gettemplate', static::FIRSTNAME_ERROR)],
216
            ['base64_', str_replace('%1', 'base64_', static::FIRSTNAME_ERROR)],
217
            ['afterfiltercall', str_replace('%1', 'afterfiltercall', static::FIRSTNAME_ERROR)],
218
            ['.filter(', str_replace('%1', '.filter(', static::FIRSTNAME_ERROR)],
219
            ['{{', str_replace('%1', '{{', static::FIRSTNAME_ERROR)],
220
            ['}}', str_replace('%1', '}}', static::FIRSTNAME_ERROR)],
221
            ['{{template}}', str_replace('%1', '{{template}}', static::FIRSTNAME_ERROR)],
222
            ['{{var this.getTemplateFilter().filter(lastname)}}', str_replace('%1', '{{var this.getTemplateFilter().filter(lastname)}}', static::FIRSTNAME_ERROR)],
223
            ['<invalid>', str_replace('%1', '<invalid>', static::FIRSTNAME_ERROR)],
224
            ['[brackets]', str_replace('%1', '[brackets]', static::FIRSTNAME_ERROR)],
225
            ['{}', str_replace('%1', '{}', static::FIRSTNAME_ERROR)],
226
            ['=equals', str_replace('%1', '=equals', static::FIRSTNAME_ERROR)],
227
            ['^caret', str_replace('%1', '^caret', static::FIRSTNAME_ERROR)],
228
            ['~tilde', str_replace('%1', '~tilde', static::FIRSTNAME_ERROR)],
229
            ['invalid~name', str_replace('%1', 'invalid~name', static::FIRSTNAME_ERROR)],
230
            ['{{', str_replace('%1', '{{', static::FIRSTNAME_ERROR)],
231
            ['}}', str_replace('%1', '}}', static::FIRSTNAME_ERROR)],
232
            ['{', str_replace('%1', '{', static::FIRSTNAME_ERROR)],
233
            ['}', str_replace('%1', '}', static::FIRSTNAME_ERROR)],
234
            ['[', str_replace('%1', '[', static::FIRSTNAME_ERROR)],
235
            [']', str_replace('%1', ']', static::FIRSTNAME_ERROR)],
236
            ['<', str_replace('%1', '<', static::FIRSTNAME_ERROR)],
237
            ['>', str_replace('%1', '>', static::FIRSTNAME_ERROR)],
238
            ['=', str_replace('%1', '=', static::FIRSTNAME_ERROR)],
239
            ['^', str_replace('%1', '^', static::FIRSTNAME_ERROR)],
240
            ['~', str_replace('%1', '~', static::FIRSTNAME_ERROR)],
241
            [
242
                'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.',
243
                str_replace('%1', 'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.', static::FIRSTNAME_ERROR)
244
            ],
245
        ];
246
    }
247
}
248