|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Sales; |
|
7
|
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\OrderInterface; |
|
9
|
|
|
use CommerceLeague\ActiveCampaign\Api\OrderRepositoryInterface; |
|
10
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Client; |
|
11
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Request\OrderBuilder as OrderRequestBuilder; |
|
12
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
|
13
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Sales\ExportOrderConsumer; |
|
14
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\OrderApiResourceInterface; |
|
15
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
|
16
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
|
17
|
|
|
use Magento\Framework\Phrase; |
|
18
|
|
|
use Magento\Sales\Api\OrderRepositoryInterface as MagentoOrderRepositoryInterface; |
|
19
|
|
|
use Magento\Sales\Model\Order as MagentoOrder; |
|
20
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
|
|
23
|
|
|
class ExportOrderConsumerTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MockObject|MagentoOrderRepositoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $magentoOrderRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MockObject|Logger |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $logger; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var MockObject|OrderRepositoryInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $orderRepository; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var MockObject|OrderRequestBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $orderRequestBuilder; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var MockObject|Client |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $client; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var MockObject|OrderApiResourceInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $orderApi; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var MockObject|MagentoOrder |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $magentoOrder; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var MockObject|OrderInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $order; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var ExportOrderConsumer |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $exportOrderConsumer; |
|
69
|
|
|
|
|
70
|
|
|
protected function setUp() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->magentoOrderRepository = $this->createMock(MagentoOrderRepositoryInterface::class); |
|
73
|
|
|
$this->logger = $this->createMock(Logger::class); |
|
74
|
|
|
$this->orderRepository = $this->createMock(OrderRepositoryInterface::class); |
|
75
|
|
|
$this->orderRequestBuilder = $this->createMock(OrderRequestBuilder::class); |
|
76
|
|
|
$this->client = $this->createMock(Client::class); |
|
77
|
|
|
$this->orderApi = $this->createMock(OrderApiResourceInterface::class); |
|
78
|
|
|
$this->magentoOrder = $this->createMock(MagentoOrder::class); |
|
79
|
|
|
$this->order = $this->createMock(OrderInterface::class); |
|
80
|
|
|
|
|
81
|
|
|
$this->exportOrderConsumer = new ExportOrderConsumer( |
|
82
|
|
|
$this->magentoOrderRepository, |
|
83
|
|
|
$this->logger, |
|
84
|
|
|
$this->orderRepository, |
|
85
|
|
|
$this->orderRequestBuilder, |
|
86
|
|
|
$this->client |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testConsumeWithAbsentMagentoOrder() |
|
91
|
|
|
{ |
|
92
|
|
|
$magentoOrderId = 123; |
|
93
|
|
|
|
|
94
|
|
|
$exceptionMessage = 'an exception message'; |
|
95
|
|
|
$exception = new NoSuchEntityException(new Phrase($exceptionMessage)); |
|
96
|
|
|
|
|
97
|
|
|
$this->magentoOrderRepository->expects($this->once()) |
|
|
|
|
|
|
98
|
|
|
->method('get') |
|
99
|
|
|
->with($magentoOrderId) |
|
100
|
|
|
->willThrowException($exception); |
|
101
|
|
|
|
|
102
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
|
|
103
|
|
|
->method('error') |
|
104
|
|
|
->with($exceptionMessage); |
|
105
|
|
|
|
|
106
|
|
|
$this->orderRepository->expects($this->never()) |
|
|
|
|
|
|
107
|
|
|
->method('getOrCreateByMagentoOrderId'); |
|
108
|
|
|
|
|
109
|
|
|
$this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId])); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @throws \Magento\Framework\Exception\CouldNotSaveException |
|
114
|
|
|
* @group current |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testConsumeApiRequestException() |
|
117
|
|
|
{ |
|
118
|
|
|
$magentoOrderId = 123; |
|
119
|
|
|
$request = ['request']; |
|
120
|
|
|
|
|
121
|
|
|
$this->magentoOrderRepository->expects($this->once()) |
|
122
|
|
|
->method('get') |
|
123
|
|
|
->with($magentoOrderId) |
|
124
|
|
|
->willReturn($this->magentoOrder); |
|
125
|
|
|
|
|
126
|
|
|
$this->magentoOrder->expects($this->once()) |
|
127
|
|
|
->method('getId') |
|
|
|
|
|
|
128
|
|
|
->willReturn($magentoOrderId); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$this->orderRepository->expects($this->once()) |
|
131
|
|
|
->method('getOrCreateByMagentoOrderId') |
|
132
|
|
|
->with($magentoOrderId) |
|
133
|
|
|
->willReturn($this->order); |
|
134
|
|
|
|
|
135
|
|
|
$this->orderRequestBuilder->expects($this->once()) |
|
|
|
|
|
|
136
|
|
|
->method('build') |
|
137
|
|
|
->with($this->magentoOrder) |
|
138
|
|
|
->willReturn($request); |
|
139
|
|
|
|
|
140
|
|
|
$this->order->expects($this->once()) |
|
|
|
|
|
|
141
|
|
|
->method('getActiveCampaignId') |
|
142
|
|
|
->willReturn(null); |
|
143
|
|
|
|
|
144
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
|
|
145
|
|
|
->method('getOrderApi') |
|
146
|
|
|
->willReturn($this->orderApi); |
|
147
|
|
|
|
|
148
|
|
|
/** @var MockObject|HttpException $httpException */ |
|
149
|
|
|
$httpException = $this->createMock(HttpException::class); |
|
150
|
|
|
|
|
151
|
|
|
$this->orderApi->expects($this->once()) |
|
|
|
|
|
|
152
|
|
|
->method('create') |
|
153
|
|
|
->with(['ecomOrder' => $request]) |
|
154
|
|
|
->willThrowException($httpException); |
|
155
|
|
|
|
|
156
|
|
|
$this->logger->expects($this->once()) |
|
157
|
|
|
->method('error'); |
|
158
|
|
|
|
|
159
|
|
|
$this->order->expects($this->never()) |
|
160
|
|
|
->method('setActiveCampaignId'); |
|
161
|
|
|
|
|
162
|
|
|
$this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId])); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testConsumeUpdate() |
|
166
|
|
|
{ |
|
167
|
|
|
$magentoOrderId = 123; |
|
168
|
|
|
$request = ['request']; |
|
169
|
|
|
$activeCampaignId = 456; |
|
170
|
|
|
$response = ['ecomOrder' => ['id' => $activeCampaignId]]; |
|
171
|
|
|
|
|
172
|
|
|
$this->magentoOrderRepository->expects($this->once()) |
|
173
|
|
|
->method('get') |
|
174
|
|
|
->with($magentoOrderId) |
|
175
|
|
|
->willReturn($this->magentoOrder); |
|
176
|
|
|
|
|
177
|
|
|
$this->magentoOrder->expects($this->once()) |
|
178
|
|
|
->method('getId') |
|
179
|
|
|
->willReturn($magentoOrderId); |
|
180
|
|
|
|
|
181
|
|
|
$this->orderRepository->expects($this->once()) |
|
182
|
|
|
->method('getOrCreateByMagentoOrderId') |
|
183
|
|
|
->with($magentoOrderId) |
|
184
|
|
|
->willReturn($this->order); |
|
185
|
|
|
|
|
186
|
|
|
$this->orderRequestBuilder->expects($this->once()) |
|
187
|
|
|
->method('build') |
|
188
|
|
|
->with($this->magentoOrder) |
|
189
|
|
|
->willReturn($request); |
|
190
|
|
|
|
|
191
|
|
|
$this->order->expects($this->once()) |
|
192
|
|
|
->method('getActiveCampaignId') |
|
193
|
|
|
->willReturn($activeCampaignId); |
|
194
|
|
|
|
|
195
|
|
|
$this->client->expects($this->once()) |
|
196
|
|
|
->method('getOrderApi') |
|
197
|
|
|
->willReturn($this->orderApi); |
|
198
|
|
|
|
|
199
|
|
|
$this->orderApi->expects($this->once()) |
|
200
|
|
|
->method('update') |
|
201
|
|
|
->with($activeCampaignId, ['ecomOrder' => $request]) |
|
202
|
|
|
->willReturn($response); |
|
203
|
|
|
|
|
204
|
|
|
$this->order->expects($this->once()) |
|
205
|
|
|
->method('setActiveCampaignId') |
|
206
|
|
|
->with($activeCampaignId) |
|
207
|
|
|
->willReturnSelf(); |
|
208
|
|
|
|
|
209
|
|
|
$this->orderRepository->expects($this->once()) |
|
210
|
|
|
->method('save') |
|
211
|
|
|
->with($this->order); |
|
212
|
|
|
|
|
213
|
|
|
$this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId])); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function testConsumeCreate() |
|
217
|
|
|
{ |
|
218
|
|
|
$magentoOrderId = 123; |
|
219
|
|
|
$request = ['request']; |
|
220
|
|
|
$activeCampaignId = 456; |
|
221
|
|
|
$response = ['ecomOrder' => ['id' => $activeCampaignId]]; |
|
222
|
|
|
|
|
223
|
|
|
$this->magentoOrderRepository->expects($this->once()) |
|
224
|
|
|
->method('get') |
|
225
|
|
|
->with($magentoOrderId) |
|
226
|
|
|
->willReturn($this->magentoOrder); |
|
227
|
|
|
|
|
228
|
|
|
$this->magentoOrder->expects($this->once()) |
|
229
|
|
|
->method('getId') |
|
230
|
|
|
->willReturn($magentoOrderId); |
|
231
|
|
|
|
|
232
|
|
|
$this->orderRepository->expects($this->once()) |
|
233
|
|
|
->method('getOrCreateByMagentoOrderId') |
|
234
|
|
|
->with($magentoOrderId) |
|
235
|
|
|
->willReturn($this->order); |
|
236
|
|
|
|
|
237
|
|
|
$this->orderRequestBuilder->expects($this->once()) |
|
238
|
|
|
->method('build') |
|
239
|
|
|
->with($this->magentoOrder) |
|
240
|
|
|
->willReturn($request); |
|
241
|
|
|
|
|
242
|
|
|
$this->order->expects($this->once()) |
|
243
|
|
|
->method('getActiveCampaignId') |
|
244
|
|
|
->willReturn(null); |
|
245
|
|
|
|
|
246
|
|
|
$this->client->expects($this->once()) |
|
247
|
|
|
->method('getOrderApi') |
|
248
|
|
|
->willReturn($this->orderApi); |
|
249
|
|
|
|
|
250
|
|
|
$this->orderApi->expects($this->once()) |
|
251
|
|
|
->method('create') |
|
252
|
|
|
->with(['ecomOrder' => $request]) |
|
253
|
|
|
->willReturn($response); |
|
254
|
|
|
|
|
255
|
|
|
$this->order->expects($this->once()) |
|
256
|
|
|
->method('setActiveCampaignId') |
|
257
|
|
|
->with($activeCampaignId) |
|
258
|
|
|
->willReturnSelf(); |
|
259
|
|
|
|
|
260
|
|
|
$this->orderRepository->expects($this->once()) |
|
261
|
|
|
->method('save') |
|
262
|
|
|
->with($this->order); |
|
263
|
|
|
|
|
264
|
|
|
$this->exportOrderConsumer->consume(json_encode(['magento_order_id' => $magentoOrderId])); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
} |
|
268
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.