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' => [], |
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' => [], |
119
|
|
|
], |
120
|
|
|
'methodHandle2' => [ |
121
|
|
|
'name' => 'methodName2', |
122
|
|
|
'enabled' => null, |
123
|
|
|
'rules' => [], |
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('getRules') |
176
|
|
|
->willReturn([]); |
177
|
|
|
|
178
|
|
|
$mockShippingMethod->expects($this->any()) |
179
|
|
|
->method('getAllErrors') |
180
|
|
|
->willReturn([ |
181
|
|
|
'ohnoes' => 'horrible error', |
182
|
|
|
]); |
183
|
|
|
|
184
|
|
|
return $mockShippingMethod; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return Mock|CategoriesService |
189
|
|
|
*/ |
190
|
|
|
private function setMockShippingMethodsService() |
191
|
|
|
{ |
192
|
|
|
$mockShippingMethodsService = $this->getMockBuilder(Commerce_ShippingMethodsService::class) |
193
|
|
|
->disableOriginalConstructor() |
194
|
|
|
->setMethods(['getAllShippingMethods', 'saveShippingMethod', 'delete']) |
195
|
|
|
->getMock(); |
196
|
|
|
|
197
|
|
|
$mockShippingMethodsService->expects($this->any()) |
198
|
|
|
->method('getAllShippingMethods') |
199
|
|
|
->willReturn([]); |
200
|
|
|
|
201
|
|
|
$this->setComponent(Craft::app(), 'commerce_shippingMethods', $mockShippingMethodsService); |
202
|
|
|
|
203
|
|
|
return $mockShippingMethodsService; |
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
|
|
|
|