ConfigTest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 100
c 1
b 0
f 0
dl 0
loc 269
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetFirstnameErrorMessage() 0 9 1
A testIsEnabledLastname() 0 8 1
A testisEnabled() 0 8 1
A testGetStreetErrorMessage() 0 9 1
A testGetStreetStopwords() 0 10 1
A testGetLastnameStopwords() 0 10 1
A testGetFirstnameRegex() 0 9 1
A isEnabledDataProvider() 0 9 1
A testIsEnabledFirstname() 0 8 1
A setUp() 0 12 1
A testGetLastnameErrorMessage() 0 9 1
A testGetStreetRegex() 0 9 1
A testGetValidationType() 0 8 1
A testIsEnabledStreet() 0 8 1
A testGetFirstnameStopwords() 0 10 1
A testGetLastnameRegex() 0 9 1
1
<?php
2
3
namespace Hryvinskyi\QuoteAddressValidator\Test\Unit\Model;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\Config;
6
use Magento\Framework\App\Config\ScopeConfigInterface;
7
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
8
use Magento\Store\Model\ScopeInterface;
9
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\MockObject 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
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...
11
12
class ConfigTest extends TestCase
13
{
14
    /**
15
     * @var Config
16
     */
17
    protected $model;
18
19
    /**
20
     * @var ScopeConfigInterface|MockObject
21
     */
22
    protected $scopeConfigMock;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    protected function setUp(): void
28
    {
29
        $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
30
            ->setMethods(['getValue', 'isSetFlag'])
31
            ->disableOriginalConstructor()
32
            ->getMockForAbstractClass();
33
34
        $objectManager = new ObjectManagerHelper($this);
0 ignored issues
show
Deprecated Code introduced by
The class Magento\Framework\TestFr...it\Helper\ObjectManager has been deprecated: Class under test should be instantiated with `new` keyword with explicit dependencies declaration ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

34
        $objectManager = /** @scrutinizer ignore-deprecated */ new ObjectManagerHelper($this);
Loading history...
35
        $this->model = $objectManager->getObject(
36
            Config::class,
37
            [
38
                'scopeConfig' => $this->scopeConfigMock
39
            ]
40
        );
41
    }
42
43
    /**
44
     * Test isEnabled()
45
     *
46
     * @return void
47
     * @dataProvider isEnabledDataProvider
48
     */
49
    public function testisEnabled($isSetFlag, $result): void
50
    {
51
        $this->scopeConfigMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Framework\App\Config\ScopeConfigInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->scopeConfigMock->/** @scrutinizer ignore-call */ 
52
                                expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            ->method('isSetFlag')
53
            ->with(Config::XML_CONF_ENABLED, ScopeInterface::SCOPE_STORE)
54
            ->willReturn($isSetFlag);
55
56
        $this->assertEquals($result, $this->model->isEnabled());
57
    }
58
59
    /**
60
     * Data provider for isEnabled()
61
     *
62
     * @return array
63
     */
64
    public function isEnabledDataProvider(): array
65
    {
66
        return [
67
            [true, true],
68
            [false, false],
69
            [true, 1],
70
            [false, 0],
71
            [false, null],
72
            [false, '']
73
        ];
74
    }
75
76
    /**
77
     * Test getValidationType()
78
     *
79
     * @return void
80
     */
81
    public function testGetValidationType(): void
82
    {
83
        $this->scopeConfigMock->expects($this->once())
84
            ->method('getValue')
85
            ->with(Config::XML_CONF_VALIDATION_TYPE, ScopeInterface::SCOPE_STORE)
86
            ->willReturn('1');
87
88
        $this->assertEquals(1, $this->model->getValidationType());
89
    }
90
91
    /**
92
     * Test isEnabledFirstname()
93
     *
94
     * @return void
95
     */
96
    public function testIsEnabledFirstname(): void
97
    {
98
        $this->scopeConfigMock->expects($this->once())
99
            ->method('isSetFlag')
100
            ->with(Config::XML_CONF_ENABLED_FIRSTNAME, ScopeInterface::SCOPE_STORE)
101
            ->willReturn(1);
102
103
        $this->assertEquals(true, $this->model->isEnabledFirstname());
104
    }
105
106
    /**
107
     * Test isEnabledLastname()
108
     *
109
     * @return void
110
     */
111
    public function testIsEnabledLastname(): void
112
    {
113
        $this->scopeConfigMock->expects($this->once())
114
            ->method('isSetFlag')
115
            ->with(Config::XML_CONF_ENABLED_LASTNAME, ScopeInterface::SCOPE_STORE)
116
            ->willReturn(1);
117
118
        $this->assertEquals(true, $this->model->isEnabledLastname());
119
    }
120
121
    /**
122
     * Test isEnabledStreet()
123
     *
124
     * @return void
125
     */
126
    public function testIsEnabledStreet(): void
127
    {
128
        $this->scopeConfigMock->expects($this->once())
129
            ->method('isSetFlag')
130
            ->with(Config::XML_CONF_ENABLED_STREET, ScopeInterface::SCOPE_STORE)
131
            ->willReturn(1);
132
133
        $this->assertEquals(true, $this->model->isEnabledStreet());
134
    }
135
136
    /**
137
     * Test getFirstnameStopwords()
138
     *
139
     * @return void
140
     */
141
    public function testGetFirstnameStopwords(): void
142
    {
143
        $stopwords = 'test,test,example,Example';
144
        $expected = ['test', 'example', 'Example'];
145
        $this->scopeConfigMock->expects($this->once())
146
            ->method('getValue')
147
            ->with(Config::XML_CONF_FIRSTNAME_STOPWORDS, ScopeInterface::SCOPE_STORE)
148
            ->willReturn($stopwords);
149
150
        $this->assertEquals($expected, $this->model->getFirstnameStopwords());
151
    }
152
153
    /**
154
     * Test getLastnameStopwords()
155
     *
156
     * @return void
157
     */
158
    public function testGetLastnameStopwords(): void
159
    {
160
        $stopwords = 'test,test, example, Example';
161
        $expected = ['test', 'example', 'Example'];
162
        $this->scopeConfigMock->expects($this->once())
163
            ->method('getValue')
164
            ->with(Config::XML_CONF_LASTNAME_STOPWORDS, ScopeInterface::SCOPE_STORE)
165
            ->willReturn($stopwords);
166
167
        $this->assertEquals($expected, $this->model->getLastnameStopwords());
168
    }
169
170
    /**
171
     * Test getStreetStopwords()
172
     *
173
     * @return void
174
     */
175
    public function testGetStreetStopwords(): void
176
    {
177
        $stopwords = 'test,test, example, Example';
178
        $expected = ['test', 'example', 'Example'];
179
        $this->scopeConfigMock->expects($this->once())
180
            ->method('getValue')
181
            ->with(Config::XML_CONF_STREET_STOPWORDS, ScopeInterface::SCOPE_STORE)
182
            ->willReturn($stopwords);
183
184
        $this->assertEquals($expected, $this->model->getStreetStopwords());
185
    }
186
187
    /**
188
     * Test getFirstnameRegex()
189
     *
190
     * @return void
191
     */
192
    public function testGetFirstnameRegex(): void
193
    {
194
        $regex = '/^[a-zA-Z]+$/';
195
        $this->scopeConfigMock->expects($this->once())
196
            ->method('getValue')
197
            ->with(Config::XML_CONF_FIRSTNAME_REGEX, ScopeInterface::SCOPE_STORE)
198
            ->willReturn($regex);
199
200
        $this->assertEquals($regex, $this->model->getFirstnameRegex());
201
    }
202
203
    /**
204
     * Test getLastnameRegex()
205
     *
206
     * @return void
207
     */
208
    public function testGetLastnameRegex(): void
209
    {
210
        $regex = '/^[a-zA-Z]+$/';
211
        $this->scopeConfigMock->expects($this->once())
212
            ->method('getValue')
213
            ->with(Config::XML_CONF_LASTNAME_REGEX, ScopeInterface::SCOPE_STORE)
214
            ->willReturn($regex);
215
216
        $this->assertEquals($regex, $this->model->getLastnameRegex());
217
    }
218
219
    /**
220
     * Test getStreetRegex()
221
     *
222
     * @return void
223
     */
224
    public function testGetStreetRegex(): void
225
    {
226
        $regex = '/^[a-zA-Z0-9\s]+$/';
227
        $this->scopeConfigMock->expects($this->once())
228
            ->method('getValue')
229
            ->with(Config::XML_CONF_STREET_REGEX, ScopeInterface::SCOPE_STORE)
230
            ->willReturn($regex);
231
232
        $this->assertEquals($regex, $this->model->getStreetRegex());
233
    }
234
235
    /**
236
     * Test getFirstnameErrorMessage()
237
     *
238
     * @return void
239
     */
240
    public function testGetFirstnameErrorMessage(): void
241
    {
242
        $errorMessage = 'Invalid firstname';
243
        $this->scopeConfigMock->expects($this->once())
244
            ->method('getValue')
245
            ->with(Config::XML_CONF_FIRSTNAME_ERROR_MESSAGE, ScopeInterface::SCOPE_STORE)
246
            ->willReturn($errorMessage);
247
248
        $this->assertEquals($errorMessage, $this->model->getFirstnameErrorMessage());
249
    }
250
251
    /**
252
     * Test getLastnameErrorMessage()
253
     *
254
     * @return void
255
     */
256
    public function testGetLastnameErrorMessage(): void
257
    {
258
        $errorMessage = 'Invalid lastname';
259
        $this->scopeConfigMock->expects($this->once())
260
            ->method('getValue')
261
            ->with(Config::XML_CONF_LASTNAME_ERROR_MESSAGE, ScopeInterface::SCOPE_STORE)
262
            ->willReturn($errorMessage);
263
264
        $this->assertEquals($errorMessage, $this->model->getLastnameErrorMessage());
265
    }
266
267
    /**
268
     * Test getStreetErrorMessage()
269
     *
270
     * @return void
271
     */
272
    public function testGetStreetErrorMessage(): void
273
    {
274
        $errorMessage = 'Invalid street';
275
        $this->scopeConfigMock->expects($this->once())
276
            ->method('getValue')
277
            ->with(Config::XML_CONF_STREET_ERROR_MESSAGE, ScopeInterface::SCOPE_STORE)
278
            ->willReturn($errorMessage);
279
280
        $this->assertEquals($errorMessage, $this->model->getStreetErrorMessage());
281
    }
282
}
283