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
07:04
created

TaxRatesTest::getMockTaxRate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 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' => 'categoryHandle1',
117
                        'taxZone' => 'zoneName1',
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' => 'categoryHandle1',
134
                        'taxZone' => 'zoneName1',
135
                    ],
136
                    'rateName2' => [
137
                        'name' => 'rateName2',
138
                        'rate' => null,
139
                        'include' => null,
140
                        'isVat' => null,
141
                        'taxable' => null,
142
                        'taxCategory' => 'categoryHandle1',
143
                        'taxZone' => 'zoneName1',
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' => 'categoryHandle1',
168
                        'taxZone' => 'zoneName1',
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
            ->setMethods(['__get', 'getTaxCategory', 'getTaxZone', 'getAllErrors'])
189
            ->getMock();
190
191
        $mockTaxRate->expects($this->any())
192
            ->method('__get')
193
            ->willReturnMap([
194
                ['id', $rateId],
195
                ['name', 'rateName'.$rateId],
196
            ]);
197
198
        $mockTaxRate->expects($this->any())
199
            ->method('getTaxCategory')
200
            ->willReturn($this->getMockTaxCategory($rateId));
201
202
        $mockTaxRate->expects($this->any())
203
            ->method('getTaxZone')
204
            ->willReturn($this->getMockTaxZone($rateId));
205
206
        $mockTaxRate->expects($this->any())
207
            ->method('getAllErrors')
208
            ->willReturn([
209
                'ohnoes' => 'horrible error',
210
            ]);
211
212
        return $mockTaxRate;
213
    }
214
215
    /**
216
     * @param string $categoryId
217
     *
218
     * @return Mock|Commce_TaxCategoryModel
219
     */
220
    private function getMockTaxCategory($categoryId)
221
    {
222
        $mockTaxCategory = $this->getMockBuilder(Commerce_TaxCategoryModel::class)
223
            ->disableOriginalConstructor()
224
            ->getMock();
225
226
        $mockTaxCategory->expects($this->any())
227
            ->method('__get')
228
            ->willReturnMap([
229
                ['id', $categoryId],
230
                ['handle', 'categoryHandle'.$categoryId],
231
            ]);
232
    }
233
234
    /**
235
     * @param string $zoneId
236
     *
237
     * @return Mock|Commce_TaxZoneModel
238
     */
239
    private function getMockTaxZone($zoneId)
240
    {
241
        $mockTaxZone = $this->getMockBuilder(Commerce_TaxZoneModel::class)
242
            ->disableOriginalConstructor()
243
            ->getMock();
244
245
        $mockTaxZone->expects($this->any())
246
            ->method('__get')
247
            ->willReturnMap([
248
                ['id', $zoneId],
249
                ['name', 'zoneName'.$zoneId],
250
            ]);
251
    }
252
253
    /**
254
     * @return Mock|Commerce_TaxRatesService
255
     */
256
    private function setMockTaxRatesService()
257
    {
258
        $mockTaxRatesService = $this->getMockBuilder(Commerce_TaxRatesService::class)
259
            ->disableOriginalConstructor()
260
            ->setMethods(['getAllTaxRates', 'saveTaxRate', 'deleteTaxRateById'])
261
            ->getMock();
262
263
        $mockTaxRatesService->expects($this->any())
264
            ->method('getAllTaxRates')
265
            ->willReturn([]);
266
267
        $this->setComponent(Craft::app(), 'commerce_taxRates', $mockTaxRatesService);
268
269
        return $mockTaxRatesService;
270
    }
271
272
    /**
273
     * @return Mock|Commerce_TaxCategoriesService
274
     */
275
    private function setMockTaxCategoriesService()
276
    {
277
        $mockTaxCategoriesService = $this->getMockBuilder(Commerce_TaxCategoriesService::class)
278
            ->disableOriginalConstructor()
279
            ->setMethods(['getTaxCategoryByHandle'])
280
            ->getMock();
281
282
        $mockTaxCategoriesService->expects($this->any())
283
            ->method('getTaxCategoryByHandle')
284
            ->willReturn($this->getMockTaxCategory(1));
285
286
        $this->setComponent(Craft::app(), 'commerce_taxCategories', $mockTaxCategoriesService);
287
288
        return $mockTaxCategoriesService;
289
    }
290
291
    /**
292
     * @return Mock|Commerce_TaxZonesService
293
     */
294
    private function setMockTaxZonesService()
295
    {
296
        $mockTaxZonesService = $this->getMockBuilder(Commerce_TaxZonesService::class)
297
            ->disableOriginalConstructor()
298
            ->setMethods(['getAllTaxZones'])
299
            ->getMock();
300
301
        $mockTaxZonesService->expects($this->any())
302
            ->method('getAllTaxZones')
303
            ->willReturn([]);
304
305
        $this->setComponent(Craft::app(), 'commerce_taxZones', $mockTaxZonesService);
306
307
        return $mockTaxZonesService;
308
    }
309
310
    /**
311
     * @return Mock|DbConnection
312
     */
313
    private function setMockDbConnection()
314
    {
315
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
316
            ->disableOriginalConstructor()
317
            ->setMethods(['createCommand'])
318
            ->getMock();
319
        $mockDbConnection->autoConnect = false; // Do not auto connect
320
321
        $mockDbCommand = $this->getMockDbCommand();
322
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
323
324
        Craft::app()->setComponent('db', $mockDbConnection);
325
326
        return $mockDbConnection;
327
    }
328
329
    /**
330
     * @return Mock|DbCommand
331
     */
332
    private function getMockDbCommand()
333
    {
334
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
335
            ->disableOriginalConstructor()
336
            ->setMethods(['insertOrUpdate'])
337
            ->getMock();
338
339
        return $mockDbCommand;
340
    }
341
}
342