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

OrderStatusesTest::provideValidOrderStatuses()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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