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