StreetTest::validStreetProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 60
rs 9.424
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
namespace Hryvinskyi\QuoteAddressValidator\Test\Unit\Model\Validation;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\ConfigInterface;
6
use Hryvinskyi\QuoteAddressValidator\Model\Validation\Street;
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 StreetTest extends TestCase
12
{
13
    private $configMock;
14
    private $addressMock;
15
    private $streetValidator;
16
17
    public const STREET_REGEX = '/^([\p{L}0-9&#$€£¥¢%&?!()@_:;,\'+\s\-\.\*\/\\\\]*)$/u';
18
    public const STREET_STOPW = '{{,}},gettemplate,base64_,afterfiltercall,.filter(';
19
    public const STREET_ERROR = 'Street "%1" is not valid, please use only letters, spaces, hyphens (-), apostrophes (\'). Street 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->streetValidator = new Street($this->configMock);
26
        $this->configMock->method('getStreetRegex')->willReturn(static::STREET_REGEX);
27
        $this->configMock->method('getStreetStopwords')->willReturn(explode(',', static::STREET_STOPW));
28
        $this->configMock->method('getStreetErrorMessage')->willReturn(static::STREET_ERROR);
29
        preg_match(static::STREET_REGEX, '');
30
    }
31
32
    public function testExecuteValidationDisabled()
33
    {
34
        // Test case when the last name validation is disabled
35
        $this->configMock->method('isEnabledStreet')->willReturn(false);
36
        $this->addressMock->method('getStreet')->willReturn('{{template}} &#$€£¥¢%&?!()@_:;,.*+');
37
38
        $result = $this->streetValidator->execute($this->addressMock);
39
        $this->assertTrue($result);
40
    }
41
42
    /**
43
     * @dataProvider validStreetProvider
44
     */
45
    public function testExecuteValidStreetRegexOnly($street)
46
    {
47
        $this->configMock->method('getValidationType')->willReturn(1);
48
        $this->configMock->method('isEnabledStreet')->willReturn(true);
49
        $this->addressMock->method('getStreet')->willReturn($street);
50
51
        $result = $this->streetValidator->execute($this->addressMock);
52
        $this->assertTrue($result);
53
    }
54
55
    /**
56
     * @dataProvider validStreetProvider
57
     */
58
    public function testExecuteValidStreetStopWordsOnly($street)
59
    {
60
        $this->configMock->method('getValidationType')->willReturn(2);
61
        $this->configMock->method('isEnabledStreet')->willReturn(true);
62
        $this->addressMock->method('getStreet')->willReturn($street);
63
64
        $result = $this->streetValidator->execute($this->addressMock);
65
        $this->assertTrue($result);
66
    }
67
68
    /**
69
     * @dataProvider validStreetProvider
70
     */
71
    public function testExecuteValidStreetRegexAndStopWords($street)
72
    {
73
        $this->configMock->method('getValidationType')->willReturn(3);
74
        $this->configMock->method('isEnabledStreet')->willReturn(true);
75
        $this->addressMock->method('getStreet')->willReturn($street);
76
77
        $result = $this->streetValidator->execute($this->addressMock);
78
        $this->assertTrue($result);
79
    }
80
81
    public function validStreetProvider()
82
    {
83
        return [
84
            [null],
85
            [''],
86
            [
87
                ['Pine Street Apt 7B'],
88
                ['Pine Street Apt 7B'],
89
            ],
90
            [
91
                ['Pine Street Apt 7B +'],
92
                ['Pine Street Apt 7B +'],
93
            ],
94
            [
95
                ['Grünerstraße 42'],
96
                ['Grünerstraße 42'],
97
            ],
98
            [
99
                ['Reithmeier-Wolfgang Ave'],
100
                ['Reithmeier-Wolfgang Ave'],
101
            ],
102
            [
103
                ['#1 Victory Plaza'],
104
                ['#1 Victory Plaza'],
105
            ],
106
            [
107
                ['B2B Complex'],
108
                ['B2B Complex'],
109
            ],
110
            [
111
                ['Order #1234'],
112
                ['Order #1234'],
113
            ],
114
            [
115
                ['Pine Street Apt 7B'],
116
                ['Pine Street Apt 7B'],
117
            ],
118
            [
119
                ['Unit 7/8 Queens Avenue , '],
120
                ['Unit 7/8 Queens Avenue , '],
121
            ],
122
            [
123
                ['Unit 7\8 Queens Avenue &'],
124
                ['Unit 7\8 Queens Avenue &'],
125
            ],
126
            [
127
                ['Unit 7\8 Queens Avenue #'],
128
                ['Unit 7\8 Queens Avenue #'],
129
            ],
130
            [
131
                ['Unit 7\8 Queens Avenue $'],
132
                ['Unit 7\8 Queens Avenue $'],
133
            ],
134
            [
135
                ['Unit 7\8 Queens Avenue €'],
136
                ['Unit 7\8 Queens Avenue €'],
137
            ],
138
            [
139
                ['Unit 7\8 Queens Avenue £¥¢%&?!()@_:;,'],
140
                ['Unit 7\8 Queens Avenue £¥¢%&?!()@_:;,'],
141
            ]
142
        ];
143
    }
144
145
    /**
146
     * @dataProvider invalidStreetProvider
147
     */
148
    public function testExecuteInvalidStreetRegexOnly($street, $expectedMessage)
149
    {
150
        $this->configMock->method('getValidationType')->willReturn(1);
151
        $this->configMock->method('isEnabledStreet')->willReturn(true);
152
        $this->addressMock->method('getStreet')->willReturn($street);
153
154
        $this->expectException(LocalizedException::class);
155
        $this->expectExceptionMessage($expectedMessage);
156
157
        $result = $this->streetValidator->execute($this->addressMock);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
158
    }
159
    /**
160
     * @dataProvider invalidStreetStopWordProvider
161
     */
162
    public function testExecuteInvalidStreetStopWordOnly($street, $expectedMessage)
163
    {
164
        $this->configMock->method('getValidationType')->willReturn(2);
165
        $this->configMock->method('isEnabledStreet')->willReturn(true);
166
        $this->addressMock->method('getStreet')->willReturn($street);
167
168
        $this->expectException(LocalizedException::class);
169
        $this->expectExceptionMessage($expectedMessage);
170
171
        $result = $this->streetValidator->execute($this->addressMock);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
172
    }
173
174
175
    public function invalidStreetStopWordProvider()
176
    {
177
        return [
178
            [['gettemplate'], str_replace('%1', 'gettemplate', static::STREET_ERROR)],
179
            [['base64_'], str_replace('%1', 'base64_', static::STREET_ERROR)],
180
            [['afterfiltercall'], str_replace('%1', 'afterfiltercall', static::STREET_ERROR)],
181
            [['.filter('], str_replace('%1', '.filter(', static::STREET_ERROR)],
182
            [['{{'], str_replace('%1', '{{', static::STREET_ERROR)],
183
            [['}}'], str_replace('%1', '}}', static::STREET_ERROR)],
184
        ];
185
    }
186
187
    /**
188
     * @dataProvider invalidStreetStopWordRegexProvider
189
     */
190
    public function testExecuteInvalidStreetRegexAndStopWord($street, $expectedMessage)
191
    {
192
        $this->configMock->method('getValidationType')->willReturn(3);
193
        $this->configMock->method('isEnabledStreet')->willReturn(true);
194
        $this->addressMock->method('getStreet')->willReturn($street);
195
196
        $this->expectException(LocalizedException::class);
197
        $this->expectExceptionMessage($expectedMessage);
198
199
        $result = $this->streetValidator->execute($this->addressMock);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
200
    }
201
202
    public function invalidStreetProvider()
203
    {
204
        return [
205
            [['{{template}}'], str_replace('%1', '{{template}}', static::STREET_ERROR)],
206
            [['{{var this.getTemplateFilter().filter(firstname)}}'], str_replace('%1', '{{var this.getTemplateFilter().filter(firstname)}}', static::STREET_ERROR)],
207
            [['{{var this.getTemp%00lateFilter().filter(firstname)}}'], str_replace('%1', '{{var this.getTemp%00lateFilter().filter(firstname)}}', static::STREET_ERROR)],
208
            [['{{var this.filter(firstname)}}'], str_replace('%1', '{{var this.filter(firstname)}}', static::STREET_ERROR)],
209
            [['Grünerstraße {{'], str_replace('%1', 'Grünerstraße {{', static::STREET_ERROR)],
210
            [['Grünerstraße }}'], str_replace('%1', 'Grünerstraße }}', static::STREET_ERROR)],
211
            [['Grünerstraße {'], str_replace('%1', 'Grünerstraße {', static::STREET_ERROR)],
212
            [['Grünerstraße }'], str_replace('%1', 'Grünerstraße }', static::STREET_ERROR)],
213
            [['Grünerstraße ['], str_replace('%1', 'Grünerstraße [', static::STREET_ERROR)],
214
            [['Grünerstraße ]'], str_replace('%1', 'Grünerstraße ]', static::STREET_ERROR)],
215
            [['Grünerstraße <'], str_replace('%1', 'Grünerstraße <', static::STREET_ERROR)],
216
            [['Grünerstraße >'], str_replace('%1', 'Grünerstraße >', static::STREET_ERROR)],
217
            [['Grünerstraße ='], str_replace('%1', 'Grünerstraße =', static::STREET_ERROR)],
218
            [['Grünerstraße ^'], str_replace('%1', 'Grünerstraße ^', static::STREET_ERROR)],
219
            [['Grünerstraße ~'], str_replace('%1', 'Grünerstraße ~', static::STREET_ERROR)],
220
            [['{{Grünerstraße'], str_replace('%1', '{{Grünerstraße', static::STREET_ERROR)],
221
            [['}}Grünerstraße'], str_replace('%1', '}}Grünerstraße', static::STREET_ERROR)],
222
            [['{Grünerstraße'], str_replace('%1', '{Grünerstraße', static::STREET_ERROR)],
223
            [['}Grünerstraße'], str_replace('%1', '}Grünerstraße', static::STREET_ERROR)],
224
            [['[Grünerstraße'], str_replace('%1', '[Grünerstraße', static::STREET_ERROR)],
225
            [[']Grünerstraße'], str_replace('%1', ']Grünerstraße', static::STREET_ERROR)],
226
            [['<Grünerstraße'], str_replace('%1', '<Grünerstraße', static::STREET_ERROR)],
227
            [['>Grünerstraße'], str_replace('%1', '>Grünerstraße', static::STREET_ERROR)],
228
            [['=Grünerstraße'], str_replace('%1', '=Grünerstraße', static::STREET_ERROR)],
229
            [['^Grünerstraße'], str_replace('%1', '^Grünerstraße', static::STREET_ERROR)],
230
            [['~Grünerstraße'], str_replace('%1', '~Grünerstraße', static::STREET_ERROR)],
231
        ];
232
    }
233
234
235
    public function invalidStreetStopWordRegexProvider()
236
    {
237
        return [
238
            [['gettemplate'], str_replace('%1', 'gettemplate', static::STREET_ERROR)],
239
            [['base64_'], str_replace('%1', 'base64_', static::STREET_ERROR)],
240
            [['afterfiltercall'], str_replace('%1', 'afterfiltercall', static::STREET_ERROR)],
241
            [['.filter('], str_replace('%1', '.filter(', static::STREET_ERROR)],
242
            [['{{'], str_replace('%1', '{{', static::STREET_ERROR)],
243
            [['}}'], str_replace('%1', '}}', static::STREET_ERROR)],
244
            [['{{template}}'], str_replace('%1', '{{template}}', static::STREET_ERROR)],
245
            [['{{var this.getTemplateFilter().filter(lastname)}}'], str_replace('%1', '{{var this.getTemplateFilter().filter(lastname)}}', static::STREET_ERROR)],
246
            [['<invalid>'], str_replace('%1', '<invalid>', static::STREET_ERROR)],
247
            [['[brackets]'], str_replace('%1', '[brackets]', static::STREET_ERROR)],
248
            [['{}'], str_replace('%1', '{}', static::STREET_ERROR)],
249
            [['=equals'], str_replace('%1', '=equals', static::STREET_ERROR)],
250
            [['^caret'], str_replace('%1', '^caret', static::STREET_ERROR)],
251
            [['~tilde'], str_replace('%1', '~tilde', static::STREET_ERROR)],
252
            [['invalid~name'], str_replace('%1', 'invalid~name', static::STREET_ERROR)],
253
            [['{{'], str_replace('%1', '{{', static::STREET_ERROR)],
254
            [['}}'], str_replace('%1', '}}', static::STREET_ERROR)],
255
            [['{'], str_replace('%1', '{', static::STREET_ERROR)],
256
            [['}'], str_replace('%1', '}', static::STREET_ERROR)],
257
            [['['], str_replace('%1', '[', static::STREET_ERROR)],
258
            [[']'], str_replace('%1', ']', static::STREET_ERROR)],
259
            [['<'], str_replace('%1', '<', static::STREET_ERROR)],
260
            [['>'], str_replace('%1', '>', static::STREET_ERROR)],
261
            [['='], str_replace('%1', '=', static::STREET_ERROR)],
262
            [['^'], str_replace('%1', '^', static::STREET_ERROR)],
263
            [['~'], str_replace('%1', '~', static::STREET_ERROR)],
264
        ];
265
    }
266
}
267