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

ShippingZonesTest::provideValidShippingZones()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 32
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\BaseTest;
6
use Craft\Commerce_ShippingZoneModel;
7
use Craft\Commerce_ShippingZonesService;
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 ShippingZonesTest.
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\ShippingZones
24
 * @covers ::__construct
25
 * @covers ::<!public>
26
 */
27
class ShippingZonesTest extends BaseTest
28
{
29
    //==============================================================================================================
30
    //=================================================  TESTS  ====================================================
31
    //==============================================================================================================
32
33
    /**
34
     * @covers ::export
35
     * @dataProvider provideValidShippingZones
36
     *
37
     * @param ShippingZoneModel[] $zones
38
     * @param array               $expectedResult
39
     */
40
    public function testSuccessfulExport(array $zones, array $expectedResult = [])
41
    {
42
        $schematicShippingZonesService = new ShippingZones();
43
44
        $actualResult = $schematicShippingZonesService->export($zones);
45
46
        $this->assertSame($expectedResult, $actualResult);
47
    }
48
49
    /**
50
     * @covers ::import
51
     * @dataProvider provideValidShippingZoneDefinitions
52
     *
53
     * @param array $zoneDefinitions
54
     */
55
    public function testSuccessfulImport(array $zoneDefinitions)
56
    {
57
        $this->setMockShippingZonesService();
58
        $this->setMockDbConnection();
59
60
        $schematicShippingZonesService = new ShippingZones();
61
62
        $import = $schematicShippingZonesService->import($zoneDefinitions);
63
64
        $this->assertInstanceOf(Result::class, $import);
65
        $this->assertFalse($import->hasErrors());
66
    }
67
68
    /**
69
     * @covers ::import
70
     * @dataProvider provideValidShippingZoneDefinitions
71
     *
72
     * @param array $zoneDefinitions
73
     */
74
    public function testImportWithForceOption(array $zoneDefinitions)
75
    {
76
        $this->setMockShippingZonesService();
77
        $this->setMockDbConnection();
78
79
        $schematicShippingZonesService = new ShippingZones();
80
81
        $import = $schematicShippingZonesService->import($zoneDefinitions, 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 provideValidShippingZones()
95
    {
96
        return [
97
            'single zone' => [
98
                'ShippingZones' => [
99
                    'zone1' => $this->getMockShippingZone(1),
100
                ],
101
                'expectedResult' => [
102
                    'zoneName1' => [
103
                        'name' => 'zoneName1',
104
                        'description' => null,
105
                        'countryBased' => null,
106
                        'default' => null,
107
                        'countries' => array(),
108
                        'states' => array(),
109
                    ],
110
                ],
111
            ],
112
            'multiple zones' => [
113
                'ShippingZones' => [
114
                    'zone1' => $this->getMockShippingZone(1),
115
                    'zone2' => $this->getMockShippingZone(2),
116
                ],
117
                'expectedResult' => [
118
                    'zoneName1' => [
119
                        'name' => 'zoneName1',
120
                        'description' => null,
121
                        'countryBased' => null,
122
                        'default' => null,
123
                        'countries' => array(),
124
                        'states' => array(),
125
                    ],
126
                    'zoneName2' => [
127
                        'name' => 'zoneName2',
128
                        'description' => null,
129
                        'countryBased' => null,
130
                        'default' => null,
131
                        'countries' => array(),
132
                        'states' => array(),
133
                    ],
134
                ],
135
            ],
136
        ];
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function provideValidShippingZoneDefinitions()
143
    {
144
        return [
145
            'emptyArray' => [
146
                'zoneDefinitions' => [],
147
            ],
148
            'single zone' => [
149
                'zoneDefinitions' => [
150
                    'zoneName1' => [
151
                        'name' => 'zoneName1',
152
                        'description' => null,
153
                        'countryBased' => null,
154
                        'default' => null,
155
                        'countries' => array(),
156
                        'states' => array(),
157
                    ],
158
                ],
159
            ],
160
        ];
161
    }
162
163
    //==============================================================================================================
164
    //=================================================  MOCKS  ====================================================
165
    //==============================================================================================================
166
167
    /**
168
     * @param string $zoneId
169
     *
170
     * @return Mock|ShippingZoneModel
171
     */
172
    private function getMockShippingZone($zoneId)
173
    {
174
        $mockShippingZone = $this->getMockBuilder(Commerce_ShippingZoneModel::class)
175
            ->disableOriginalConstructor()
176
            ->getMock();
177
178
        $mockShippingZone->expects($this->any())
179
            ->method('__get')
180
            ->willReturnMap([
181
                ['id', $zoneId],
182
                ['name', 'zoneName'.$zoneId],
183
            ]);
184
185
        $mockShippingZone->expects($this->any())
186
            ->method('getCountries')
187
            ->willReturn([]);
188
189
        $mockShippingZone->expects($this->any())
190
            ->method('getStates')
191
            ->willReturn([]);
192
193
        $mockShippingZone->expects($this->any())
194
            ->method('getAllErrors')
195
            ->willReturn([
196
                'ohnoes' => 'horrible error',
197
            ]);
198
199
        return $mockShippingZone;
200
    }
201
202
    /**
203
     * @return Mock|CategoriesService
204
     */
205
    private function setMockShippingZonesService()
206
    {
207
        $mockShippingZonesService = $this->getMockBuilder(Commerce_ShippingZonesService::class)
208
            ->disableOriginalConstructor()
209
            ->setMethods(['getAllShippingZones', 'saveShippingZone', 'deleteShippingZoneById'])
210
            ->getMock();
211
212
        $mockShippingZonesService->expects($this->any())
213
            ->method('getAllShippingZones')
214
            ->willReturn([]);
215
216
        $this->setComponent(Craft::app(), 'commerce_shippingZones', $mockShippingZonesService);
217
218
        return $mockShippingZonesService;
219
    }
220
221
    /**
222
     * @return Mock|DbConnection
223
     */
224
    private function setMockDbConnection()
225
    {
226
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
227
            ->disableOriginalConstructor()
228
            ->setMethods(['createCommand'])
229
            ->getMock();
230
        $mockDbConnection->autoConnect = false; // Do not auto connect
231
232
        $mockDbCommand = $this->getMockDbCommand();
233
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
234
235
        Craft::app()->setComponent('db', $mockDbConnection);
236
237
        return $mockDbConnection;
238
    }
239
240
    /**
241
     * @return Mock|DbCommand
242
     */
243
    private function getMockDbCommand()
244
    {
245
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
246
            ->disableOriginalConstructor()
247
            ->setMethods(['insertOrUpdate'])
248
            ->getMock();
249
250
        return $mockDbCommand;
251
    }
252
}
253