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

ShippingMethodsTest::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_ShippingMethodModel;
7
use Craft\Commerce_ShippingMethodsService;
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 ShippingMethodsTest.
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\ShippingMethods
24
 * @covers ::__construct
25
 * @covers ::<!public>
26
 */
27
class ShippingMethodsTest extends BaseTest
28
{
29
    //==============================================================================================================
30
    //=================================================  TESTS  ====================================================
31
    //==============================================================================================================
32
33
    /**
34
     * @covers ::export
35
     * @dataProvider provideValidShippingMethods
36
     *
37
     * @param ShippingMethodModel[] $methods
38
     * @param array                 $expectedResult
39
     */
40
    public function testSuccessfulExport(array $methods, array $expectedResult = [])
41
    {
42
        $schematicShippingMethodsService = new ShippingMethods();
43
44
        $actualResult = $schematicShippingMethodsService->export($methods);
45
46
        $this->assertSame($expectedResult, $actualResult);
47
    }
48
49
    /**
50
     * @covers ::import
51
     * @dataProvider provideValidShippingMethodDefinitions
52
     *
53
     * @param array $methodDefinitions
54
     */
55
    public function testSuccessfulImport(array $methodDefinitions)
56
    {
57
        $this->setMockShippingMethodsService();
58
        $this->setMockDbConnection();
59
60
        $schematicShippingMethodsService = new ShippingMethods();
61
62
        $import = $schematicShippingMethodsService->import($methodDefinitions);
63
64
        $this->assertInstanceOf(Result::class, $import);
65
        $this->assertFalse($import->hasErrors());
66
    }
67
68
    /**
69
     * @covers ::import
70
     * @dataProvider provideValidShippingMethodDefinitions
71
     *
72
     * @param array $methodDefinitions
73
     */
74
    public function testImportWithForceOption(array $methodDefinitions)
75
    {
76
        $this->setMockShippingMethodsService();
77
        $this->setMockDbConnection();
78
79
        $schematicShippingMethodsService = new ShippingMethods();
80
81
        $import = $schematicShippingMethodsService->import($methodDefinitions, 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 provideValidShippingMethods()
95
    {
96
        return [
97
            'single method' => [
98
                'ShippingMethods' => [
99
                    'method1' => $this->getMockShippingMethod(1),
100
                ],
101
                'expectedResult' => [
102
                    'methodHandle1' => [
103
                        'name' => 'methodName1',
104
                        'enabled' => null,
105
                        'rules' => null,
106
                    ],
107
                ],
108
            ],
109
            'multiple methods' => [
110
                'ShippingMethods' => [
111
                    'method1' => $this->getMockShippingMethod(1),
112
                    'method2' => $this->getMockShippingMethod(2),
113
                ],
114
                'expectedResult' => [
115
                    'methodHandle1' => [
116
                        'name' => 'methodName1',
117
                        'enabled' => null,
118
                        'rules' => null,
119
                    ],
120
                    'methodHandle2' => [
121
                        'name' => 'methodName2',
122
                        'enabled' => null,
123
                        'rules' => null,
124
                    ],
125
                ],
126
            ],
127
        ];
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function provideValidShippingMethodDefinitions()
134
    {
135
        return [
136
            'emptyArray' => [
137
                'methodDefinitions' => [],
138
            ],
139
            'single method' => [
140
                'methodDefinitions' => [
141
                    'methodHandle1' => [
142
                        'name' => 'methodName1',
143
                        'enabled' => null,
144
                        'rules' => null,
145
                    ],
146
                ],
147
            ],
148
        ];
149
    }
150
151
    //==============================================================================================================
152
    //=================================================  MOCKS  ====================================================
153
    //==============================================================================================================
154
155
    /**
156
     * @param string $methodId
157
     *
158
     * @return Mock|ShippingMethodModel
159
     */
160
    private function getMockShippingMethod($methodId)
161
    {
162
        $mockShippingMethod = $this->getMockBuilder(Commerce_ShippingMethodModel::class)
163
            ->disableOriginalConstructor()
164
            ->getMock();
165
166
        $mockShippingMethod->expects($this->any())
167
            ->method('__get')
168
            ->willReturnMap([
169
                ['id', $methodId],
170
                ['handle', 'methodHandle'.$methodId],
171
                ['name', 'methodName'.$methodId],
172
            ]);
173
174
        $mockShippingMethod->expects($this->any())
175
            ->method('getAllErrors')
176
            ->willReturn([
177
                'ohnoes' => 'horrible error',
178
            ]);
179
180
        return $mockShippingMethod;
181
    }
182
183
    /**
184
     * @return Mock|CategoriesService
185
     */
186
    private function setMockShippingMethodsService()
187
    {
188
        $mockShippingMethodsService = $this->getMockBuilder(Commerce_ShippingMethodsService::class)
189
            ->disableOriginalConstructor()
190
            ->setMethods(['getAllShippingMethods', 'saveShippingMethod', 'delete'])
191
            ->getMock();
192
193
        $mockShippingMethodsService->expects($this->any())
194
            ->method('getAllShippingMethods')
195
            ->willReturn([]);
196
197
        $this->setComponent(Craft::app(), 'commerce_shippingMethods', $mockShippingMethodsService);
198
199
        return $mockShippingMethodsService;
200
    }
201
202
    /**
203
     * @return Mock|DbConnection
204
     */
205
    private function setMockDbConnection()
206
    {
207
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
208
            ->disableOriginalConstructor()
209
            ->setMethods(['createCommand'])
210
            ->getMock();
211
        $mockDbConnection->autoConnect = false; // Do not auto connect
212
213
        $mockDbCommand = $this->getMockDbCommand();
214
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
215
216
        Craft::app()->setComponent('db', $mockDbConnection);
217
218
        return $mockDbConnection;
219
    }
220
221
    /**
222
     * @return Mock|DbCommand
223
     */
224
    private function getMockDbCommand()
225
    {
226
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
227
            ->disableOriginalConstructor()
228
            ->setMethods(['insertOrUpdate'])
229
            ->getMock();
230
231
        return $mockDbCommand;
232
    }
233
}
234