1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the MailChimpEcommerceBundle package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 kevin92dev.es |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* Feel free to edit as you please, and have fun. |
11
|
|
|
* |
12
|
|
|
* @author Kevin Murillo <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Kevin92dev\MailChimpEcommerceBundle\Tests; |
16
|
|
|
|
17
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\RequestTypes; |
18
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Services\OrderService; |
19
|
|
|
use PHPUnit_Framework_TestCase; |
20
|
|
|
|
21
|
|
|
class OrderServiceTest extends PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
private $mailChimpStub; |
24
|
|
|
private $mcOrder; |
25
|
|
|
private $mcOrderLine; |
26
|
|
|
private $mcProduct; |
27
|
|
|
private $mcProductVariant; |
28
|
|
|
private $mcCustomer; |
29
|
|
|
private $mcAddress; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub |
34
|
|
|
|
35
|
|
|
$this->mailChimpStub = $this |
36
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Services\MailChimp') |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
$this->mcOrder = $this |
40
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Order') |
41
|
|
|
->getMock(); |
42
|
|
|
$this->mcOrderLine = $this |
43
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\OrderLine') |
44
|
|
|
->getMock(); |
45
|
|
|
$this->mcProduct = $this |
46
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Product') |
47
|
|
|
->getMock(); |
48
|
|
|
$this->mcProductVariant = $this |
49
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\ProductVariant') |
50
|
|
|
->getMock(); |
51
|
|
|
$this->mcCustomer = $this |
52
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Customer') |
53
|
|
|
->getMock(); |
54
|
|
|
$this->mcAddress = $this |
55
|
|
|
->getMockBuilder('\Kevin92dev\MailChimpEcommerceBundle\Entities\Address') |
56
|
|
|
->getMock(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSendOrderToMailChimpSuccess() |
60
|
|
|
{ |
61
|
|
|
$method = RequestTypes::$POST; |
62
|
|
|
$resource = '/orders'; |
63
|
|
|
$body = [ |
64
|
|
|
'landing_site' => "landing_site", |
65
|
|
|
'financial_status' => "processing", |
66
|
|
|
'fulfillment_status' => "", |
67
|
|
|
'currency_code' => "EUR", |
68
|
|
|
'order_total' => 21, |
69
|
|
|
'tax_total' => 4.48, |
70
|
|
|
'shipping_total' => 0, |
71
|
|
|
'tracking_code' => "prec", |
72
|
|
|
'processed_at_foreign' => "", |
73
|
|
|
'cancelled_at_foreign' => "", |
74
|
|
|
'updated_at_foreign' => "", |
75
|
|
|
'id' => "1234", |
76
|
|
|
'campaign_id' => "", |
77
|
|
|
'customer' => [ |
78
|
|
|
'id' => "50", |
79
|
|
|
'email_address' => "[email protected]", |
80
|
|
|
'opt_in_status' => true, |
81
|
|
|
'company' => "", |
82
|
|
|
'first_name' => "Kevin", |
83
|
|
|
'last_name' => "Murillo", |
84
|
|
|
'orders_count' => 5, |
85
|
|
|
'total_spent' => 50.75, |
86
|
|
|
'address' => [ |
87
|
|
|
'address1' => "Av. Test nº 92", |
88
|
|
|
'address2' => "", |
89
|
|
|
'city' => "Barcelona", |
90
|
|
|
'province' => "Barcelona", |
91
|
|
|
'province_code' => "BCN", |
92
|
|
|
'postal_code' => "08080", |
93
|
|
|
'country' => "Spain", |
94
|
|
|
'country_code' => "ESP", |
95
|
|
|
] |
96
|
|
|
], |
97
|
|
|
'shipping_address' => [ |
98
|
|
|
'name' => "shipping_address address for [email protected]", |
99
|
|
|
'address1' => "Av. Test nº 92", |
100
|
|
|
'address2' => "", |
101
|
|
|
'city' => "Barcelona", |
102
|
|
|
'province' => "Barcelona", |
103
|
|
|
'province_code' => "BCN", |
104
|
|
|
'postal_code' => "08080", |
105
|
|
|
'country' => "Spain", |
106
|
|
|
'country_code' => "ESP", |
107
|
|
|
], |
108
|
|
|
'billing_address' => [ |
109
|
|
|
'name' => "billing_address address for [email protected]", |
110
|
|
|
'address1' => "Av. Test nº 92", |
111
|
|
|
'address2' => "", |
112
|
|
|
'city' => "Barcelona", |
113
|
|
|
'province' => "Barcelona", |
114
|
|
|
'province_code' => "BCN", |
115
|
|
|
'postal_code' => "08080", |
116
|
|
|
'country' => "Spain", |
117
|
|
|
'country_code' => "ESP", |
118
|
|
|
] |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
// Create mocks |
122
|
|
|
$this->setCustomerMock(); |
123
|
|
|
$this->setAddressMock(); |
124
|
|
|
$this->setOrderMock($this->mcCustomer, $this->mcAddress); |
125
|
|
|
|
126
|
|
|
// Check that doRequest call have the right params |
127
|
|
|
$this->mailChimpStub |
128
|
|
|
->expects($this->once()) |
129
|
|
|
->method('doRequest') |
130
|
|
|
->with( |
131
|
|
|
$method, |
132
|
|
|
$body, |
133
|
|
|
$resource |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
// Call method to test |
137
|
|
|
$orderService = new OrderService($this->mailChimpStub); |
138
|
|
|
$orderService->create($this->mcOrder); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function setOrderMock($customerMock, $addressMock) |
142
|
|
|
{ |
143
|
|
|
$this->setOrderLineMock(); |
144
|
|
|
|
145
|
|
|
$array = [ |
146
|
|
|
'getId' => 1234, |
147
|
|
|
'getCampaignId' => '', |
148
|
|
|
'getLandingSite' => 'landing_site', |
149
|
|
|
'getFinancialStatus' => 'processing', |
150
|
|
|
'getFulfillmentStatus' => '', |
151
|
|
|
'getCurrencyCode' => 'EUR', |
152
|
|
|
'getOrderTotal' => 21, |
153
|
|
|
'getTaxTotal' => '4.48', |
154
|
|
|
'getShippingTotal' => '0', |
155
|
|
|
'getTrackingCode' => 'prec', |
156
|
|
|
'getProcessedAtForeign' => '', |
157
|
|
|
'getCancelledAtForeign' => '', |
158
|
|
|
'getUpdatedAtForeign' => '', |
159
|
|
|
'getCustomer' => $customerMock, |
160
|
|
|
'getShippingAddress' => $addressMock, |
161
|
|
|
'getBillingAddress' => $addressMock, |
162
|
|
|
'getOrderLines' => $this->mcOrderLine, |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
foreach ($array as $method => $value) { |
166
|
|
|
$this->mcOrder |
167
|
|
|
->expects($this->any()) |
168
|
|
|
->method($method) |
169
|
|
|
->will($this->returnValue($value)); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function setOrderLineMock() |
174
|
|
|
{ |
175
|
|
|
$this->setProductMock(); |
176
|
|
|
$this->setProductVariantMock(); |
177
|
|
|
|
178
|
|
|
$array = [ |
179
|
|
|
'getId' => 1234, |
180
|
|
|
'getProduct' => $this->mcProduct, |
181
|
|
|
'getProductVariant' => $this->mcProductVariant, |
182
|
|
|
'getQuantity' => 2, |
183
|
|
|
'getPrice' => 10.50, |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
foreach ($array as $method => $value) { |
187
|
|
|
$this->mcOrderLine |
188
|
|
|
->expects($this->any()) |
189
|
|
|
->method($method) |
190
|
|
|
->will($this->returnValue($value)); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
View Code Duplication |
private function setProductMock() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$array = [ |
197
|
|
|
'getId' => 567 |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
foreach ($array as $method => $value) { |
201
|
|
|
$this->mcProduct |
202
|
|
|
->expects($this->any()) |
203
|
|
|
->method($method) |
204
|
|
|
->will($this->returnValue($value)); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
private function setProductVariantMock() |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$array = [ |
211
|
|
|
'getId' => 567, |
212
|
|
|
]; |
213
|
|
|
|
214
|
|
|
foreach ($array as $method => $value) { |
215
|
|
|
$this->mcProductVariant |
216
|
|
|
->expects($this->any()) |
217
|
|
|
->method($method) |
218
|
|
|
->will($this->returnValue($value)); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
private function setCustomerMock() |
223
|
|
|
{ |
224
|
|
|
$array = [ |
225
|
|
|
'getId' => 50, |
226
|
|
|
'getEmail' => '[email protected]', |
227
|
|
|
'isOptInStatus' => true, |
228
|
|
|
'getCompany' => '', |
229
|
|
|
'getFirstname' => 'Kevin', |
230
|
|
|
'getLastname' => 'Murillo', |
231
|
|
|
'getOrdersCount' => 5, |
232
|
|
|
'getTotalSpent' => 50.75, |
233
|
|
|
'getAddress' => $this->mcAddress, |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
foreach ($array as $method => $value) { |
237
|
|
|
$this->mcCustomer |
238
|
|
|
->expects($this->any()) |
239
|
|
|
->method($method) |
240
|
|
|
->will($this->returnValue($value)); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
private function setAddressMock() |
245
|
|
|
{ |
246
|
|
|
$array = [ |
247
|
|
|
'getAddress1' => 'Av. Test nº 92', |
248
|
|
|
'getAddress2' => '', |
249
|
|
|
'getCity' => 'Barcelona', |
250
|
|
|
'getProvince' => 'Barcelona', |
251
|
|
|
'getProvinceCode' => 'BCN', |
252
|
|
|
'getPostalCode' => '08080', |
253
|
|
|
'getCountry' => 'Spain', |
254
|
|
|
'getCountryCode' => 'ESP', |
255
|
|
|
]; |
256
|
|
|
|
257
|
|
|
foreach ($array as $method => $value) { |
258
|
|
|
$this->mcAddress |
259
|
|
|
->expects($this->any()) |
260
|
|
|
->method($method) |
261
|
|
|
->will($this->returnValue($value)); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.