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:15
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->setMockDbConnection();
59
60
        $schematicTaxRatesService = new TaxRates();
61
62
        $import = $schematicTaxRatesService->import($rateDefinitions);
63
64
        $this->assertInstanceOf(Result::class, $import);
65
        $this->assertFalse($import->hasErrors());
66
    }
67
68
    /**
69
     * @covers ::import
70
     * @dataProvider provideValidTaxRateDefinitions
71
     *
72
     * @param array $rateDefinitions
73
     */
74
    public function testImportWithForceOption(array $rateDefinitions)
75
    {
76
        $this->setMockTaxRatesService();
77
        $this->setMockDbConnection();
78
79
        $schematicTaxRatesService = new TaxRates();
80
81
        $import = $schematicTaxRatesService->import($rateDefinitions, true);
82
83
        $this->assertInstanceOf(Result::class, $import);
84
        $this->assertFalse($import->hasErrors());
85
    }
86
87
    //==============================================================================================================
88
    //==============================================  PROVIDERS  ===================================================
89
    //==============================================================================================================
90
91
    /**
92
     * @return array
93
     */
94
    public function provideValidTaxRates()
95
    {
96
        return [
97
            'single rate' => [
98
                'TaxRates' => [
99
                    'rate1' => $this->getMockTaxRate(1),
100
                ],
101
                'expectedResult' => [
102
                    'rateName1' => [
103
                        'name' => 'rateName1',
104
                        'rate' => null,
105
                        'include' => null,
106
                        'isVat' => null,
107
                        'taxable' => null,
108
                        'taxRate' => null,
109
                        'taxZone' => null,
110
                    ],
111
                ],
112
            ],
113
            'multiple rates' => [
114
                'TaxRates' => [
115
                    'rate1' => $this->getMockTaxRate(1),
116
                    'rate2' => $this->getMockTaxRate(2),
117
                ],
118
                'expectedResult' => [
119
                    'rateName1' => [
120
                        'name' => 'rateName1',
121
                        'rate' => null,
122
                        'include' => null,
123
                        'isVat' => null,
124
                        'taxable' => null,
125
                        'taxRate' => null,
126
                        'taxZone' => null,
127
                    ],
128
                    'rateName2' => [
129
                        'name' => 'rateName2',
130
                        'rate' => null,
131
                        'include' => null,
132
                        'isVat' => null,
133
                        'taxable' => null,
134
                        'taxRate' => null,
135
                        'taxZone' => null,
136
                    ],
137
                ],
138
            ],
139
        ];
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function provideValidTaxRateDefinitions()
146
    {
147
        return [
148
            'emptyArray' => [
149
                'rateDefinitions' => [],
150
            ],
151
            'single rate' => [
152
                'rateDefinitions' => [
153
                    'rateName1' => [
154
                        'name' => 'rateName1',
155
                        'rate' => null,
156
                        'include' => null,
157
                        'isVat' => null,
158
                        'taxable' => null,
159
                        'taxRate' => null,
160
                        'taxZone' => null,
161
                    ],
162
                ],
163
            ],
164
        ];
165
    }
166
167
    //==============================================================================================================
168
    //=================================================  MOCKS  ====================================================
169
    //==============================================================================================================
170
171
    /**
172
     * @param string $rateId
173
     *
174
     * @return Mock|TaxRateModel
175
     */
176
    private function getMockTaxRate($rateId)
177
    {
178
        $mockTaxRate = $this->getMockBuilder(Commerce_TaxRateModel::class)
179
            ->disableOriginalConstructor()
180
            ->getMock();
181
182
        $mockTaxRate->expects($this->any())
183
            ->method('__get')
184
            ->willReturnMap([
185
                ['id', $rateId],
186
                ['name', 'rateName'.$rateId],
187
            ]);
188
189
        $mockTaxRate->expects($this->any())
190
            ->method('getAllErrors')
191
            ->willReturn([
192
                'ohnoes' => 'horrible error',
193
            ]);
194
195
        return $mockTaxRate;
196
    }
197
198
    /**
199
     * @return Mock|RatesService
200
     */
201
    private function setMockTaxRatesService()
202
    {
203
        $mockTaxRatesService = $this->getMockBuilder(Commerce_TaxRatesService::class)
204
            ->disableOriginalConstructor()
205
            ->setMethods(['getAllTaxRates', 'saveTaxRate', 'deleteTaxRateById'])
206
            ->getMock();
207
208
        $mockTaxRatesService->expects($this->any())
209
            ->method('getAllTaxRates')
210
            ->willReturn([]);
211
212
        $this->setComponent(Craft::app(), 'commerce_taxRates', $mockTaxRatesService);
213
214
        return $mockTaxRatesService;
215
    }
216
217
    /**
218
     * @return Mock|DbConnection
219
     */
220
    private function setMockDbConnection()
221
    {
222
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
223
            ->disableOriginalConstructor()
224
            ->setMethods(['createCommand'])
225
            ->getMock();
226
        $mockDbConnection->autoConnect = false; // Do not auto connect
227
228
        $mockDbCommand = $this->getMockDbCommand();
229
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
230
231
        Craft::app()->setComponent('db', $mockDbConnection);
232
233
        return $mockDbConnection;
234
    }
235
236
    /**
237
     * @return Mock|DbCommand
238
     */
239
    private function getMockDbCommand()
240
    {
241
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
242
            ->disableOriginalConstructor()
243
            ->setMethods(['insertOrUpdate'])
244
            ->getMock();
245
246
        return $mockDbCommand;
247
    }
248
}
249