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

provideValidShippingCategories()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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