GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#4)
by Bob Olde
04:52
created

TaxRatesTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 312
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 312
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testSuccessfulExport() 0 8 1
A testSuccessfulImport() 0 14 1
A testImportWithForceOption() 0 14 1
A provideValidTaxRates() 0 47 1
A provideValidTaxRateDefinitions() 0 21 1
B getMockTaxRate() 0 29 1
A getMockTaxCategory() 0 13 1
A getMockTaxZone() 0 13 1
A setMockTaxRatesService() 0 15 1
A setMockTaxCategoriesService() 0 17 1
A setMockTaxZonesService() 0 15 1
A setMockDbConnection() 0 15 1
A getMockDbCommand() 0 9 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\BaseTest;
6
use Craft\Commerce_TaxCategoryModel;
7
use Craft\Commerce_TaxRateModel;
8
use Craft\Commerce_TaxZoneModel;
9
use Craft\Commerce_TaxCategoriesService;
10
use Craft\Commerce_TaxRatesService;
11
use Craft\Commerce_TaxZonesService;
12
use Craft\Craft;
13
use Craft\DbCommand;
14
use Craft\DbConnection;
15
use NerdsAndCompany\Schematic\Models\Result;
16
use PHPUnit_Framework_MockObject_MockObject as Mock;
17
18
/**
19
 * Class TaxRatesTest.
20
 *
21
 * @author    Nerds & Company
22
 * @copyright Copyright (c) 2015-2017, Nerds & Company
23
 * @license   MIT
24
 *
25
 * @see      http://www.nerds.company
26
 *
27
 * @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\TaxRates
28
 * @covers ::__construct
29
 * @covers ::<!public>
30
 */
31
class TaxRatesTest extends BaseTest
32
{
33
    //==============================================================================================================
34
    //=================================================  TESTS  ====================================================
35
    //==============================================================================================================
36
37
    /**
38
     * @covers ::export
39
     * @dataProvider provideValidTaxRates
40
     *
41
     * @param TaxRateModel[] $rates
42
     * @param array          $expectedResult
43
     */
44
    public function testSuccessfulExport(array $rates, array $expectedResult = [])
45
    {
46
        $schematicTaxRatesService = new TaxRates();
47
48
        $actualResult = $schematicTaxRatesService->export($rates);
49
50
        $this->assertSame($expectedResult, $actualResult);
51
    }
52
53
    /**
54
     * @covers ::import
55
     * @dataProvider provideValidTaxRateDefinitions
56
     *
57
     * @param array $rateDefinitions
58
     */
59
    public function testSuccessfulImport(array $rateDefinitions)
60
    {
61
        $this->setMockTaxRatesService();
62
        $this->setMockTaxCategoriesService();
63
        $this->setMockTaxZonesService();
64
        $this->setMockDbConnection();
65
66
        $schematicTaxRatesService = new TaxRates();
67
68
        $import = $schematicTaxRatesService->import($rateDefinitions);
69
70
        $this->assertInstanceOf(Result::class, $import);
71
        $this->assertFalse($import->hasErrors());
72
    }
73
74
    /**
75
     * @covers ::import
76
     * @dataProvider provideValidTaxRateDefinitions
77
     *
78
     * @param array $rateDefinitions
79
     */
80
    public function testImportWithForceOption(array $rateDefinitions)
81
    {
82
        $this->setMockTaxRatesService();
83
        $this->setMockTaxCategoriesService();
84
        $this->setMockTaxZonesService();
85
        $this->setMockDbConnection();
86
87
        $schematicTaxRatesService = new TaxRates();
88
89
        $import = $schematicTaxRatesService->import($rateDefinitions, true);
90
91
        $this->assertInstanceOf(Result::class, $import);
92
        $this->assertFalse($import->hasErrors());
93
    }
94
95
    //==============================================================================================================
96
    //==============================================  PROVIDERS  ===================================================
97
    //==============================================================================================================
98
99
    /**
100
     * @return array
101
     */
102
    public function provideValidTaxRates()
103
    {
104
        return [
105
            'single rate' => [
106
                'TaxRates' => [
107
                    'rate1' => $this->getMockTaxRate(1),
108
                ],
109
                'expectedResult' => [
110
                    'rateName1' => [
111
                        'name' => 'rateName1',
112
                        'rate' => null,
113
                        'include' => null,
114
                        'isVat' => null,
115
                        'taxable' => null,
116
                        'taxCategory' => null,
117
                        'taxZone' => null,
118
                    ],
119
                ],
120
            ],
121
            'multiple rates' => [
122
                'TaxRates' => [
123
                    'rate1' => $this->getMockTaxRate(1),
124
                    'rate2' => $this->getMockTaxRate(2),
125
                ],
126
                'expectedResult' => [
127
                    'rateName1' => [
128
                        'name' => 'rateName1',
129
                        'rate' => null,
130
                        'include' => null,
131
                        'isVat' => null,
132
                        'taxable' => null,
133
                        'taxCategory' => null,
134
                        'taxZone' => null,
135
                    ],
136
                    'rateName2' => [
137
                        'name' => 'rateName2',
138
                        'rate' => null,
139
                        'include' => null,
140
                        'isVat' => null,
141
                        'taxable' => null,
142
                        'taxCategory' => null,
143
                        'taxZone' => null,
144
                    ],
145
                ],
146
            ],
147
        ];
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    public function provideValidTaxRateDefinitions()
154
    {
155
        return [
156
            'emptyArray' => [
157
                'rateDefinitions' => [],
158
            ],
159
            'single rate' => [
160
                'rateDefinitions' => [
161
                    'rateName1' => [
162
                        'name' => 'rateName1',
163
                        'rate' => null,
164
                        'include' => null,
165
                        'isVat' => null,
166
                        'taxable' => null,
167
                        'taxCategory' => null,
168
                        'taxZone' => null,
169
                    ],
170
                ],
171
            ],
172
        ];
173
    }
174
175
    //==============================================================================================================
176
    //=================================================  MOCKS  ====================================================
177
    //==============================================================================================================
178
179
    /**
180
     * @param string $rateId
181
     *
182
     * @return Mock|Commce_TaxRateModel
183
     */
184
    private function getMockTaxRate($rateId)
185
    {
186
        $mockTaxRate = $this->getMockBuilder(Commerce_TaxRateModel::class)
187
            ->disableOriginalConstructor()
188
            ->getMock();
189
190
        $mockTaxRate->expects($this->any())
191
            ->method('__get')
192
            ->willReturnMap([
193
                ['id', $rateId],
194
                ['name', 'rateName'.$rateId],
195
            ]);
196
197
        $mockTaxRate->expects($this->any())
198
            ->method('getTaxCategory')
199
            ->willReturn($this->getMockTaxCategory($rateId));
200
201
        $mockTaxRate->expects($this->any())
202
            ->method('getTaxZone')
203
            ->willReturn($this->getMockTaxZone($rateId));
204
205
        $mockTaxRate->expects($this->any())
206
            ->method('getAllErrors')
207
            ->willReturn([
208
                'ohnoes' => 'horrible error',
209
            ]);
210
211
        return $mockTaxRate;
212
    }
213
214
    /**
215
     * @param string $categoryId
216
     *
217
     * @return Mock|Commce_TaxCategoryModel
218
     */
219
    private function getMockTaxCategory($categoryId)
220
    {
221
        $mockTaxCategory = $this->getMockBuilder(Commerce_TaxCategoryModel::class)
222
            ->disableOriginalConstructor()
223
            ->getMock();
224
225
        $mockTaxCategory->expects($this->any())
226
            ->method('__get')
227
            ->willReturnMap([
228
                ['id', $categoryId],
229
                ['handle', 'categoryHandle'.$categoryId],
230
            ]);
231
    }
232
233
    /**
234
     * @param string $zoneId
235
     *
236
     * @return Mock|Commce_TaxCategoryModel
237
     */
238
    private function getMockTaxZone($zoneId)
239
    {
240
        $mockTaxZone = $this->getMockBuilder(Commerce_TaxZoneModel::class)
241
            ->disableOriginalConstructor()
242
            ->getMock();
243
244
        $mockTaxZone->expects($this->any())
245
            ->method('__get')
246
            ->willReturnMap([
247
                ['id', $zoneId],
248
                ['name', 'zoneName'.$zoneId],
249
            ]);
250
    }
251
252
    /**
253
     * @return Mock|Commerce_TaxRatesService
254
     */
255
    private function setMockTaxRatesService()
256
    {
257
        $mockTaxRatesService = $this->getMockBuilder(Commerce_TaxRatesService::class)
258
            ->disableOriginalConstructor()
259
            ->setMethods(['getAllTaxRates', 'saveTaxRate', 'deleteTaxRateById'])
260
            ->getMock();
261
262
        $mockTaxRatesService->expects($this->any())
263
            ->method('getAllTaxRates')
264
            ->willReturn([]);
265
266
        $this->setComponent(Craft::app(), 'commerce_taxRates', $mockTaxRatesService);
267
268
        return $mockTaxRatesService;
269
    }
270
271
    /**
272
     * @return Mock|Commerce_TaxCategoriesService
273
     */
274
    private function setMockTaxCategoriesService()
275
    {
276
        $mockTaxCategoriesService = $this->getMockBuilder(Commerce_TaxCategoriesService::class)
277
            ->disableOriginalConstructor()
278
            ->setMethods(['getTaxCategoryByHandle'])
279
            ->getMock();
280
281
        $mockTaxCategoriesService->expects($this->any())
282
            ->method('getTaxCategoryByHandle')
283
            ->willReturnMap([
284
                'id' => 1,
285
            ]);
286
287
        $this->setComponent(Craft::app(), 'commerce_taxCategories', $mockTaxCategoriesService);
288
289
        return $mockTaxCategoriesService;
290
    }
291
292
    /**
293
     * @return Mock|Commerce_TaxZonesService
294
     */
295
    private function setMockTaxZonesService()
296
    {
297
        $mockTaxZonesService = $this->getMockBuilder(Commerce_TaxZonesService::class)
298
            ->disableOriginalConstructor()
299
            ->setMethods(['getAllTaxZones'])
300
            ->getMock();
301
302
        $mockTaxZonesService->expects($this->any())
303
            ->method('getAllTaxZones')
304
            ->willReturn([]);
305
306
        $this->setComponent(Craft::app(), 'commerce_taxZones', $mockTaxZonesService);
307
308
        return $mockTaxZonesService;
309
    }
310
311
    /**
312
     * @return Mock|DbConnection
313
     */
314
    private function setMockDbConnection()
315
    {
316
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
317
            ->disableOriginalConstructor()
318
            ->setMethods(['createCommand'])
319
            ->getMock();
320
        $mockDbConnection->autoConnect = false; // Do not auto connect
321
322
        $mockDbCommand = $this->getMockDbCommand();
323
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
324
325
        Craft::app()->setComponent('db', $mockDbConnection);
326
327
        return $mockDbConnection;
328
    }
329
330
    /**
331
     * @return Mock|DbCommand
332
     */
333
    private function getMockDbCommand()
334
    {
335
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
336
            ->disableOriginalConstructor()
337
            ->setMethods(['insertOrUpdate'])
338
            ->getMock();
339
340
        return $mockDbCommand;
341
    }
342
}
343