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
Push — master ( a15255...979e92 )
by Bob Olde
10s
created

StatesTest::getMockState()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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