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:28
created

TaxRatesTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 257
rs 10
c 0
b 0
f 0

10 Methods

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