GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c92bdf...416677 )
by Andreas
04:49
created

testExecuteContactExportDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace Test\Unit\Console\Command;
7
8
use CommerceLeague\ActiveCampaign\Console\Command\ExportContactCommand;
9
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper;
10
use CommerceLeague\ActiveCampaign\MessageQueue\Topics;
11
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Customer\Collection as CustomerCollection;
12
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
0 ignored issues
show
Bug introduced by
The type CommerceLeague\ActiveCam...tomer\CollectionFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Subscriber\Collection as SubscriberCollection;
14
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Subscriber\CollectionFactory as SubscriberCollectionFactory;
0 ignored issues
show
Bug introduced by
The type CommerceLeague\ActiveCam...riber\CollectionFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use CommerceLeague\ActiveCampaign\Test\Unit\Console\Command\TestOutput;
16
use Magento\Framework\Console\Cli;
17
use Magento\Framework\MessageQueue\PublisherInterface;
18
use PHPUnit\Framework\MockObject\MockObject;
19
use PHPUnit\Framework\TestCase;
20
use Symfony\Component\Console\Exception\RuntimeException;
21
use Symfony\Component\Console\Helper\ProgressBar;
22
use Symfony\Component\Console\Helper\ProgressBarFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...lper\ProgressBarFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Console\Tester\CommandTester;
24
25
class ExportContactCommandTest extends TestCase
26
{
27
    /**
28
     * @var MockObject|ConfigHelper
29
     */
30
    protected $configHelper;
31
32
    /**
33
     * @var MockObject|CustomerCollectionFactory
34
     */
35
    protected $customerCollectionFactory;
36
37
    /**
38
     * @var MockObject|CustomerCollection
39
     */
40
    protected $customerCollection;
41
42
    /**
43
     * @var MockObject|SubscriberCollectionFactory
44
     */
45
    protected $subscriberCollectionFactory;
46
47
    /**
48
     * @var MockObject|SubscriberCollection
49
     */
50
    protected $subscriberCollection;
51
52
    /**
53
     * @var MockObject|ProgressBarFactory
54
     */
55
    protected $progressBarFactory;
56
57
    /**
58
     * @var MockObject|PublisherInterface
59
     */
60
    protected $publisher;
61
62
    /**
63
     * @var ExportContactCommand
64
     */
65
    protected $exportContactCommand;
66
67
    /**
68
     * @var CommandTester
69
     */
70
    protected $exportContactCommandTester;
71
72
    protected function setUp()
73
    {
74
        $this->configHelper = $this->createMock(ConfigHelper::class);
75
76
        $this->customerCollectionFactory = $this->getMockBuilder(CustomerCollectionFactory::class)
77
            ->disableOriginalConstructor()
78
            ->setMethods(['create'])
79
            ->getMock();
80
81
        $this->customerCollection = $this->createMock(CustomerCollection::class);
82
83
        $this->customerCollectionFactory->expects($this->any())
84
            ->method('create')
85
            ->willReturn($this->customerCollection);
86
87
        $this->subscriberCollectionFactory = $this->getMockBuilder(SubscriberCollectionFactory::class)
88
            ->disableOriginalConstructor()
89
            ->setMethods(['create'])
90
            ->getMock();
91
92
        $this->subscriberCollection = $this->createMock(SubscriberCollection::class);
93
94
        $this->subscriberCollectionFactory->expects($this->any())
95
            ->method('create')
96
            ->willReturn($this->subscriberCollection);
97
98
        $this->progressBarFactory = $this->getMockBuilder(ProgressBarFactory::class)
99
            ->disableOriginalConstructor()
100
            ->setMethods(['create'])
101
            ->getMock();
102
103
        $this->publisher = $this->createMock(PublisherInterface::class);
104
105
        $this->exportContactCommand = new ExportContactCommand(
106
            $this->configHelper,
107
            $this->customerCollectionFactory,
108
            $this->subscriberCollectionFactory,
109
            $this->progressBarFactory,
110
            $this->publisher
111
        );
112
113
        $this->exportContactCommandTester = new CommandTester($this->exportContactCommand);
114
    }
115
116
    public function testExecuteDisabled()
117
    {
118
        $this->configHelper->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCampaign\Helper\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        $this->configHelper->/** @scrutinizer ignore-call */ 
119
                             expects($this->once())

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.

Loading history...
119
            ->method('isEnabled')
120
            ->willReturn(false);
121
122
        $this->expectException(RuntimeException::class);
123
        $this->expectExceptionMessage('Export disabled by system configuration');
124
125
        $this->exportContactCommandTester->execute([]);
126
    }
127
128
    public function testExecuteContactExportDisabled()
129
    {
130
        $this->configHelper->expects($this->once())
131
            ->method('isEnabled')
132
            ->willReturn(true);
133
134
        $this->configHelper->expects($this->once())
135
            ->method('isContactExportEnabled')
136
            ->willReturn(false);
137
138
        $this->expectException(RuntimeException::class);
139
        $this->expectExceptionMessage('Export disabled by system configuration');
140
141
        $this->exportContactCommandTester->execute([]);
142
    }
143
144
    public function testExecuteWithoutOptions()
145
    {
146
        $this->configHelper->expects($this->once())
147
            ->method('isEnabled')
148
            ->willReturn(true);
149
150
        $this->configHelper->expects($this->once())
151
            ->method('isContactExportEnabled')
152
            ->willReturn(true);
153
154
        $this->expectException(RuntimeException::class);
155
        $this->expectExceptionMessage('Please provide at least one option');
156
157
        $this->exportContactCommandTester->execute([]);
158
    }
159
160
    public function testExecuteWithEmailAndOtherOptions()
161
    {
162
        $this->configHelper->expects($this->once())
163
            ->method('isEnabled')
164
            ->willReturn(true);
165
166
        $this->configHelper->expects($this->once())
167
            ->method('isContactExportEnabled')
168
            ->willReturn(true);
169
170
        $this->expectException(RuntimeException::class);
171
        $this->expectExceptionMessage('You cannot use --email together with another option');
172
173
        $this->exportContactCommandTester->execute(
174
            ['--email' => '[email protected]', '--omitted' => true, '--all' => true]
175
        );
176
    }
177
178
    public function testExecuteWithAllAndOmittedOption()
179
    {
180
        $this->configHelper->expects($this->once())
181
            ->method('isEnabled')
182
            ->willReturn(true);
183
184
        $this->configHelper->expects($this->once())
185
            ->method('isContactExportEnabled')
186
            ->willReturn(true);
187
188
        $this->expectException(RuntimeException::class);
189
        $this->expectExceptionMessage('You cannot use --omitted and --all together');
190
191
        $this->exportContactCommandTester->execute(
192
            ['--omitted' => true, '--all' => true]
193
        );
194
    }
195
196
    public function testExecuteWithoutCustomerIdsAndSubscriberEmails()
197
    {
198
        $this->configHelper->expects($this->once())
199
            ->method('isEnabled')
200
            ->willReturn(true);
201
202
        $this->configHelper->expects($this->once())
203
            ->method('isContactExportEnabled')
204
            ->willReturn(true);
205
206
        $this->customerCollection->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...del\Customer\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        $this->customerCollection->/** @scrutinizer ignore-call */ 
207
                                   expects($this->once())

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.

Loading history...
207
            ->method('getAllIds')
208
            ->willReturn([]);
209
210
        $this->subscriberCollection->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...l\Subscriber\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
        $this->subscriberCollection->/** @scrutinizer ignore-call */ 
211
                                     expects($this->once())

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.

Loading history...
211
            ->method('getAllEmails')
212
            ->willReturn([]);
213
214
        $this->exportContactCommandTester->execute(
215
            ['--all' => true]
216
        );
217
218
        $this->assertContains(
219
            'No contact(s) found matching your criteria',
220
            $this->exportContactCommandTester->getDisplay()
221
        );
222
223
        $this->assertEquals(
224
            Cli::RETURN_FAILURE,
225
            $this->exportContactCommandTester->getStatusCode()
226
        );
227
    }
228
229
    public function testExecuteWithEmailFromCustomerOption()
230
    {
231
        $email = '[email protected]';
232
        $customerId = 123;
233
234
        $this->configHelper->expects($this->once())
235
            ->method('isEnabled')
236
            ->willReturn(true);
237
238
        $this->configHelper->expects($this->once())
239
            ->method('isContactExportEnabled')
240
            ->willReturn(true);
241
242
        $this->customerCollection->expects($this->once())
243
            ->method('addEmailFilter')
244
            ->with($email)
245
            ->willReturnSelf();
246
247
        $this->customerCollection->expects($this->never())
248
            ->method('addCustomerOmittedFilter');
249
250
        $this->customerCollection->expects($this->once())
251
            ->method('getAllIds')
252
            ->willReturn([$customerId]);
253
254
        $progressBar = new ProgressBar(new TestOutput());
255
256
        $this->progressBarFactory->expects($this->once())
257
            ->method('create')
258
            ->willReturn($progressBar);
259
260
        $this->publisher->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Framework\MessageQueue\PublisherInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
        $this->publisher->/** @scrutinizer ignore-call */ 
261
                          expects($this->once())

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.

Loading history...
261
            ->method('publish')
262
            ->with(
263
                Topics::CUSTOMER_CONTACT_EXPORT,
264
                json_encode(['magento_customer_id' => $customerId])
265
            );
266
267
        $this->exportContactCommandTester->execute(
268
            ['--email' => $email]
269
        );
270
271
        $this->assertContains(
272
            '1 contact(s) have been scheduled for export.',
273
            $this->exportContactCommandTester->getDisplay()
274
        );
275
276
        $this->assertEquals(Cli::RETURN_SUCCESS, $this->exportContactCommandTester->getStatusCode());
277
    }
278
279
    public function testExecuteWithEmailFromSubscriberOption()
280
    {
281
        $email = '[email protected]';
282
283
        $this->configHelper->expects($this->once())
284
            ->method('isEnabled')
285
            ->willReturn(true);
286
287
        $this->configHelper->expects($this->once())
288
            ->method('isContactExportEnabled')
289
            ->willReturn(true);
290
291
        $this->customerCollection->expects($this->once())
292
            ->method('addEmailFilter')
293
            ->with($email)
294
            ->willReturnSelf();
295
296
        $this->customerCollection->expects($this->never())
297
            ->method('addCustomerOmittedFilter');
298
299
        $this->customerCollection->expects($this->once())
300
            ->method('getAllIds')
301
            ->willReturn([]);
302
303
        $this->subscriberCollection->expects($this->once())
304
            ->method('excludeCustomers')
305
            ->willReturnSelf();
306
307
        $this->subscriberCollection->expects($this->once())
308
            ->method('addEmailFilter')
309
            ->willReturnSelf();
310
311
        $this->subscriberCollection->expects($this->never())
312
            ->method('addContactOmittedFilter');
313
314
        $this->subscriberCollection->expects($this->once())
315
            ->method('getAllEmails')
316
            ->willReturn([$email]);
317
318
        $progressBar = new ProgressBar(new TestOutput());
319
320
        $this->progressBarFactory->expects($this->once())
321
            ->method('create')
322
            ->willReturn($progressBar);
323
324
        $this->publisher->expects($this->once())
325
            ->method('publish')
326
            ->with(
327
                Topics::NEWSLETTER_CONTACT_EXPORT,
328
                json_encode(['email' => $email])
329
            );
330
331
        $this->exportContactCommandTester->execute(
332
            ['--email' => $email]
333
        );
334
335
        $this->assertContains(
336
            '1 contact(s) have been scheduled for export.',
337
            $this->exportContactCommandTester->getDisplay()
338
        );
339
340
        $this->assertEquals(Cli::RETURN_SUCCESS, $this->exportContactCommandTester->getStatusCode());
341
    }
342
343
    public function testExecuteWithOmittedOption()
344
    {
345
        $customerIds = [123, 456];
346
        $emails = ['[email protected]', '[email protected]'];
347
348
        $this->configHelper->expects($this->once())
349
            ->method('isEnabled')
350
            ->willReturn(true);
351
352
        $this->configHelper->expects($this->once())
353
            ->method('isContactExportEnabled')
354
            ->willReturn(true);
355
356
        $this->customerCollection->expects($this->never())
357
            ->method('addEmailFilter');
358
359
        $this->customerCollection->expects($this->once())
360
            ->method('addContactOmittedFilter')
361
            ->willReturnSelf();
362
363
        $this->customerCollection->expects($this->once())
364
            ->method('getAllIds')
365
            ->willReturn($customerIds);
366
367
        $this->subscriberCollection->expects($this->never())
368
            ->method('addEmailFilter');
369
370
        $this->subscriberCollection->expects($this->once())
371
            ->method('addContactOmittedFilter')
372
            ->willReturnSelf();
373
374
        $this->subscriberCollection->expects($this->once())
375
            ->method('getAllEmails')
376
            ->willReturn($emails);
377
378
        $progressBar = new ProgressBar(new TestOutput());
379
380
        $this->progressBarFactory->expects($this->exactly(2))
381
            ->method('create')
382
            ->willReturn($progressBar);
383
384
        $this->publisher->expects($this->exactly(4))
385
            ->method('publish');
386
387
        $this->exportContactCommandTester->execute(
388
            ['--omitted' => true]
389
        );
390
391
        $this->assertContains(
392
            '4 contact(s) have been scheduled for export.',
393
            $this->exportContactCommandTester->getDisplay()
394
        );
395
396
        $this->assertEquals(Cli::RETURN_SUCCESS, $this->exportContactCommandTester->getStatusCode());
397
    }
398
399
400
    public function testExecuteWithAllOption()
401
    {
402
        $customerIds = [123, 456];
403
        $emails = ['[email protected]', '[email protected]'];
404
405
        $this->configHelper->expects($this->once())
406
            ->method('isEnabled')
407
            ->willReturn(true);
408
409
        $this->configHelper->expects($this->once())
410
            ->method('isContactExportEnabled')
411
            ->willReturn(true);
412
413
        $this->customerCollection->expects($this->never())
414
            ->method('addEmailFilter');
415
416
        $this->customerCollection->expects($this->never())
417
            ->method('addContactOmittedFilter');
418
419
        $this->customerCollection->expects($this->once())
420
            ->method('getAllIds')
421
            ->willReturn($customerIds);
422
423
        $this->subscriberCollection->expects($this->never())
424
            ->method('addEmailFilter');
425
426
        $this->subscriberCollection->expects($this->never())
427
            ->method('addContactOmittedFilter');
428
429
        $this->subscriberCollection->expects($this->once())
430
            ->method('getAllEmails')
431
            ->willReturn($emails);
432
433
        $progressBar = new ProgressBar(new TestOutput());
434
435
        $this->progressBarFactory->expects($this->exactly(2))
436
            ->method('create')
437
            ->willReturn($progressBar);
438
439
        $this->publisher->expects($this->exactly(4))
440
            ->method('publish');
441
442
        $this->exportContactCommandTester->execute(
443
            ['--all' => true]
444
        );
445
446
        $this->assertContains(
447
            '4 contact(s) have been scheduled for export.',
448
            $this->exportContactCommandTester->getDisplay()
449
        );
450
451
        $this->assertEquals(Cli::RETURN_SUCCESS, $this->exportContactCommandTester->getStatusCode());
452
    }
453
}
454