1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\Console\Command; |
7
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Console\Command\ExportOrderCommand; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper; |
10
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Topics; |
11
|
|
|
use Magento\Framework\Console\Cli; |
12
|
|
|
use Magento\Framework\MessageQueue\PublisherInterface; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory; |
|
|
|
|
16
|
|
|
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Order\Collection as OrderCollection; |
17
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
18
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
19
|
|
|
use Symfony\Component\Console\Helper\ProgressBarFactory; |
|
|
|
|
20
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
21
|
|
|
|
22
|
|
|
class ExportOrderCommandTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var MockObject|ConfigHelper |
26
|
|
|
*/ |
27
|
|
|
protected $configHelper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MockObject|OrderCollectionFactory |
31
|
|
|
*/ |
32
|
|
|
protected $orderCollectionFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MockObject|OrderCollection |
36
|
|
|
*/ |
37
|
|
|
protected $orderCollection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MockObject|PublisherInterface |
41
|
|
|
*/ |
42
|
|
|
protected $publisher; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var MockObject|ProgressBarFactory |
46
|
|
|
*/ |
47
|
|
|
protected $progressBarFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ExportOrderCommand |
51
|
|
|
*/ |
52
|
|
|
protected $exportOrderCommand; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var CommandTester |
56
|
|
|
*/ |
57
|
|
|
protected $exportOrderCommandTester; |
58
|
|
|
|
59
|
|
|
protected function setUp() |
60
|
|
|
{ |
61
|
|
|
$this->configHelper = $this->createMock(ConfigHelper::class); |
62
|
|
|
|
63
|
|
|
$this->orderCollectionFactory = $this->getMockBuilder(OrderCollectionFactory::class) |
64
|
|
|
->disableOriginalConstructor() |
65
|
|
|
->setMethods(['create']) |
66
|
|
|
->getMock(); |
67
|
|
|
|
68
|
|
|
$this->orderCollection = $this->createMock(OrderCollection::class); |
69
|
|
|
|
70
|
|
|
$this->orderCollectionFactory->expects($this->any()) |
71
|
|
|
->method('create') |
72
|
|
|
->willReturn($this->orderCollection); |
73
|
|
|
|
74
|
|
|
$this->publisher = $this->createMock(PublisherInterface::class); |
75
|
|
|
|
76
|
|
|
$this->progressBarFactory = $this->getMockBuilder(ProgressBarFactory::class) |
77
|
|
|
->disableOriginalConstructor() |
78
|
|
|
->setMethods(['create']) |
79
|
|
|
->getMock(); |
80
|
|
|
|
81
|
|
|
$this->exportOrderCommand = new ExportOrderCommand( |
82
|
|
|
$this->configHelper, |
83
|
|
|
$this->orderCollectionFactory, |
84
|
|
|
$this->progressBarFactory, |
85
|
|
|
$this->publisher |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->exportOrderCommandTester = new CommandTester($this->exportOrderCommand); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testExecuteDisabled() |
92
|
|
|
{ |
93
|
|
|
$this->configHelper->expects($this->once()) |
|
|
|
|
94
|
|
|
->method('isEnabled') |
95
|
|
|
->willReturn(false); |
96
|
|
|
|
97
|
|
|
$this->expectException(RuntimeException::class); |
98
|
|
|
$this->expectExceptionMessage('Export disabled by system configuration'); |
99
|
|
|
|
100
|
|
|
$this->exportOrderCommandTester->execute([]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testExecuteOrderExportDisabled() |
104
|
|
|
{ |
105
|
|
|
$this->configHelper->expects($this->once()) |
106
|
|
|
->method('isEnabled') |
107
|
|
|
->willReturn(true); |
108
|
|
|
|
109
|
|
|
$this->configHelper->expects($this->once()) |
110
|
|
|
->method('isOrderExportEnabled') |
111
|
|
|
->willReturn(false); |
112
|
|
|
|
113
|
|
|
$this->expectException(RuntimeException::class); |
114
|
|
|
$this->expectExceptionMessage('Export disabled by system configuration'); |
115
|
|
|
|
116
|
|
|
$this->exportOrderCommandTester->execute([]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testExecuteWithoutOptions() |
120
|
|
|
{ |
121
|
|
|
$this->configHelper->expects($this->once()) |
122
|
|
|
->method('isEnabled') |
123
|
|
|
->willReturn(true); |
124
|
|
|
|
125
|
|
|
$this->configHelper->expects($this->once()) |
126
|
|
|
->method('isOrderExportEnabled') |
127
|
|
|
->willReturn(true); |
128
|
|
|
|
129
|
|
|
$this->expectException(RuntimeException::class); |
130
|
|
|
$this->expectExceptionMessage('Please provide at least one option'); |
131
|
|
|
|
132
|
|
|
$this->exportOrderCommandTester->execute([]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testExecuteWithOrderIdAndOtherOptions() |
136
|
|
|
{ |
137
|
|
|
$this->configHelper->expects($this->once()) |
138
|
|
|
->method('isEnabled') |
139
|
|
|
->willReturn(true); |
140
|
|
|
|
141
|
|
|
$this->configHelper->expects($this->once()) |
142
|
|
|
->method('isOrderExportEnabled') |
143
|
|
|
->willReturn(true); |
144
|
|
|
|
145
|
|
|
$this->expectException(RuntimeException::class); |
146
|
|
|
$this->expectExceptionMessage('You cannot use --order-id together with another option'); |
147
|
|
|
|
148
|
|
|
$this->exportOrderCommandTester->execute( |
149
|
|
|
['--order-id' => 123, '--omitted' => true, '--all' => true] |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testExecuteWithAllAndOmittedOption() |
154
|
|
|
{ |
155
|
|
|
$this->configHelper->expects($this->once()) |
156
|
|
|
->method('isEnabled') |
157
|
|
|
->willReturn(true); |
158
|
|
|
|
159
|
|
|
$this->configHelper->expects($this->once()) |
160
|
|
|
->method('isOrderExportEnabled') |
161
|
|
|
->willReturn(true); |
162
|
|
|
|
163
|
|
|
$this->expectException(RuntimeException::class); |
164
|
|
|
$this->expectExceptionMessage('You cannot use --omitted and --all together'); |
165
|
|
|
|
166
|
|
|
$this->exportOrderCommandTester->execute( |
167
|
|
|
['--omitted' => true, '--all' => true] |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testExecuteWithoutOrderIds() |
172
|
|
|
{ |
173
|
|
|
$this->configHelper->expects($this->once()) |
174
|
|
|
->method('isEnabled') |
175
|
|
|
->willReturn(true); |
176
|
|
|
|
177
|
|
|
$this->configHelper->expects($this->once()) |
178
|
|
|
->method('isOrderExportEnabled') |
179
|
|
|
->willReturn(true); |
180
|
|
|
|
181
|
|
|
$this->orderCollection->expects($this->once()) |
|
|
|
|
182
|
|
|
->method('getAllIds') |
183
|
|
|
->willReturn([]); |
184
|
|
|
|
185
|
|
|
$this->exportOrderCommandTester->execute( |
186
|
|
|
['--all' => true] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$this->assertContains( |
190
|
|
|
'No order(s) found matching your criteria', |
191
|
|
|
$this->exportOrderCommandTester->getDisplay() |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$this->assertEquals( |
195
|
|
|
Cli::RETURN_FAILURE, |
196
|
|
|
$this->exportOrderCommandTester->getStatusCode() |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testExecuteWithOrderIdOption() |
201
|
|
|
{ |
202
|
|
|
$orderId = 123; |
203
|
|
|
|
204
|
|
|
$this->configHelper->expects($this->once()) |
205
|
|
|
->method('isEnabled') |
206
|
|
|
->willReturn(true); |
207
|
|
|
|
208
|
|
|
$this->configHelper->expects($this->once()) |
209
|
|
|
->method('isOrderExportEnabled') |
210
|
|
|
->willReturn(true); |
211
|
|
|
|
212
|
|
|
$this->orderCollection->expects($this->once()) |
213
|
|
|
->method('addExcludeGuestFilter') |
214
|
|
|
->willReturnSelf(); |
215
|
|
|
|
216
|
|
|
$this->orderCollection->expects($this->once()) |
217
|
|
|
->method('addIdFilter') |
218
|
|
|
->with($orderId) |
219
|
|
|
->willReturnSelf(); |
220
|
|
|
|
221
|
|
|
$this->orderCollection->expects($this->never()) |
222
|
|
|
->method('addOmittedFilter'); |
223
|
|
|
|
224
|
|
|
$this->orderCollection->expects($this->once()) |
225
|
|
|
->method('getAllIds') |
226
|
|
|
->willReturn([$orderId]); |
227
|
|
|
|
228
|
|
|
$progressBar = new ProgressBar(new TestOutput()); |
229
|
|
|
|
230
|
|
|
$this->progressBarFactory->expects($this->once()) |
231
|
|
|
->method('create') |
232
|
|
|
->willReturn($progressBar); |
233
|
|
|
|
234
|
|
|
$this->publisher->expects($this->once()) |
|
|
|
|
235
|
|
|
->method('publish') |
236
|
|
|
->with( |
237
|
|
|
Topics::SALES_ORDER_EXPORT, |
238
|
|
|
json_encode(['magento_order_id' => $orderId]) |
239
|
|
|
); |
240
|
|
|
|
241
|
|
|
$this->exportOrderCommandTester->execute( |
242
|
|
|
['--order-id' => $orderId] |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$this->assertContains( |
246
|
|
|
'1 order(s) have been scheduled for export.', |
247
|
|
|
$this->exportOrderCommandTester->getDisplay() |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
$this->assertEquals(Cli::RETURN_SUCCESS, $this->exportOrderCommandTester->getStatusCode()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testWithOmittedOption() |
254
|
|
|
{ |
255
|
|
|
$orderIds = [123, 456]; |
256
|
|
|
|
257
|
|
|
$this->configHelper->expects($this->once()) |
258
|
|
|
->method('isEnabled') |
259
|
|
|
->willReturn(true); |
260
|
|
|
|
261
|
|
|
$this->configHelper->expects($this->once()) |
262
|
|
|
->method('isOrderExportEnabled') |
263
|
|
|
->willReturn(true); |
264
|
|
|
|
265
|
|
|
$this->orderCollection->expects($this->once()) |
266
|
|
|
->method('addExcludeGuestFilter') |
267
|
|
|
->willReturnSelf(); |
268
|
|
|
|
269
|
|
|
$this->orderCollection->expects($this->never()) |
270
|
|
|
->method('addIdFilter'); |
271
|
|
|
|
272
|
|
|
$this->orderCollection->expects($this->once()) |
273
|
|
|
->method('addOmittedFilter') |
274
|
|
|
->willReturnSelf(); |
275
|
|
|
|
276
|
|
|
$this->orderCollection->expects($this->once()) |
277
|
|
|
->method('getAllIds') |
278
|
|
|
->willReturn($orderIds); |
279
|
|
|
|
280
|
|
|
$progressBar = new ProgressBar(new TestOutput()); |
281
|
|
|
|
282
|
|
|
$this->progressBarFactory->expects($this->once()) |
283
|
|
|
->method('create') |
284
|
|
|
->willReturn($progressBar); |
285
|
|
|
|
286
|
|
|
$this->publisher->expects($this->exactly(2)) |
287
|
|
|
->method('publish'); |
288
|
|
|
|
289
|
|
|
$this->exportOrderCommandTester->execute( |
290
|
|
|
['--omitted' => true] |
291
|
|
|
); |
292
|
|
|
|
293
|
|
|
$this->assertContains( |
294
|
|
|
'2 order(s) have been scheduled for export.', |
295
|
|
|
$this->exportOrderCommandTester->getDisplay() |
296
|
|
|
); |
297
|
|
|
|
298
|
|
|
$this->assertEquals(Cli::RETURN_SUCCESS, $this->exportOrderCommandTester->getStatusCode()); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function testExecuteWithAllOption() |
302
|
|
|
{ |
303
|
|
|
$orderIds = [123, 456, 789]; |
304
|
|
|
|
305
|
|
|
$this->configHelper->expects($this->once()) |
306
|
|
|
->method('isEnabled') |
307
|
|
|
->willReturn(true); |
308
|
|
|
|
309
|
|
|
$this->configHelper->expects($this->once()) |
310
|
|
|
->method('isOrderExportEnabled') |
311
|
|
|
->willReturn(true); |
312
|
|
|
|
313
|
|
|
$this->orderCollection->expects($this->once()) |
314
|
|
|
->method('addExcludeGuestFilter') |
315
|
|
|
->willReturnSelf(); |
316
|
|
|
|
317
|
|
|
$this->orderCollection->expects($this->never()) |
318
|
|
|
->method('addIdFilter'); |
319
|
|
|
|
320
|
|
|
$this->orderCollection->expects($this->never()) |
321
|
|
|
->method('addOmittedFilter'); |
322
|
|
|
|
323
|
|
|
$this->orderCollection->expects($this->once()) |
324
|
|
|
->method('getAllIds') |
325
|
|
|
->willReturn($orderIds); |
326
|
|
|
|
327
|
|
|
$progressBar = new ProgressBar(new TestOutput()); |
328
|
|
|
|
329
|
|
|
$this->progressBarFactory->expects($this->once()) |
330
|
|
|
->method('create') |
331
|
|
|
->willReturn($progressBar); |
332
|
|
|
|
333
|
|
|
$this->publisher->expects($this->exactly(3)) |
334
|
|
|
->method('publish'); |
335
|
|
|
|
336
|
|
|
$this->exportOrderCommandTester->execute( |
337
|
|
|
['--all' => true] |
338
|
|
|
); |
339
|
|
|
|
340
|
|
|
$this->assertContains( |
341
|
|
|
'3 order(s) have been scheduled for export.', |
342
|
|
|
$this->exportOrderCommandTester->getDisplay() |
343
|
|
|
); |
344
|
|
|
|
345
|
|
|
$this->assertEquals(Cli::RETURN_SUCCESS, $this->exportOrderCommandTester->getStatusCode()); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
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