Completed
Pull Request — master (#157)
by Barnabas
02:14
created

NewsletterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Newsletter\Test;
4
5
use DrewM\MailChimp\MailChimp;
6
use Mockery;
7
use PHPUnit\Framework\TestCase;
8
use Spatie\Newsletter\Exceptions\FailedResponse;
9
use Spatie\Newsletter\Newsletter;
10
use Spatie\Newsletter\NewsletterListCollection;
11
12
class NewsletterTest extends TestCase
13
{
14
    /** @var Mockery\Mock */
15
    protected $mailChimpApi;
16
17
    /** @var \Spatie\Newsletter\Newsletter */
18
    protected $newsletter;
19
20
    public function setUp()
21
    {
22
        $this->mailChimpApi = Mockery::mock(MailChimp::class);
23
24
        $newsletterLists = NewsletterListCollection::createFromConfig(
25
            [
26
                'lists' => [
27
                    'list1' => ['id' => 123],
28
                    'list2' => ['id' => 456],
29
                ],
30
                'defaultListName' => 'list1',
31
            ]
32
33
        );
34
35
        $this->newsletter = new Newsletter($this->mailChimpApi, $newsletterLists);
36
    }
37
38
    public function tearDown()
39
    {
40
        parent::tearDown();
41
42
        if ($container = Mockery::getContainer()) {
43
            $this->addToAssertionCount($container->mockery_getExpectationCount());
44
        }
45
46
        Mockery::close();
47
    }
48
49
    /** @test */
50 View Code Duplication
    public function it_can_subscribe_someone()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $email = '[email protected]';
53
54
        $url = 'lists/123/members';
55
56
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
57
        $this->mailChimpApi->shouldReceive('post')->withArgs([
58
            $url,
59
            [
60
                'email_address' => $email,
61
                'status' => 'subscribed',
62
                'email_type' => 'html',
63
            ],
64
        ]);
65
66
        $this->newsletter->subscribe($email);
67
    }
68
69
    /** @test */
70 View Code Duplication
    public function it_throws_an_exception_if_subscribing_someone_fails()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $email = '[email protected]';
73
74
        $url = 'lists/123/members';
75
76
        $this->mailChimpApi->shouldReceive('success')->andReturn(false);
77
        $this->mailChimpApi->shouldReceive('post')->withArgs([
78
            $url,
79
            [
80
                'email_address' => $email,
81
                'status' => 'subscribed',
82
                'email_type' => 'html',
83
            ],
84
        ])->andReturn('failure');
85
86
        try {
87
            $this->newsletter->subscribe($email);
88
        } catch (FailedResponse $e) {
89
            $this->assertEquals('failure', $e->response);
90
            return;
91
        }
92
93
        $this->fail('Expected FailedResponse exception to be thrown');
94
    }
95
96
    /** @test */
97 View Code Duplication
    public function it_can_subscribe_someone_as_pending()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $email = '[email protected]';
100
101
        $url = 'lists/123/members';
102
103
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
104
        $this->mailChimpApi->shouldReceive('post')->withArgs([
105
            $url,
106
            [
107
                'email_address' => $email,
108
                'status' => 'pending',
109
                'email_type' => 'html',
110
            ],
111
        ]);
112
113
        $this->newsletter->subscribePending($email);
114
    }
115
116
    /** @test */
117 View Code Duplication
    public function it_throws_an_exception_if_subscribing_someone_as_pending_fails()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $email = '[email protected]';
120
121
        $url = 'lists/123/members';
122
123
        $this->mailChimpApi->shouldReceive('success')->andReturn(false);
124
        $this->mailChimpApi->shouldReceive('post')->withArgs([
125
            $url,
126
            [
127
                'email_address' => $email,
128
                'status' => 'pending',
129
                'email_type' => 'html',
130
            ],
131
        ])->andReturn('failure');
132
133
        try {
134
            $this->newsletter->subscribePending($email);
135
        } catch (FailedResponse $e) {
136
            $this->assertEquals('failure', $e->response);
137
            return;
138
        }
139
140
        $this->fail('Expected FailedResponse exception to be thrown');
141
    }
142
143
    /** @test */
144 View Code Duplication
    public function it_can_subscribe_or_update_someone()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        $email = '[email protected]';
147
148
        $url = 'lists/123/members';
149
150
        $subscriberHash = 'abc123';
151
152
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
153
        $this->mailChimpApi->shouldReceive('subscriberHash')
154
            ->once()
155
            ->withArgs([$email])
156
            ->andReturn($subscriberHash);
157
158
        $this->mailChimpApi->shouldReceive('put')->withArgs([
159
            "{$url}/{$subscriberHash}",
160
            [
161
                'email_address' => $email,
162
                'status' => 'subscribed',
163
                'email_type' => 'html',
164
            ],
165
        ]);
166
167
        $this->newsletter->subscribeOrUpdate($email);
168
    }
169
170
    /** @test */
171
    public function it_throws_an_exception_if_subscribing_or_updating_someone_fails()
172
    {
173
        $email = '[email protected]';
174
175
        $url = 'lists/123/members';
176
177
        $subscriberHash = 'abc123';
178
179
        $this->mailChimpApi->shouldReceive('success')->andReturn(false);
180
        $this->mailChimpApi->shouldReceive('subscriberHash')
181
            ->once()
182
            ->withArgs([$email])
183
            ->andReturn($subscriberHash);
184
185
        $this->mailChimpApi->shouldReceive('put')->withArgs([
186
            "{$url}/{$subscriberHash}",
187
            [
188
                'email_address' => $email,
189
                'status' => 'subscribed',
190
                'email_type' => 'html',
191
            ],
192
        ])->andReturn('failure');
193
194
        try {
195
            $this->newsletter->subscribeOrUpdate($email);
196
        } catch (FailedResponse $e) {
197
            $this->assertEquals('failure', $e->response);
198
            return;
199
        }
200
201
        $this->fail('Expected FailedResponse exception to be thrown');
202
    }
203
204
    /** @test */
205
    public function it_can_subscribe_someone_with_merge_fields()
206
    {
207
        $email = '[email protected]';
208
209
        $mergeFields = ['FNAME' => 'Freek'];
210
211
        $url = 'lists/123/members';
212
213
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
214
        $this->mailChimpApi->shouldReceive('post')
215
            ->once()
216
            ->withArgs([
217
                $url,
218
                [
219
                    'email_address' => $email,
220
                    'status' => 'subscribed',
221
                    'merge_fields' => $mergeFields,
222
                    'email_type' => 'html',
223
                ],
224
            ]);
225
226
        $this->newsletter->subscribe($email, $mergeFields);
227
    }
228
229
    /** @test */
230
    public function it_can_subscribe_or_update_someone_with_merge_fields()
231
    {
232
        $email = '[email protected]';
233
234
        $mergeFields = ['FNAME' => 'Freek'];
235
236
        $url = 'lists/123/members';
237
238
        $subscriberHash = 'abc123';
239
240
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
241
        $this->mailChimpApi->shouldReceive('subscriberHash')
242
            ->once()
243
            ->withArgs([$email])
244
            ->andReturn($subscriberHash);
245
246
        $this->mailChimpApi->shouldReceive('put')
247
            ->once()
248
            ->withArgs([
249
                "{$url}/{$subscriberHash}",
250
                [
251
                    'email_address' => $email,
252
                    'status' => 'subscribed',
253
                    'merge_fields' => $mergeFields,
254
                    'email_type' => 'html',
255
                ],
256
            ]);
257
258
        $this->newsletter->subscribeOrUpdate($email, $mergeFields);
259
    }
260
261
    /** @test */
262 View Code Duplication
    public function it_can_subscribe_someone_to_an_alternative_list()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
263
    {
264
        $email = '[email protected]';
265
266
        $url = 'lists/456/members';
267
268
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
269
        $this->mailChimpApi->shouldReceive('post')
270
            ->once()
271
            ->withArgs([
272
                $url,
273
                [
274
                    'email_address' => $email,
275
                    'status' => 'subscribed',
276
                    'email_type' => 'html',
277
                ],
278
            ]);
279
280
        $this->newsletter->subscribe($email, [], 'list2');
281
    }
282
283
    /** @test */
284 View Code Duplication
    public function it_can_subscribe_or_update_someone_to_an_alternative_list()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
285
    {
286
        $email = '[email protected]';
287
288
        $url = 'lists/456/members';
289
290
        $subscriberHash = 'abc123';
291
292
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
293
        $this->mailChimpApi->shouldReceive('subscriberHash')
294
            ->once()
295
            ->withArgs([$email])
296
            ->andReturn($subscriberHash);
297
298
        $this->mailChimpApi->shouldReceive('put')
299
            ->once()
300
            ->withArgs([
301
                "{$url}/{$subscriberHash}",
302
                [
303
                    'email_address' => $email,
304
                    'status' => 'subscribed',
305
                    'email_type' => 'html',
306
                ],
307
            ]);
308
309
        $this->newsletter->subscribeOrUpdate($email, [], 'list2');
310
    }
311
312
    /** @test */
313
    public function it_can_override_the_defaults_when_subscribing_someone()
314
    {
315
        $email = '[email protected]';
316
317
        $url = 'lists/123/members';
318
319
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
320
        $this->mailChimpApi->shouldReceive('post')
321
            ->once()
322
            ->withArgs([
323
                $url,
324
                [
325
                    'email_address' => $email,
326
                    'status' => 'pending',
327
                    'email_type' => 'text',
328
                ],
329
            ]);
330
331
        $this->newsletter->subscribe($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
332
    }
333
334
    /** @test */
335
    public function it_can_override_the_defaults_when_subscribing_or_updating_someone()
336
    {
337
        $email = '[email protected]';
338
339
        $url = 'lists/123/members';
340
341
        $subscriberHash = 'abc123';
342
343
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
344
        $this->mailChimpApi->shouldReceive('subscriberHash')
345
            ->once()
346
            ->withArgs([$email])
347
            ->andReturn($subscriberHash);
348
349
        $this->mailChimpApi->shouldReceive('put')
350
            ->once()
351
            ->withArgs([
352
                "{$url}/{$subscriberHash}",
353
                [
354
                    'email_address' => $email,
355
                    'status' => 'pending',
356
                    'email_type' => 'text',
357
                ],
358
            ]);
359
360
        $this->newsletter->subscribeOrUpdate($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
361
    }
362
363
    /** @test */
364
    public function it_can_change_the_email_address_of_a_subscriber()
365
    {
366
        $email = '[email protected]';
367
        $newEmail = '[email protected]';
368
369
        $url = 'lists/123/members';
370
371
        $subscriberHash = 'abc123';
372
373
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
374
        $this->mailChimpApi->shouldReceive('subscriberHash')
375
            ->once()
376
            ->withArgs([$email])
377
            ->andReturn($subscriberHash);
378
379
        $this->mailChimpApi
380
            ->shouldReceive('patch')
381
            ->once()
382
            ->withArgs([
383
                "{$url}/{$subscriberHash}",
384
                [
385
                    'email_address' => $newEmail,
386
                ],
387
            ]);
388
389
        $this->newsletter->updateEmailAddress($email, $newEmail);
390
    }
391
392
    /** @test */
393
    public function it_can_unsubscribe_someone()
394
    {
395
        $email = '[email protected]';
396
397
        $subscriberHash = 'abc123';
398
399
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
400
        $this->mailChimpApi->shouldReceive('subscriberHash')
401
            ->once()
402
            ->withArgs([$email])
403
            ->andReturn($subscriberHash);
404
405
        $this->mailChimpApi
406
            ->shouldReceive('patch')
407
            ->once()
408
            ->withArgs([
409
                "lists/123/members/{$subscriberHash}",
410
                [
411
                    'status' => 'unsubscribed',
412
                ],
413
            ]);
414
415
        $this->newsletter->unsubscribe('[email protected]');
416
    }
417
418
    /** @test */
419
    public function it_throws_an_exception_if_trying_to_unsubscribe_someone_fails()
420
    {
421
        $email = '[email protected]';
422
423
        $subscriberHash = 'abc123';
424
425
        $this->mailChimpApi->shouldReceive('success')->andReturn(false);
426
        $this->mailChimpApi->shouldReceive('subscriberHash')
427
            ->once()
428
            ->withArgs([$email])
429
            ->andReturn($subscriberHash);
430
431
        $this->mailChimpApi
432
            ->shouldReceive('patch')
433
            ->once()
434
            ->withArgs([
435
                "lists/123/members/{$subscriberHash}",
436
                [
437
                    'status' => 'unsubscribed',
438
                ],
439
            ])->andReturn('failure');
440
441
        try {
442
            $this->newsletter->unsubscribe('[email protected]');
443
        } catch (FailedResponse $e) {
444
            $this->assertEquals('failure', $e->response);
445
            return;
446
        }
447
448
        $this->fail('Expected FailedResponse exception to be thrown');
449
    }
450
451
    /** @test */
452
    public function it_can_unsubscribe_someone_from_a_specific_list()
453
    {
454
        $email = '[email protected]';
455
456
        $subscriberHash = 'abc123';
457
458
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
459
        $this->mailChimpApi->shouldReceive('subscriberHash')
460
            ->once()
461
            ->withArgs([$email])
462
            ->andReturn($subscriberHash);
463
464
        $this->mailChimpApi
465
            ->shouldReceive('patch')
466
            ->once()
467
            ->withArgs([
468
                "lists/456/members/{$subscriberHash}",
469
                [
470
                    'status' => 'unsubscribed',
471
                ],
472
            ]);
473
474
        $this->newsletter->unsubscribe('[email protected]', 'list2');
475
    }
476
477
    /** @test */
478 View Code Duplication
    public function it_can_delete_someone()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
479
    {
480
        $email = '[email protected]';
481
482
        $subscriberHash = 'abc123';
483
484
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
485
        $this->mailChimpApi->shouldReceive('subscriberHash')
486
            ->once()
487
            ->withArgs([$email])
488
            ->andReturn($subscriberHash);
489
490
        $this->mailChimpApi
491
            ->shouldReceive('delete')
492
            ->once()
493
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
494
495
        $this->newsletter->delete('[email protected]');
496
    }
497
498
    /** @test */
499 View Code Duplication
    public function it_can_delete_someone_from_a_specific_list()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
500
    {
501
        $email = '[email protected]';
502
503
        $subscriberHash = 'abc123';
504
505
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
506
        $this->mailChimpApi->shouldReceive('subscriberHash')
507
            ->once()
508
            ->withArgs([$email])
509
            ->andReturn($subscriberHash);
510
511
        $this->mailChimpApi
512
            ->shouldReceive('delete')
513
            ->once()
514
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
515
516
        $this->newsletter->delete('[email protected]', 'list2');
517
    }
518
519
    /** @test */
520
    public function it_exposes_the_api()
521
    {
522
        $api = $this->newsletter->getApi();
523
524
        $this->assertSame($this->mailChimpApi, $api);
525
    }
526
527
    /** @test */
528
    public function it_can_get_the_list_members()
529
    {
530
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
531
        $this->mailChimpApi
532
            ->shouldReceive('get')
533
            ->once()
534
            ->withArgs(['lists/123/members', []]);
535
536
        $this->newsletter->getMembers();
537
    }
538
539
    /** @test */
540 View Code Duplication
    public function it_can_get_the_member()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
541
    {
542
        $email = '[email protected]';
543
544
        $subscriberHash = 'abc123';
545
546
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
547
        $this->mailChimpApi->shouldReceive('subscriberHash')
548
            ->once()
549
            ->withArgs([$email])
550
            ->andReturn($subscriberHash);
551
552
        $this->mailChimpApi
553
            ->shouldReceive('get')
554
            ->once()
555
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
556
557
        $this->newsletter->getMember($email);
558
    }
559
560
    /** @test */
561 View Code Duplication
    public function it_can_get_the_member_activity()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
562
    {
563
        $email = '[email protected]';
564
565
        $subscriberHash = 'abc123';
566
567
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
568
        $this->mailChimpApi->shouldReceive('subscriberHash')
569
            ->once()
570
            ->withArgs([$email])
571
            ->andReturn($subscriberHash);
572
573
        $this->mailChimpApi
574
            ->shouldReceive('get')
575
            ->once()
576
            ->withArgs(["lists/123/members/{$subscriberHash}/activity"]);
577
578
        $this->newsletter->getMemberActivity($email);
579
    }
580
581
    /** @test */
582 View Code Duplication
    public function it_can_get_the_member_from_a_specific_list()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
583
    {
584
        $email = '[email protected]';
585
586
        $subscriberHash = 'abc123';
587
588
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
589
        $this->mailChimpApi->shouldReceive('subscriberHash')
590
            ->once()
591
            ->withArgs([$email])
592
            ->andReturn($subscriberHash);
593
594
        $this->mailChimpApi
595
            ->shouldReceive('get')
596
            ->once()
597
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
598
599
        $this->newsletter->getMember($email, 'list2');
600
    }
601
602
    /** @test */
603
    public function is_can_create_a_campaign()
604
    {
605
        $fromName = 'Spatie';
606
        $replyTo = '[email protected]';
607
        $subject = 'This is a subject';
608
        $html = '<b>This is the content</b>';
609
        $listName = 'list1';
610
        $options = ['extraOption' => 'extraValue'];
611
        $contentOptions = ['plain text' => 'this is the plain text content'];
612
613
        $campaignId = 'newCampaignId';
614
615
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
616
        $this->mailChimpApi
617
            ->shouldReceive('post')
618
            ->once()
619
            ->withArgs(
620
                [
621
                    'campaigns',
622
                    [
623
                        'type' => 'regular',
624
                        'recipients' => [
625
                            'list_id' => 123,
626
                        ],
627
                        'settings' => [
628
                            'subject_line' => $subject,
629
                            'from_name' => $fromName,
630
                            'reply_to' => $replyTo,
631
                        ],
632
                        'extraOption' => 'extraValue',
633
                    ],
634
                ]
635
            )
636
            ->andReturn(['id' => $campaignId]);
637
638
        $this->mailChimpApi
639
            ->shouldReceive('put')
640
            ->once()
641
            ->withArgs([
642
                "campaigns/{$campaignId}/content",
643
                [
644
                    'html' => $html,
645
                    'plain text' => 'this is the plain text content',
646
                ],
647
            ]);
648
649
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
650
    }
651
652
    /** @test */
653
    public function it_throws_an_exception_if_creating_a_campaign_fails()
654
    {
655
        $fromName = 'Spatie';
656
        $replyTo = '[email protected]';
657
        $subject = 'This is a subject';
658
        $html = '<b>This is the content</b>';
659
        $listName = 'list1';
660
        $options = ['extraOption' => 'extraValue'];
661
        $contentOptions = ['plain text' => 'this is the plain text content'];
662
663
        $this->mailChimpApi->shouldReceive('success')->andReturn(false);
664
        $this->mailChimpApi
665
            ->shouldReceive('post')
666
            ->once()
667
            ->withArgs(
668
                [
669
                    'campaigns',
670
                    [
671
                        'type' => 'regular',
672
                        'recipients' => [
673
                            'list_id' => 123,
674
                        ],
675
                        'settings' => [
676
                            'subject_line' => $subject,
677
                            'from_name' => $fromName,
678
                            'reply_to' => $replyTo,
679
                        ],
680
                        'extraOption' => 'extraValue',
681
                    ],
682
                ]
683
            )
684
            ->andReturn('failure');
685
686
        try {
687
            $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
688
        } catch (FailedResponse $e) {
689
            $this->assertEquals('failure', $e->response);
690
            return;
691
        }
692
693
        $this->fail('Expected FailedResponse exception to be thrown');
694
    }
695
696
    /** @test */
697
    public function it_throws_an_exception_if_updating_a_campaigns_content_fails()
698
    {
699
        $fromName = 'Spatie';
700
        $replyTo = '[email protected]';
701
        $subject = 'This is a subject';
702
        $html = '<b>This is the content</b>';
703
        $listName = 'list1';
704
        $options = ['extraOption' => 'extraValue'];
705
        $contentOptions = ['plain text' => 'this is the plain text content'];
706
707
        $campaignId = 'newCampaignId';
708
709
        $this->mailChimpApi->shouldReceive('success')->andReturn(true, false);
710
        $this->mailChimpApi
711
            ->shouldReceive('post')
712
            ->once()
713
            ->withArgs(
714
                [
715
                    'campaigns',
716
                    [
717
                        'type' => 'regular',
718
                        'recipients' => [
719
                            'list_id' => 123,
720
                        ],
721
                        'settings' => [
722
                            'subject_line' => $subject,
723
                            'from_name' => $fromName,
724
                            'reply_to' => $replyTo,
725
                        ],
726
                        'extraOption' => 'extraValue',
727
                    ],
728
                ]
729
            )
730
            ->andReturn(['id' => $campaignId]);
731
732
        $this->mailChimpApi
733
            ->shouldReceive('put')
734
            ->once()
735
            ->withArgs([
736
                "campaigns/{$campaignId}/content",
737
                [
738
                    'html' => $html,
739
                    'plain text' => 'this is the plain text content',
740
                ],
741
            ])->andReturn('failure');
742
743
        try {
744
            $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
745
        } catch (FailedResponse $e) {
746
            $this->assertEquals('failure', $e->response);
747
            return;
748
        }
749
750
        $this->fail('Expected FailedResponse exception to be thrown');
751
    }
752
}
753