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->setMockEmailsService(); |
59
|
|
|
$this->setMockDbConnection(); |
60
|
|
|
|
61
|
|
|
$schematicOrderStatusesService = new OrderStatuses(); |
62
|
|
|
|
63
|
|
|
$import = $schematicOrderStatusesService->import($statusDefinitions); |
64
|
|
|
|
65
|
|
|
$this->assertInstanceOf(Result::class, $import); |
66
|
|
|
$this->assertFalse($import->hasErrors()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @covers ::import |
71
|
|
|
* @dataProvider provideValidOrderStatusDefinitions |
72
|
|
|
* |
73
|
|
|
* @param array $statusDefinitions |
74
|
|
|
*/ |
75
|
|
|
public function testImportWithForceOption(array $statusDefinitions) |
76
|
|
|
{ |
77
|
|
|
$this->setMockOrderStatusesService(); |
78
|
|
|
$this->setMockEmailsService(); |
79
|
|
|
$this->setMockDbConnection(); |
80
|
|
|
|
81
|
|
|
$schematicOrderStatusesService = new OrderStatuses(); |
82
|
|
|
|
83
|
|
|
$import = $schematicOrderStatusesService->import($statusDefinitions, true); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(Result::class, $import); |
86
|
|
|
$this->assertFalse($import->hasErrors()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//============================================================================================================== |
90
|
|
|
//============================================== PROVIDERS =================================================== |
91
|
|
|
//============================================================================================================== |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function provideValidOrderStatuses() |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'single status' => [ |
100
|
|
|
'OrderStatuses' => [ |
101
|
|
|
'status1' => $this->getMockOrderStatus(1), |
102
|
|
|
], |
103
|
|
|
'expectedResult' => [ |
104
|
|
|
'statusHandle1' => [ |
105
|
|
|
'name' => 'statusName1', |
106
|
|
|
'color' => null, |
107
|
|
|
'sortOrder' => null, |
108
|
|
|
'default' => null, |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'multiple statuses' => [ |
113
|
|
|
'OrderStatuses' => [ |
114
|
|
|
'status1' => $this->getMockOrderStatus(1), |
115
|
|
|
'status2' => $this->getMockOrderStatus(2), |
116
|
|
|
], |
117
|
|
|
'expectedResult' => [ |
118
|
|
|
'statusHandle1' => [ |
119
|
|
|
'name' => 'statusName1', |
120
|
|
|
'color' => null, |
121
|
|
|
'sortOrder' => null, |
122
|
|
|
'default' => null, |
123
|
|
|
], |
124
|
|
|
'statusHandle2' => [ |
125
|
|
|
'name' => 'statusName2', |
126
|
|
|
'color' => null, |
127
|
|
|
'sortOrder' => null, |
128
|
|
|
'default' => null, |
129
|
|
|
], |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function provideValidOrderStatusDefinitions() |
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
|
|
'emptyArray' => [ |
142
|
|
|
'statusDefinitions' => [], |
143
|
|
|
], |
144
|
|
|
'single type' => [ |
145
|
|
|
'statusDefinitions' => [ |
146
|
|
|
'statusHandle1' => [ |
147
|
|
|
'name' => 'statusName1', |
148
|
|
|
'color' => null, |
149
|
|
|
'sortOrder' => null, |
150
|
|
|
'default' => null, |
151
|
|
|
], |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
//============================================================================================================== |
158
|
|
|
//================================================= MOCKS ==================================================== |
159
|
|
|
//============================================================================================================== |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $statusId |
163
|
|
|
* |
164
|
|
|
* @return Mock|Commerce_OrderStatusModel |
165
|
|
|
*/ |
166
|
|
|
private function getMockOrderStatus($statusId) |
167
|
|
|
{ |
168
|
|
|
$mockOrderStatus = $this->getMockBuilder(Commerce_OrderStatusModel::class) |
169
|
|
|
->disableOriginalConstructor() |
170
|
|
|
->getMock(); |
171
|
|
|
|
172
|
|
|
$mockOrderStatus->expects($this->any()) |
173
|
|
|
->method('__get') |
174
|
|
|
->willReturnMap([ |
175
|
|
|
['id', $statusId], |
176
|
|
|
['handle', 'statusHandle'.$statusId], |
177
|
|
|
['name', 'statusName'.$statusId], |
178
|
|
|
]); |
179
|
|
|
|
180
|
|
|
$mockOrderStatus->expects($this->any()) |
181
|
|
|
->method('getAllErrors') |
182
|
|
|
->willReturn([ |
183
|
|
|
'ohnoes' => 'horrible error', |
184
|
|
|
]); |
185
|
|
|
|
186
|
|
|
$mockOrderStatus->expects($this->any()) |
187
|
|
|
->method('getEmails') |
188
|
|
|
->willReturn([]); |
189
|
|
|
|
190
|
|
|
return $mockOrderStatus; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return Mock|Commerce_OrderStatusesService |
195
|
|
|
*/ |
196
|
|
|
private function setMockOrderStatusesService() |
197
|
|
|
{ |
198
|
|
|
$mockOrderStatusesService = $this->getMockBuilder(Commerce_OrderStatusesService::class) |
199
|
|
|
->disableOriginalConstructor() |
200
|
|
|
->setMethods(['getAllOrderStatuses', 'saveOrderStatus', 'deleteOrderStatusById']) |
201
|
|
|
->getMock(); |
202
|
|
|
|
203
|
|
|
$mockOrderStatusesService->expects($this->any()) |
204
|
|
|
->method('getAllOrderStatuses') |
205
|
|
|
->willReturn([]); |
206
|
|
|
|
207
|
|
|
$this->setComponent(Craft::app(), 'commerce_orderStatuses', $mockOrderStatusesService); |
208
|
|
|
|
209
|
|
|
return $mockOrderStatusesService; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return Mock|Commerce_EmailsService |
214
|
|
|
*/ |
215
|
|
|
private function setMockEmailsService() |
216
|
|
|
{ |
217
|
|
|
$mockEmailsService = $this->getMockBuilder(Commerce_EmailsService::class) |
218
|
|
|
->disableOriginalConstructor() |
219
|
|
|
->setMethods(['getAllEmails', 'saveEmail', 'deleteEmailById']) |
220
|
|
|
->getMock(); |
221
|
|
|
|
222
|
|
|
$mockEmailsService->expects($this->any()) |
223
|
|
|
->method('getAllEmails') |
224
|
|
|
->willReturn([]); |
225
|
|
|
|
226
|
|
|
$this->setComponent(Craft::app(), 'commerce_emails', $mockEmailsService); |
227
|
|
|
|
228
|
|
|
return $mockEmailsService; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return Mock|DbConnection |
233
|
|
|
*/ |
234
|
|
|
private function setMockDbConnection() |
235
|
|
|
{ |
236
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
237
|
|
|
->disableOriginalConstructor() |
238
|
|
|
->setMethods(['createCommand']) |
239
|
|
|
->getMock(); |
240
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
241
|
|
|
|
242
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
243
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
244
|
|
|
|
245
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
246
|
|
|
|
247
|
|
|
return $mockDbConnection; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return Mock|DbCommand |
252
|
|
|
*/ |
253
|
|
|
private function getMockDbCommand() |
254
|
|
|
{ |
255
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
256
|
|
|
->disableOriginalConstructor() |
257
|
|
|
->setMethods(['insertOrUpdate']) |
258
|
|
|
->getMock(); |
259
|
|
|
|
260
|
|
|
return $mockDbCommand; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|