1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Quote; |
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\AbandonedCartBuilder as AbandonedCartRequestBuilder; |
12
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
13
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Quote\ExportAbandonedCartConsumer; |
14
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\OrderApiResourceInterface; |
15
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
16
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException; |
17
|
|
|
use Magento\Quote\Model\Quote; |
18
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Magento\Quote\Model\QuoteFactory; |
|
|
|
|
21
|
|
|
|
22
|
|
|
class ExportAbandonedCartConsumerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var MockObject|QuoteFactory |
26
|
|
|
*/ |
27
|
|
|
protected $quoteFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MockObject|Quote |
31
|
|
|
*/ |
32
|
|
|
protected $quote; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MockObject|Logger |
36
|
|
|
*/ |
37
|
|
|
protected $logger; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MockObject|OrderRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
protected $orderRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var MockObject|AbandonedCartRequestBuilder |
46
|
|
|
*/ |
47
|
|
|
protected $abandonedCartRequestBuilder; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var MockObject|Client |
51
|
|
|
*/ |
52
|
|
|
protected $client; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var MockObject|OrderApiResourceInterface |
56
|
|
|
*/ |
57
|
|
|
protected $orderApi; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var MockObject|OrderInterface |
61
|
|
|
*/ |
62
|
|
|
protected $order; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var ExportAbandonedCartConsumer |
66
|
|
|
*/ |
67
|
|
|
protected $exportAbandonedCartConsumer; |
68
|
|
|
|
69
|
|
|
protected function setUp() |
70
|
|
|
{ |
71
|
|
|
$this->quoteFactory = $this->getMockBuilder(QuoteFactory::class) |
72
|
|
|
->disableOriginalConstructor() |
73
|
|
|
->setMethods(['create']) |
74
|
|
|
->getMock(); |
75
|
|
|
|
76
|
|
|
$this->quote = $this->createMock(Quote::class); |
77
|
|
|
|
78
|
|
|
$this->quoteFactory->expects($this->any()) |
79
|
|
|
->method('create') |
80
|
|
|
->willReturn($this->quote); |
81
|
|
|
|
82
|
|
|
$this->logger = $this->createMock(Logger::class); |
83
|
|
|
$this->orderRepository = $this->createMock(OrderRepositoryInterface::class); |
84
|
|
|
$this->abandonedCartRequestBuilder = $this->createMock(AbandonedCartRequestBuilder::class); |
85
|
|
|
$this->client = $this->createMock(Client::class); |
86
|
|
|
$this->orderApi = $this->createMock(OrderApiResourceInterface::class); |
87
|
|
|
$this->order = $this->createMock(OrderInterface::class); |
88
|
|
|
|
89
|
|
|
$this->exportAbandonedCartConsumer = new ExportAbandonedCartConsumer( |
90
|
|
|
$this->quoteFactory, |
91
|
|
|
$this->logger, |
92
|
|
|
$this->orderRepository, |
93
|
|
|
$this->abandonedCartRequestBuilder, |
94
|
|
|
$this->client |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testConsumeWithAbsentQuote() |
99
|
|
|
{ |
100
|
|
|
$quoteId = 123; |
101
|
|
|
|
102
|
|
|
$this->quote->expects($this->once()) |
103
|
|
|
->method('loadByIdWithoutStore') |
|
|
|
|
104
|
|
|
->with(123) |
|
|
|
|
105
|
|
|
->willReturn($this->quote); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->quote->expects($this->once()) |
108
|
|
|
->method('getId') |
109
|
|
|
->willReturn(null); |
110
|
|
|
|
111
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
112
|
|
|
->method('error') |
113
|
|
|
->with(__('The Quote with the "%1" ID doesn\'t exist', $quoteId)); |
114
|
|
|
|
115
|
|
|
$this->orderRepository->expects($this->never()) |
|
|
|
|
116
|
|
|
->method('getOrCreateByMagentoQuoteId'); |
117
|
|
|
|
118
|
|
|
$this->exportAbandonedCartConsumer->consume( |
119
|
|
|
json_encode(['quote_id' => $quoteId]) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testConsumeApiHttpException() |
124
|
|
|
{ |
125
|
|
|
$quoteId = 123; |
126
|
|
|
$request = ['request']; |
127
|
|
|
|
128
|
|
|
$this->quote->expects($this->once()) |
129
|
|
|
->method('loadByIdWithoutStore') |
130
|
|
|
->with(123) |
131
|
|
|
->willReturn($this->quote); |
132
|
|
|
|
133
|
|
|
$this->quote->expects($this->any()) |
134
|
|
|
->method('getId') |
135
|
|
|
->willReturn($quoteId); |
136
|
|
|
|
137
|
|
|
$this->orderRepository->expects($this->once()) |
138
|
|
|
->method('getOrCreateByMagentoQuoteId') |
139
|
|
|
->with($quoteId) |
140
|
|
|
->willReturn($this->order); |
141
|
|
|
|
142
|
|
|
$this->abandonedCartRequestBuilder->expects($this->once()) |
|
|
|
|
143
|
|
|
->method('build') |
144
|
|
|
->with($this->quote) |
145
|
|
|
->willReturn($request); |
146
|
|
|
|
147
|
|
|
$this->client->expects($this->once()) |
|
|
|
|
148
|
|
|
->method('getOrderApi') |
149
|
|
|
->willReturn($this->orderApi); |
150
|
|
|
|
151
|
|
|
/** @var MockObject|HttpException $httpException */ |
152
|
|
|
$httpException = $this->createMock(HttpException::class); |
153
|
|
|
|
154
|
|
|
$this->orderApi->expects($this->once()) |
|
|
|
|
155
|
|
|
->method('create') |
156
|
|
|
->with(['ecomOrder' => $request]) |
157
|
|
|
->willThrowException($httpException); |
158
|
|
|
|
159
|
|
|
$this->logger->expects($this->once()) |
160
|
|
|
->method('error'); |
161
|
|
|
|
162
|
|
|
$this->order->expects($this->never()) |
|
|
|
|
163
|
|
|
->method('setActiveCampaignId'); |
164
|
|
|
|
165
|
|
|
$this->exportAbandonedCartConsumer->consume(json_encode(['quote_id' => $quoteId])); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testConsumeApiUnprocessableEntityHttpExceptionException() |
169
|
|
|
{ |
170
|
|
|
$quoteId = 123; |
171
|
|
|
$request = ['request']; |
172
|
|
|
$responseErrors = ['first error', 'second error']; |
173
|
|
|
|
174
|
|
|
$this->quote->expects($this->once()) |
175
|
|
|
->method('loadByIdWithoutStore') |
176
|
|
|
->with(123) |
177
|
|
|
->willReturn($this->quote); |
178
|
|
|
|
179
|
|
|
$this->quote->expects($this->any()) |
180
|
|
|
->method('getId') |
181
|
|
|
->willReturn($quoteId); |
182
|
|
|
|
183
|
|
|
$this->orderRepository->expects($this->once()) |
184
|
|
|
->method('getOrCreateByMagentoQuoteId') |
185
|
|
|
->with($quoteId) |
186
|
|
|
->willReturn($this->order); |
187
|
|
|
|
188
|
|
|
$this->abandonedCartRequestBuilder->expects($this->once()) |
189
|
|
|
->method('build') |
190
|
|
|
->with($this->quote) |
191
|
|
|
->willReturn($request); |
192
|
|
|
|
193
|
|
|
$this->client->expects($this->once()) |
194
|
|
|
->method('getOrderApi') |
195
|
|
|
->willReturn($this->orderApi); |
196
|
|
|
|
197
|
|
|
/** @var MockObject|UnprocessableEntityHttpException $unprocessableEntityHttpException */ |
198
|
|
|
$unprocessableEntityHttpException = $this->createMock(UnprocessableEntityHttpException::class); |
199
|
|
|
|
200
|
|
|
$this->orderApi->expects($this->once()) |
201
|
|
|
->method('create') |
202
|
|
|
->with(['ecomOrder' => $request]) |
203
|
|
|
->willThrowException($unprocessableEntityHttpException); |
204
|
|
|
|
205
|
|
|
$this->logger->expects($this->exactly(2)) |
206
|
|
|
->method('error'); |
207
|
|
|
|
208
|
|
|
$unprocessableEntityHttpException->expects($this->once()) |
209
|
|
|
->method('getResponseErrors') |
210
|
|
|
->willReturn($responseErrors); |
211
|
|
|
|
212
|
|
|
$this->logger->expects($this->at(1)) |
213
|
|
|
->method('error') |
214
|
|
|
->with(print_r($responseErrors, true)); |
215
|
|
|
|
216
|
|
|
$this->order->expects($this->never()) |
217
|
|
|
->method('setActiveCampaignId'); |
218
|
|
|
|
219
|
|
|
$this->exportAbandonedCartConsumer->consume(json_encode(['quote_id' => $quoteId])); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testConsume() |
223
|
|
|
{ |
224
|
|
|
$quoteId = 123; |
225
|
|
|
$request = ['request']; |
226
|
|
|
$activeCampaignId = 789; |
227
|
|
|
$response = ['ecomOrder' => ['id' => $activeCampaignId]]; |
228
|
|
|
|
229
|
|
|
$this->quote->expects($this->once()) |
230
|
|
|
->method('loadByIdWithoutStore') |
231
|
|
|
->with(123) |
232
|
|
|
->willReturn($this->quote); |
233
|
|
|
|
234
|
|
|
$this->quote->expects($this->any()) |
235
|
|
|
->method('getId') |
236
|
|
|
->willReturn($quoteId); |
237
|
|
|
|
238
|
|
|
$this->orderRepository->expects($this->once()) |
239
|
|
|
->method('getOrCreateByMagentoQuoteId') |
240
|
|
|
->with($quoteId) |
241
|
|
|
->willReturn($this->order); |
242
|
|
|
|
243
|
|
|
$this->abandonedCartRequestBuilder->expects($this->once()) |
244
|
|
|
->method('build') |
245
|
|
|
->with($this->quote) |
246
|
|
|
->willReturn($request); |
247
|
|
|
|
248
|
|
|
$this->client->expects($this->once()) |
249
|
|
|
->method('getOrderApi') |
250
|
|
|
->willReturn($this->orderApi); |
251
|
|
|
|
252
|
|
|
$this->orderApi->expects($this->once()) |
253
|
|
|
->method('create') |
254
|
|
|
->with(['ecomOrder' => $request]) |
255
|
|
|
->willReturn($response); |
256
|
|
|
|
257
|
|
|
$this->order->expects($this->once()) |
258
|
|
|
->method('setActiveCampaignId') |
259
|
|
|
->with($activeCampaignId) |
260
|
|
|
->willReturnSelf(); |
261
|
|
|
|
262
|
|
|
$this->orderRepository->expects($this->once()) |
263
|
|
|
->method('save') |
264
|
|
|
->with($this->order); |
265
|
|
|
|
266
|
|
|
$this->exportAbandonedCartConsumer->consume(json_encode(['quote_id' => $quoteId])); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths