1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\Commerce_PaymentMethodModel; |
7
|
|
|
use Craft\Commerce_PaymentMethodsService; |
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 PaymentMethodsTest. |
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\PaymentMethods |
24
|
|
|
* @covers ::__construct |
25
|
|
|
* @covers ::<!public> |
26
|
|
|
*/ |
27
|
|
|
class PaymentMethodsTest extends BaseTest |
28
|
|
|
{ |
29
|
|
|
//============================================================================================================== |
30
|
|
|
//================================================= TESTS ==================================================== |
31
|
|
|
//============================================================================================================== |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers ::export |
35
|
|
|
* @dataProvider provideValidPaymentMethods |
36
|
|
|
* |
37
|
|
|
* @param PaymentMethodModel[] $methods |
38
|
|
|
* @param array $expectedResult |
39
|
|
|
*/ |
40
|
|
|
public function testSuccessfulExport(array $methods, array $expectedResult = []) |
41
|
|
|
{ |
42
|
|
|
$schematicPaymentMethodsService = new PaymentMethods(); |
43
|
|
|
|
44
|
|
|
$actualResult = $schematicPaymentMethodsService->export($methods); |
45
|
|
|
|
46
|
|
|
$this->assertSame($expectedResult, $actualResult); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers ::import |
51
|
|
|
* @dataProvider provideValidPaymentMethodDefinitions |
52
|
|
|
* |
53
|
|
|
* @param array $methodDefinitions |
54
|
|
|
*/ |
55
|
|
|
public function testSuccessfulImport(array $methodDefinitions) |
56
|
|
|
{ |
57
|
|
|
$this->setMockPaymentMethodsService(); |
58
|
|
|
$this->setMockDbConnection(); |
59
|
|
|
|
60
|
|
|
$schematicPaymentMethodsService = new PaymentMethods(); |
61
|
|
|
|
62
|
|
|
$import = $schematicPaymentMethodsService->import($methodDefinitions); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(Result::class, $import); |
65
|
|
|
$this->assertFalse($import->hasErrors()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @covers ::import |
70
|
|
|
* @dataProvider provideValidPaymentMethodDefinitions |
71
|
|
|
* |
72
|
|
|
* @param array $methodDefinitions |
73
|
|
|
*/ |
74
|
|
|
public function testImportWithForceOption(array $methodDefinitions) |
75
|
|
|
{ |
76
|
|
|
$this->setMockPaymentMethodsService(); |
77
|
|
|
$this->setMockDbConnection(); |
78
|
|
|
|
79
|
|
|
$schematicPaymentMethodsService = new PaymentMethods(); |
80
|
|
|
|
81
|
|
|
$import = $schematicPaymentMethodsService->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 provideValidPaymentMethods() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
'single method' => [ |
98
|
|
|
'PaymentMethods' => [ |
99
|
|
|
'method1' => $this->getMockPaymentMethod(1), |
100
|
|
|
], |
101
|
|
|
'expectedResult' => [ |
102
|
|
|
'methodName1' => [ |
103
|
|
|
'class' => null, |
104
|
|
|
'name' => 'methodName1', |
105
|
|
|
'paymentType' => null, |
106
|
|
|
'frontendEnabled' => null, |
107
|
|
|
'isArchived' => null, |
108
|
|
|
'dateArchived' => null, |
109
|
|
|
'settings' => null, |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
], |
113
|
|
|
'multiple methods' => [ |
114
|
|
|
'PaymentMethods' => [ |
115
|
|
|
'method1' => $this->getMockPaymentMethod(1), |
116
|
|
|
'method2' => $this->getMockPaymentMethod(2), |
117
|
|
|
], |
118
|
|
|
'expectedResult' => [ |
119
|
|
|
'methodName1' => [ |
120
|
|
|
'class' => null, |
121
|
|
|
'name' => 'methodName1', |
122
|
|
|
'paymentType' => null, |
123
|
|
|
'frontendEnabled' => null, |
124
|
|
|
'isArchived' => null, |
125
|
|
|
'dateArchived' => null, |
126
|
|
|
'settings' => null, |
127
|
|
|
], |
128
|
|
|
'methodName2' => [ |
129
|
|
|
'class' => null, |
130
|
|
|
'name' => 'methodName2', |
131
|
|
|
'paymentType' => null, |
132
|
|
|
'frontendEnabled' => null, |
133
|
|
|
'isArchived' => null, |
134
|
|
|
'dateArchived' => null, |
135
|
|
|
'settings' => null, |
136
|
|
|
], |
137
|
|
|
], |
138
|
|
|
], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function provideValidPaymentMethodDefinitions() |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
'emptyArray' => [ |
149
|
|
|
'methodDefinitions' => [], |
150
|
|
|
], |
151
|
|
|
'single method' => [ |
152
|
|
|
'methodDefinitions' => [ |
153
|
|
|
'methodName1' => [ |
154
|
|
|
'class' => null, |
155
|
|
|
'name' => 'methodName1', |
156
|
|
|
'paymentType' => null, |
157
|
|
|
'frontendEnabled' => null, |
158
|
|
|
'isArchived' => null, |
159
|
|
|
'dateArchived' => null, |
160
|
|
|
'settings' => null, |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
//============================================================================================================== |
168
|
|
|
//================================================= MOCKS ==================================================== |
169
|
|
|
//============================================================================================================== |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $methodId |
173
|
|
|
* |
174
|
|
|
* @return Mock|PaymentMethodModel |
175
|
|
|
*/ |
176
|
|
|
private function getMockPaymentMethod($methodId) |
177
|
|
|
{ |
178
|
|
|
$mockPaymentMethod = $this->getMockBuilder(Commerce_PaymentMethodModel::class) |
179
|
|
|
->disableOriginalConstructor() |
180
|
|
|
->getMock(); |
181
|
|
|
|
182
|
|
|
$mockPaymentMethod->expects($this->any()) |
183
|
|
|
->method('__get') |
184
|
|
|
->willReturnMap([ |
185
|
|
|
['id', $methodId], |
186
|
|
|
['name', 'methodName'.$methodId], |
187
|
|
|
]); |
188
|
|
|
|
189
|
|
|
$mockPaymentMethod->expects($this->any()) |
190
|
|
|
->method('getAllErrors') |
191
|
|
|
->willReturn([ |
192
|
|
|
'ohnoes' => 'horrible error', |
193
|
|
|
]); |
194
|
|
|
|
195
|
|
|
return $mockPaymentMethod; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Mock|CategoriesService |
200
|
|
|
*/ |
201
|
|
|
private function setMockPaymentMethodsService() |
202
|
|
|
{ |
203
|
|
|
$mockPaymentMethodsService = $this->getMockBuilder(Commerce_PaymentMethodsService::class) |
204
|
|
|
->disableOriginalConstructor() |
205
|
|
|
->setMethods(['getAllPaymentMethods', 'savePaymentMethod', 'archivePaymentMethod']) |
206
|
|
|
->getMock(); |
207
|
|
|
|
208
|
|
|
$mockPaymentMethodsService->expects($this->any()) |
209
|
|
|
->method('getAllPaymentMethods') |
210
|
|
|
->willReturn([]); |
211
|
|
|
|
212
|
|
|
$this->setComponent(Craft::app(), 'commerce_paymentMethods', $mockPaymentMethodsService); |
213
|
|
|
|
214
|
|
|
return $mockPaymentMethodsService; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Mock|DbConnection |
219
|
|
|
*/ |
220
|
|
|
private function setMockDbConnection() |
221
|
|
|
{ |
222
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
223
|
|
|
->disableOriginalConstructor() |
224
|
|
|
->setMethods(['createCommand']) |
225
|
|
|
->getMock(); |
226
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
227
|
|
|
|
228
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
229
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
230
|
|
|
|
231
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
232
|
|
|
|
233
|
|
|
return $mockDbConnection; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return Mock|DbCommand |
238
|
|
|
*/ |
239
|
|
|
private function getMockDbCommand() |
240
|
|
|
{ |
241
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
242
|
|
|
->disableOriginalConstructor() |
243
|
|
|
->setMethods(['insertOrUpdate']) |
244
|
|
|
->getMock(); |
245
|
|
|
|
246
|
|
|
return $mockDbCommand; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|