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 (#6)
by Bart
03:24
created

OrderStatusesTest::setMockEmailsService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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\Commerce_EmailsService;
9
use Craft\Craft;
10
use Craft\DbCommand;
11
use Craft\DbConnection;
12
use NerdsAndCompany\Schematic\Models\Result;
13
use PHPUnit_Framework_MockObject_MockObject as Mock;
14
15
/**
16
 * Class OrderStatusesTest.
17
 *
18
 * @author    Nerds & Company
19
 * @copyright Copyright (c) 2015-2017, Nerds & Company
20
 * @license   MIT
21
 *
22
 * @see      http://www.nerds.company
23
 *
24
 * @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\OrderStatuses
25
 * @covers ::__construct
26
 * @covers ::<!public>
27
 */
28
class OrderStatusesTest extends BaseTest
29
{
30
    //==============================================================================================================
31
    //=================================================  TESTS  ====================================================
32
    //==============================================================================================================
33
34
    /**
35
     * @covers ::export
36
     * @dataProvider provideValidOrderStatuses
37
     *
38
     * @param OrderStatusModel[] $types
39
     * @param array              $expectedResult
40
     */
41
    public function testSuccessfulExport(array $types, array $expectedResult = [])
42
    {
43
        $schematicOrderStatusesService = new OrderStatuses();
44
45
        $actualResult = $schematicOrderStatusesService->export($types);
46
47
        $this->assertSame($expectedResult, $actualResult);
48
    }
49
50
    /**
51
     * @covers ::import
52
     * @dataProvider provideValidOrderStatusDefinitions
53
     *
54
     * @param array $statusDefinitions
55
     */
56
    public function testSuccessfulImport(array $statusDefinitions)
57
    {
58
        $this->setMockOrderStatusesService();
59
        $this->setMockEmailsService();
60
        $this->setMockDbConnection();
61
62
        $schematicOrderStatusesService = new OrderStatuses();
63
64
        $import = $schematicOrderStatusesService->import($statusDefinitions);
65
66
        $this->assertInstanceOf(Result::class, $import);
67
        $this->assertFalse($import->hasErrors());
68
    }
69
70
    /**
71
     * @covers ::import
72
     * @dataProvider provideValidOrderStatusDefinitions
73
     *
74
     * @param array $statusDefinitions
75
     */
76
    public function testImportWithForceOption(array $statusDefinitions)
77
    {
78
        $this->setMockOrderStatusesService();
79
        $this->setMockEmailsService();
80
        $this->setMockDbConnection();
81
82
        $schematicOrderStatusesService = new OrderStatuses();
83
84
        $import = $schematicOrderStatusesService->import($statusDefinitions, true);
85
86
        $this->assertInstanceOf(Result::class, $import);
87
        $this->assertFalse($import->hasErrors());
88
    }
89
90
    //==============================================================================================================
91
    //==============================================  PROVIDERS  ===================================================
92
    //==============================================================================================================
93
94
    /**
95
     * @return array
96
     */
97
    public function provideValidOrderStatuses()
98
    {
99
        return [
100
            'single status' => [
101
                'OrderStatuses' => [
102
                    'status1' => $this->getMockOrderStatus(1),
103
                ],
104
                'expectedResult' => [
105
                    'statusHandle1' => [
106
                        'name' => 'statusName1',
107
                        'color' => null,
108
                        'sortOrder' => null,
109
                        'default' => null,
110
                    ],
111
                ],
112
            ],
113
            'multiple statuses' => [
114
                'OrderStatuses' => [
115
                    'status1' => $this->getMockOrderStatus(1),
116
                    'status2' => $this->getMockOrderStatus(2),
117
                ],
118
                'expectedResult' => [
119
                    'statusHandle1' => [
120
                        'name' => 'statusName1',
121
                        'color' => null,
122
                        'sortOrder' => null,
123
                        'default' => null,
124
                    ],
125
                    'statusHandle2' => [
126
                        'name' => 'statusName2',
127
                        'color' => null,
128
                        'sortOrder' => null,
129
                        'default' => null,
130
                    ],
131
                ],
132
            ],
133
        ];
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function provideValidOrderStatusDefinitions()
140
    {
141
        return [
142
            'emptyArray' => [
143
                'statusDefinitions' => [],
144
            ],
145
            'single type' => [
146
                'statusDefinitions' => [
147
                    'statusHandle1' => [
148
                        'name' => 'statusName1',
149
                        'color' => null,
150
                        'sortOrder' => null,
151
                        'default' => null,
152
                    ],
153
                ],
154
            ],
155
        ];
156
    }
157
158
    //==============================================================================================================
159
    //=================================================  MOCKS  ====================================================
160
    //==============================================================================================================
161
162
    /**
163
     * @param string $statusId
164
     *
165
     * @return Mock|Commerce_OrderStatusModel
166
     */
167
    private function getMockOrderStatus($statusId)
168
    {
169
        $mockOrderStatus = $this->getMockBuilder(Commerce_OrderStatusModel::class)
170
            ->disableOriginalConstructor()
171
            ->getMock();
172
173
        $mockOrderStatus->expects($this->any())
174
            ->method('__get')
175
            ->willReturnMap([
176
                ['id', $statusId],
177
                ['handle', 'statusHandle'.$statusId],
178
                ['name', 'statusName'.$statusId],
179
            ]);
180
181
        $mockOrderStatus->expects($this->any())
182
            ->method('getAllErrors')
183
            ->willReturn([
184
                'ohnoes' => 'horrible error',
185
            ]);
186
187
        $mockOrderStatus->expects($this->any())
188
            ->method('getEmails')
189
            ->willReturn([]);
190
191
        return $mockOrderStatus;
192
    }
193
194
    /**
195
     * @return Mock|Commerce_OrderStatusesService
196
     */
197
    private function setMockOrderStatusesService()
198
    {
199
        $mockOrderStatusesService = $this->getMockBuilder(Commerce_OrderStatusesService::class)
200
            ->disableOriginalConstructor()
201
            ->setMethods(['getAllOrderStatuses', 'saveOrderStatus', 'deleteOrderStatusById'])
202
            ->getMock();
203
204
        $mockOrderStatusesService->expects($this->any())
205
            ->method('getAllOrderStatuses')
206
            ->willReturn([]);
207
208
        $this->setComponent(Craft::app(), 'commerce_orderStatuses', $mockOrderStatusesService);
209
210
        return $mockOrderStatusesService;
211
    }
212
213
    /**
214
     * @return Mock|Commerce_EmailsService
215
     */
216
    private function setMockEmailsService()
217
    {
218
        $mockEmailsService = $this->getMockBuilder(Commerce_EmailsService::class)
219
            ->disableOriginalConstructor()
220
            ->setMethods(['getAllEmails', 'saveEmail', 'deleteEmailById'])
221
            ->getMock();
222
223
        $mockEmailsService->expects($this->any())
224
            ->method('getAllEmails')
225
            ->willReturn([]);
226
227
        $this->setComponent(Craft::app(), 'commerce_emails', $mockEmailsService);
228
229
        return $mockEmailsService;
230
    }
231
232
    /**
233
     * @return Mock|DbConnection
234
     */
235
    private function setMockDbConnection()
236
    {
237
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
238
            ->disableOriginalConstructor()
239
            ->setMethods(['createCommand'])
240
            ->getMock();
241
        $mockDbConnection->autoConnect = false; // Do not auto connect
242
243
        $mockDbCommand = $this->getMockDbCommand();
244
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
245
246
        Craft::app()->setComponent('db', $mockDbConnection);
247
248
        return $mockDbConnection;
249
    }
250
251
    /**
252
     * @return Mock|DbCommand
253
     */
254
    private function getMockDbCommand()
255
    {
256
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
257
            ->disableOriginalConstructor()
258
            ->setMethods(['insertOrUpdate'])
259
            ->getMock();
260
261
        return $mockDbCommand;
262
    }
263
}
264