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
02:36
created

TaxRatesTest::getMockDbCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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