testExecuteInvalidFirstnameRegexAndStopWord()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Hryvinskyi\QuoteAddressValidator\Test\Unit\Model\Validation;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\ConfigInterface;
6
use Hryvinskyi\QuoteAddressValidator\Model\Validation\Lastname;
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 LastnameTest extends TestCase
12
{
13
    private $configMock;
14
    private $addressMock;
15
    private $lastnameValidator;
16
17
    public const LASTNAME_REGEX = '/^([\p{L}0-9&#$€£¥¢%&?!()@_:;,.*+\s\-\']{1,50})$/u';
18
    public const LASTNAME_STOPW = '{{,}},gettemplate,base64_,afterfiltercall,.filter(';
19
    public const LASTNAME_ERROR = 'Last Name "%1" is not valid, please use only letters, spaces, hyphens (-), apostrophes (\'). Last 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->lastnameValidator = new Lastname($this->configMock);
26
        $this->configMock->method('getLastnameRegex')->willReturn(static::LASTNAME_REGEX);
27
        $this->configMock->method('getLastnameStopwords')->willReturn(explode(',', static::LASTNAME_STOPW));
28
        $this->configMock->method('getLastnameErrorMessage')->willReturn(static::LASTNAME_ERROR);
29
    }
30
31
    public function testExecuteValidationDisabled()
32
    {
33
        $this->configMock->method('isEnabledLastname')->willReturn(false);
34
        $this->addressMock->method('getLastname')->willReturn('{{template}} &#$€£¥¢%&?!()@_:;,.*+');
35
36
        $result = $this->lastnameValidator->execute($this->addressMock);
37
        $this->assertTrue($result);
38
    }
39
40
    /**
41
     * @dataProvider validLastnameProvider
42
     */
43
    public function testExecuteValidLastnameRegexOnly($lastname)
44
    {
45
        $this->configMock->method('getValidationType')->willReturn(1);
46
        $this->configMock->method('isEnabledLastname')->willReturn(true);
47
        $this->addressMock->method('getLastname')->willReturn($lastname);
48
49
        $result = $this->lastnameValidator->execute($this->addressMock);
50
        $this->assertTrue($result);
51
    }
52
53
    /**
54
     * @dataProvider validLastnameProvider
55
     */
56
    public function testExecuteValidLastnameStopWordsOnly($lastname)
57
    {
58
        $this->configMock->method('getValidationType')->willReturn(2);
59
        $this->configMock->method('isEnabledLastname')->willReturn(true);
60
        $this->addressMock->method('getLastname')->willReturn($lastname);
61
62
        $result = $this->lastnameValidator->execute($this->addressMock);
63
        $this->assertTrue($result);
64
    }
65
66
    public function validLastnameProvider()
67
    {
68
        return [
69
            [null],
70
            [''],
71
            ['Reithmeier, Wolfgang'],
72
            ['Reithmeier Wolfgang'],
73
            ['Reithmeier-Wolfgang'],
74
            ['Reithmeier\'Wolfgang'],
75
            ['&#$€£¥¢%&?!()@_:;,.*+'],
76
            ['&'],
77
            ['#'],
78
            ['$'],
79
            ['€'],
80
            ['£'],
81
            ['¥'],
82
            ['¢'],
83
            ['%'],
84
            ['&'],
85
            ['?'],
86
            ['!'],
87
            ['('],
88
            [')'],
89
            ['@'],
90
            ['_'],
91
            [':'],
92
            [';'],
93
            [','],
94
            ['.'],
95
            ['*'],
96
            ['+'],
97
            ['B2B'],
98
            ['José'],
99
            ["O'Connor"],
100
            ['. Gerigk & Smiejczak GbR'],
101
            ['Order #1234'],
102
            ['Order #1234+']
103
        ];
104
    }
105
106
    /**
107
     * @dataProvider invalidLastnameProvider
108
     */
109
    public function testExecuteInvalidLastnameRegexOnly($lastname, $expectedMessage)
110
    {
111
        $this->configMock->method('getValidationType')->willReturn(1);
112
        $this->configMock->method('isEnabledLastname')->willReturn(true);
113
        $this->addressMock->method('getLastname')->willReturn($lastname);
114
115
        $this->expectException(LocalizedException::class);
116
        $this->expectExceptionMessage($expectedMessage);
117
118
        $result = $this->lastnameValidator->execute($this->addressMock);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
119
    }
120
121
    public function invalidLastnameProvider()
122
    {
123
        return [
124
            ['{{template}}', str_replace('%1', '{{template}}', static::LASTNAME_ERROR)],
125
            ['{{var this.getTemplateFilter().filter(lastname)}}', str_replace('%1', '{{var this.getTemplateFilter().filter(lastname)}}', static::LASTNAME_ERROR)],
126
            ['<invalid>', str_replace('%1', '<invalid>', static::LASTNAME_ERROR)],
127
            ['[brackets]', str_replace('%1', '[brackets]', static::LASTNAME_ERROR)],
128
            ['{}', str_replace('%1', '{}', static::LASTNAME_ERROR)],
129
            ['=equals', str_replace('%1', '=equals', static::LASTNAME_ERROR)],
130
            ['^caret', str_replace('%1', '^caret', static::LASTNAME_ERROR)],
131
            ['~tilde', str_replace('%1', '~tilde', static::LASTNAME_ERROR)],
132
            ['invalid~name', str_replace('%1', 'invalid~name', static::LASTNAME_ERROR)],
133
            ['{{', str_replace('%1', '{{', static::LASTNAME_ERROR)],
134
            ['}}', str_replace('%1', '}}', static::LASTNAME_ERROR)],
135
            ['{', str_replace('%1', '{', static::LASTNAME_ERROR)],
136
            ['}', str_replace('%1', '}', static::LASTNAME_ERROR)],
137
            ['[', str_replace('%1', '[', static::LASTNAME_ERROR)],
138
            [']', str_replace('%1', ']', static::LASTNAME_ERROR)],
139
            ['<', str_replace('%1', '<', static::LASTNAME_ERROR)],
140
            ['>', str_replace('%1', '>', static::LASTNAME_ERROR)],
141
            ['=', str_replace('%1', '=', static::LASTNAME_ERROR)],
142
            ['^', str_replace('%1', '^', static::LASTNAME_ERROR)],
143
            ['~', str_replace('%1', '~', static::LASTNAME_ERROR)],
144
            [
145
                'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.',
146
                str_replace('%1', 'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.', static::LASTNAME_ERROR)
147
            ],
148
        ];
149
    }
150
151
    /**
152
     * @dataProvider invalidLastnameStopWordProvider
153
     */
154
    public function testLastnameInvalidFirstnameStopWordOnly($lastname, $expectedMessage)
155
    {
156
        $this->configMock->method('getValidationType')->willReturn(2);
157
        $this->configMock->method('isEnabledLastname')->willReturn(true);
158
        $this->addressMock->method('getLastname')->willReturn($lastname);
159
160
        $this->expectException(LocalizedException::class);
161
        $this->expectExceptionMessage($expectedMessage);
162
163
        $this->lastnameValidator->execute($this->addressMock);
164
    }
165
166
    public function invalidLastnameStopWordProvider()
167
    {
168
        return [
169
            ['gettemplate', str_replace('%1', 'gettemplate', static::LASTNAME_ERROR)],
170
            ['base64_', str_replace('%1', 'base64_', static::LASTNAME_ERROR)],
171
            ['afterfiltercall', str_replace('%1', 'afterfiltercall', static::LASTNAME_ERROR)],
172
            ['.filter(', str_replace('%1', '.filter(', static::LASTNAME_ERROR)],
173
            ['{{', str_replace('%1', '{{', static::LASTNAME_ERROR)],
174
            ['}}', str_replace('%1', '}}', static::LASTNAME_ERROR)],
175
        ];
176
    }
177
178
    /**
179
     * @dataProvider invalidFirstnameStopWordRegexProvider
180
     */
181
    public function testExecuteInvalidFirstnameRegexAndStopWord($lastname, $expectedMessage)
182
    {
183
        $this->configMock->method('getValidationType')->willReturn(3);
184
        $this->configMock->method('isEnabledLastname')->willReturn(true);
185
        $this->addressMock->method('getLastname')->willReturn($lastname);
186
187
        $this->expectException(LocalizedException::class);
188
        $this->expectExceptionMessage($expectedMessage);
189
190
        $this->lastnameValidator->execute($this->addressMock);
191
    }
192
193
    public function invalidFirstnameStopWordRegexProvider()
194
    {
195
        return [
196
            ['gettemplate', str_replace('%1', 'gettemplate', static::LASTNAME_ERROR)],
197
            ['base64_', str_replace('%1', 'base64_', static::LASTNAME_ERROR)],
198
            ['afterfiltercall', str_replace('%1', 'afterfiltercall', static::LASTNAME_ERROR)],
199
            ['.filter(', str_replace('%1', '.filter(', static::LASTNAME_ERROR)],
200
            ['{{', str_replace('%1', '{{', static::LASTNAME_ERROR)],
201
            ['}}', str_replace('%1', '}}', static::LASTNAME_ERROR)],
202
            ['{{template}}', str_replace('%1', '{{template}}', static::LASTNAME_ERROR)],
203
            ['{{var this.getTemplateFilter().filter(lastname)}}', str_replace('%1', '{{var this.getTemplateFilter().filter(lastname)}}', static::LASTNAME_ERROR)],
204
            ['<invalid>', str_replace('%1', '<invalid>', static::LASTNAME_ERROR)],
205
            ['[brackets]', str_replace('%1', '[brackets]', static::LASTNAME_ERROR)],
206
            ['{}', str_replace('%1', '{}', static::LASTNAME_ERROR)],
207
            ['=equals', str_replace('%1', '=equals', static::LASTNAME_ERROR)],
208
            ['^caret', str_replace('%1', '^caret', static::LASTNAME_ERROR)],
209
            ['~tilde', str_replace('%1', '~tilde', static::LASTNAME_ERROR)],
210
            ['invalid~name', str_replace('%1', 'invalid~name', static::LASTNAME_ERROR)],
211
            ['{{', str_replace('%1', '{{', static::LASTNAME_ERROR)],
212
            ['}}', str_replace('%1', '}}', static::LASTNAME_ERROR)],
213
            ['{', str_replace('%1', '{', static::LASTNAME_ERROR)],
214
            ['}', str_replace('%1', '}', static::LASTNAME_ERROR)],
215
            ['[', str_replace('%1', '[', static::LASTNAME_ERROR)],
216
            [']', str_replace('%1', ']', static::LASTNAME_ERROR)],
217
            ['<', str_replace('%1', '<', static::LASTNAME_ERROR)],
218
            ['>', str_replace('%1', '>', static::LASTNAME_ERROR)],
219
            ['=', str_replace('%1', '=', static::LASTNAME_ERROR)],
220
            ['^', str_replace('%1', '^', static::LASTNAME_ERROR)],
221
            ['~', str_replace('%1', '~', static::LASTNAME_ERROR)],
222
            [
223
                'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.',
224
                str_replace('%1', 'Long text more tan 50 characters. Long text more tan 50 characters. Long text more tan 50 characters.', static::LASTNAME_ERROR)
225
            ],
226
        ];
227
    }
228
}
229