1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use Lakion\ApiTestCase\JsonApiTestCase; |
15
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
16
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
17
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Mateusz Zalewski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class CheckoutApiTestCase extends JsonApiTestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected static $authorizedHeaderWithContentType = [ |
28
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
29
|
|
|
'CONTENT_TYPE' => 'application/json', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected static $authorizedHeaderWithAccept = [ |
36
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
37
|
|
|
'ACCEPT' => 'application/json', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
protected function createCart() |
44
|
|
|
{ |
45
|
|
|
$data = |
46
|
|
|
<<<EOT |
47
|
|
|
{ |
48
|
|
|
"customer": "[email protected]", |
49
|
|
|
"channel": "CHANNEL", |
50
|
|
|
"locale_code": "en_US" |
51
|
|
|
} |
52
|
|
|
EOT; |
53
|
|
|
|
54
|
|
|
$this->client->request('POST', '/api/v1/carts/', [], [], static::$authorizedHeaderWithContentType, $data); |
55
|
|
|
|
56
|
|
|
$response = $this->client->getResponse(); |
57
|
|
|
$rawResponse = json_decode($response->getContent(), true); |
58
|
|
|
|
59
|
|
|
return $rawResponse['id']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $cartId |
64
|
|
|
*/ |
65
|
|
|
protected function addItemToCart($cartId) |
66
|
|
|
{ |
67
|
|
|
$url = sprintf('/api/v1/carts/%d/items/', $cartId); |
68
|
|
|
|
69
|
|
|
$data = |
70
|
|
|
<<<EOT |
71
|
|
|
{ |
72
|
|
|
"variant": "MUG_SW", |
73
|
|
|
"quantity": 1 |
74
|
|
|
} |
75
|
|
|
EOT; |
76
|
|
|
|
77
|
|
|
$this->client->request('POST', $url, [], [], static::$authorizedHeaderWithContentType, $data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param mixed $cartId |
82
|
|
|
*/ |
83
|
|
|
protected function addressOrder($cartId) |
84
|
|
|
{ |
85
|
|
|
$this->loadFixturesFromFile('resources/countries.yml'); |
86
|
|
|
|
87
|
|
|
$data = |
88
|
|
|
<<<EOT |
89
|
|
|
{ |
90
|
|
|
"shipping_address": { |
91
|
|
|
"first_name": "Hieronim", |
92
|
|
|
"last_name": "Bosch", |
93
|
|
|
"street": "Surrealism St.", |
94
|
|
|
"country_code": "NL", |
95
|
|
|
"city": "’s-Hertogenbosch", |
96
|
|
|
"postcode": "99-999" |
97
|
|
|
}, |
98
|
|
|
"billing_address": { |
99
|
|
|
"first_name": "Vincent", |
100
|
|
|
"last_name": "van Gogh", |
101
|
|
|
"street": "Post-Impressionism St.", |
102
|
|
|
"country_code": "NL", |
103
|
|
|
"city": "Groot Zundert", |
104
|
|
|
"postcode": "88-888" |
105
|
|
|
}, |
106
|
|
|
"different_billing_address": true |
107
|
|
|
} |
108
|
|
|
EOT; |
109
|
|
|
|
110
|
|
|
$url = sprintf('/api/v1/checkouts/addressing/%d', $cartId); |
111
|
|
|
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $cartId |
116
|
|
|
*/ |
117
|
|
|
protected function selectOrderShippingMethod($cartId) |
118
|
|
|
{ |
119
|
|
|
$url = sprintf('/api/v1/checkouts/select-shipping/%d', $cartId); |
120
|
|
|
|
121
|
|
|
$this->client->request('GET', $url, [], [], static::$authorizedHeaderWithContentType); |
122
|
|
|
|
123
|
|
|
$response = $this->client->getResponse(); |
124
|
|
|
$rawResponse = json_decode($response->getContent(), true); |
125
|
|
|
|
126
|
|
|
$data = |
127
|
|
|
<<<EOT |
128
|
|
|
{ |
129
|
|
|
"shipments": [ |
130
|
|
|
{ |
131
|
|
|
"method": "{$rawResponse['shipments'][0]['methods'][0]['code']}" |
132
|
|
|
} |
133
|
|
|
] |
134
|
|
|
} |
135
|
|
|
EOT; |
136
|
|
|
|
137
|
|
|
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param mixed $cartId |
142
|
|
|
*/ |
143
|
|
|
protected function selectOrderPaymentMethod($cartId) |
144
|
|
|
{ |
145
|
|
|
$url = sprintf('/api/v1/checkouts/select-payment/%d', $cartId); |
146
|
|
|
|
147
|
|
|
$this->client->request('GET', $url, [], [], static::$authorizedHeaderWithContentType); |
148
|
|
|
|
149
|
|
|
$response = $this->client->getResponse(); |
150
|
|
|
$rawResponse = json_decode($response->getContent(), true); |
151
|
|
|
|
152
|
|
|
$data = |
153
|
|
|
<<<EOT |
154
|
|
|
{ |
155
|
|
|
"payments": [ |
156
|
|
|
{ |
157
|
|
|
"method": "{$rawResponse['payments'][0]['methods'][0]['code']}" |
158
|
|
|
} |
159
|
|
|
] |
160
|
|
|
} |
161
|
|
|
EOT; |
162
|
|
|
|
163
|
|
|
$this->client->request('PUT', $url, [], [], static::$authorizedHeaderWithContentType, $data); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param mixed $cartId |
168
|
|
|
*/ |
169
|
|
|
protected function completeOrder($cartId) |
170
|
|
|
{ |
171
|
|
|
$this->client->request('PUT', sprintf('/api/v1/checkouts/complete/%d', $cartId), [], [], static::$authorizedHeaderWithContentType); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
protected function prepareOrder() |
178
|
|
|
{ |
179
|
|
|
$cartId = $this->createCart(); |
180
|
|
|
|
181
|
|
|
$this->addItemToCart($cartId); |
182
|
|
|
$this->addressOrder($cartId); |
183
|
|
|
$this->selectOrderShippingMethod($cartId); |
184
|
|
|
$this->selectOrderPaymentMethod($cartId); |
185
|
|
|
$this->completeOrder($cartId); |
186
|
|
|
|
187
|
|
|
return $cartId; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param mixed $cartId |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
protected function getCheckoutSummaryUrl($cartId) |
196
|
|
|
{ |
197
|
|
|
return sprintf('/api/v1/checkouts/%d', $cartId); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|