Completed
Push — master ( caba9f...68a289 )
by Rias
04:00 queued 10s
created

it_can_delete_someone_permanently()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Newsletter\Test;
4
5
use Mockery;
6
use DrewM\MailChimp\MailChimp;
7
use PHPUnit\Framework\TestCase;
8
use Spatie\Newsletter\Newsletter;
9
use Spatie\Newsletter\NewsletterListCollection;
10
11
class NewsletterTest extends TestCase
12
{
13
    /** @var Mockery\Mock */
14
    protected $mailChimpApi;
15
16
    /** @var \Spatie\Newsletter\Newsletter */
17
    protected $newsletter;
18
19
    public function setUp() : void
20
    {
21
        $this->mailChimpApi = Mockery::mock(MailChimp::class);
22
23
        $this->mailChimpApi->shouldReceive('success')->andReturn(true);
24
25
        $newsletterLists = NewsletterListCollection::createFromConfig(
26
            [
27
                'lists' => [
28
                    'list1' => ['id' => 123],
29
                    'list2' => ['id' => 456],
30
                ],
31
                'defaultListName' => 'list1',
32
            ]
33
34
        );
35
36
        $this->newsletter = new Newsletter($this->mailChimpApi, $newsletterLists);
37
    }
38
39
    public function tearDown() : void
40
    {
41
        parent::tearDown();
42
43
        if ($container = Mockery::getContainer()) {
44
            $this->addToAssertionCount($container->mockery_getExpectationCount());
45
        }
46
47
        Mockery::close();
48
    }
49
50
    /** @test */
51 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...
52
    {
53
        $email = '[email protected]';
54
55
        $url = 'lists/123/members';
56
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_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...
71
    {
72
        $email = '[email protected]';
73
74
        $url = 'lists/123/members';
75
76
        $this->mailChimpApi->shouldReceive('post')->withArgs([
77
            $url,
78
            [
79
                'email_address' => $email,
80
                'status' => 'pending',
81
                'email_type' => 'html',
82
            ],
83
        ]);
84
85
        $this->newsletter->subscribePending($email);
86
    }
87
88
    /** @test */
89 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...
90
    {
91
        $email = '[email protected]';
92
93
        $url = 'lists/123/members';
94
95
        $subscriberHash = 'abc123';
96
97
        $this->mailChimpApi->shouldReceive('subscriberHash')
98
            ->once()
99
            ->withArgs([$email])
100
            ->andReturn($subscriberHash);
101
102
        $this->mailChimpApi->shouldReceive('put')->withArgs([
103
            "{$url}/{$subscriberHash}",
104
            [
105
                'email_address' => $email,
106
                'status' => 'subscribed',
107
                'email_type' => 'html',
108
            ],
109
        ]);
110
111
        $this->newsletter->subscribeOrUpdate($email);
112
    }
113
114
    /** @test */
115
    public function it_can_subscribe_someone_with_merge_fields()
116
    {
117
        $email = '[email protected]';
118
119
        $mergeFields = ['FNAME' => 'Freek'];
120
121
        $url = 'lists/123/members';
122
123
        $this->mailChimpApi->shouldReceive('post')
124
            ->once()
125
            ->withArgs([
126
                $url,
127
                [
128
                    'email_address' => $email,
129
                    'status' => 'subscribed',
130
                    'merge_fields' => $mergeFields,
131
                    'email_type' => 'html',
132
                ],
133
            ]);
134
135
        $this->newsletter->subscribe($email, $mergeFields);
136
    }
137
138
    /** @test */
139
    public function it_can_subscribe_or_update_someone_with_merge_fields()
140
    {
141
        $email = '[email protected]';
142
143
        $mergeFields = ['FNAME' => 'Freek'];
144
145
        $url = 'lists/123/members';
146
147
        $subscriberHash = 'abc123';
148
149
        $this->mailChimpApi->shouldReceive('subscriberHash')
150
            ->once()
151
            ->withArgs([$email])
152
            ->andReturn($subscriberHash);
153
154
        $this->mailChimpApi->shouldReceive('put')
155
            ->once()
156
            ->withArgs([
157
                "{$url}/{$subscriberHash}",
158
                [
159
                    'email_address' => $email,
160
                    'status' => 'subscribed',
161
                    'merge_fields' => $mergeFields,
162
                    'email_type' => 'html',
163
                ],
164
            ]);
165
166
        $this->newsletter->subscribeOrUpdate($email, $mergeFields);
167
    }
168
169
    /** @test */
170 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...
171
    {
172
        $email = '[email protected]';
173
174
        $url = 'lists/456/members';
175
176
        $this->mailChimpApi->shouldReceive('post')
177
            ->once()
178
            ->withArgs([
179
                $url,
180
                [
181
                    'email_address' => $email,
182
                    'status' => 'subscribed',
183
                    'email_type' => 'html',
184
                ],
185
            ]);
186
187
        $this->newsletter->subscribe($email, [], 'list2');
188
    }
189
190
    /** @test */
191 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...
192
    {
193
        $email = '[email protected]';
194
195
        $url = 'lists/456/members';
196
197
        $subscriberHash = 'abc123';
198
199
        $this->mailChimpApi->shouldReceive('subscriberHash')
200
            ->once()
201
            ->withArgs([$email])
202
            ->andReturn($subscriberHash);
203
204
        $this->mailChimpApi->shouldReceive('put')
205
            ->once()
206
            ->withArgs([
207
                "{$url}/{$subscriberHash}",
208
                [
209
                    'email_address' => $email,
210
                    'status' => 'subscribed',
211
                    'email_type' => 'html',
212
                ],
213
            ]);
214
215
        $this->newsletter->subscribeOrUpdate($email, [], 'list2');
216
    }
217
218
    /** @test */
219
    public function it_can_override_the_defaults_when_subscribing_someone()
220
    {
221
        $email = '[email protected]';
222
223
        $url = 'lists/123/members';
224
225
        $this->mailChimpApi->shouldReceive('post')
226
            ->once()
227
            ->withArgs([
228
                $url,
229
                [
230
                    'email_address' => $email,
231
                    'status' => 'pending',
232
                    'email_type' => 'text',
233
                ],
234
            ]);
235
236
        $this->newsletter->subscribe($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
237
    }
238
239
    /** @test */
240
    public function it_can_override_the_defaults_when_subscribing_or_updating_someone()
241
    {
242
        $email = '[email protected]';
243
244
        $url = 'lists/123/members';
245
246
        $subscriberHash = 'abc123';
247
248
        $this->mailChimpApi->shouldReceive('subscriberHash')
249
            ->once()
250
            ->withArgs([$email])
251
            ->andReturn($subscriberHash);
252
253
        $this->mailChimpApi->shouldReceive('put')
254
            ->once()
255
            ->withArgs([
256
                "{$url}/{$subscriberHash}",
257
                [
258
                    'email_address' => $email,
259
                    'status' => 'pending',
260
                    'email_type' => 'text',
261
                ],
262
            ]);
263
264
        $this->newsletter->subscribeOrUpdate($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
265
    }
266
267
    /** @test */
268
    public function it_can_change_the_email_address_of_a_subscriber()
269
    {
270
        $email = '[email protected]';
271
        $newEmail = '[email protected]';
272
273
        $url = 'lists/123/members';
274
275
        $subscriberHash = 'abc123';
276
277
        $this->mailChimpApi->shouldReceive('subscriberHash')
278
            ->once()
279
            ->withArgs([$email])
280
            ->andReturn($subscriberHash);
281
282
        $this->mailChimpApi
283
            ->shouldReceive('patch')
284
            ->once()
285
            ->withArgs([
286
                "{$url}/{$subscriberHash}",
287
                [
288
                    'email_address' => $newEmail,
289
                ],
290
            ]);
291
292
        $this->newsletter->updateEmailAddress($email, $newEmail);
293
    }
294
295
    /** @test */
296
    public function it_can_unsubscribe_someone()
297
    {
298
        $email = '[email protected]';
299
300
        $subscriberHash = 'abc123';
301
302
        $this->mailChimpApi->shouldReceive('subscriberHash')
303
            ->once()
304
            ->withArgs([$email])
305
            ->andReturn($subscriberHash);
306
307
        $this->mailChimpApi
308
            ->shouldReceive('patch')
309
            ->once()
310
            ->withArgs([
311
                "lists/123/members/{$subscriberHash}",
312
                [
313
                    'status' => 'unsubscribed',
314
                ],
315
            ]);
316
317
        $this->newsletter->unsubscribe('[email protected]');
318
    }
319
320
    /** @test */
321
    public function it_can_unsubscribe_someone_from_a_specific_list()
322
    {
323
        $email = '[email protected]';
324
325
        $subscriberHash = 'abc123';
326
327
        $this->mailChimpApi->shouldReceive('subscriberHash')
328
            ->once()
329
            ->withArgs([$email])
330
            ->andReturn($subscriberHash);
331
332
        $this->mailChimpApi
333
            ->shouldReceive('patch')
334
            ->once()
335
            ->withArgs([
336
                "lists/456/members/{$subscriberHash}",
337
                [
338
                    'status' => 'unsubscribed',
339
                ],
340
            ]);
341
342
        $this->newsletter->unsubscribe('[email protected]', 'list2');
343
    }
344
345
    /** @test */
346 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...
347
    {
348
        $email = '[email protected]';
349
350
        $subscriberHash = 'abc123';
351
352
        $this->mailChimpApi->shouldReceive('subscriberHash')
353
            ->once()
354
            ->withArgs([$email])
355
            ->andReturn($subscriberHash);
356
357
        $this->mailChimpApi
358
            ->shouldReceive('delete')
359
            ->once()
360
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
361
362
        $this->newsletter->delete('[email protected]');
363
    }
364
365
    /** @test */
366 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...
367
    {
368
        $email = '[email protected]';
369
370
        $subscriberHash = 'abc123';
371
372
        $this->mailChimpApi->shouldReceive('subscriberHash')
373
            ->once()
374
            ->withArgs([$email])
375
            ->andReturn($subscriberHash);
376
377
        $this->mailChimpApi
378
            ->shouldReceive('delete')
379
            ->once()
380
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
381
382
        $this->newsletter->delete('[email protected]', 'list2');
383
    }
384
385
    /** @test */
386
    public function it_can_delete_someone_permanently()
387
    {
388
        $email = '[email protected]';
389
390
        $subscriberHash = 'abc123';
391
392
        $this->mailChimpApi->shouldReceive('subscriberHash')
393
            ->once()
394
            ->withArgs([$email])
395
            ->andReturn($subscriberHash);
396
397
        $this->mailChimpApi
398
            ->shouldReceive('post')
399
            ->once()
400
            ->withArgs(["lists/123/members/{$subscriberHash}/actions/delete-permanent"]);
401
402
        $this->newsletter->deletePermanently('[email protected]');
403
    }
404
405
    /** @test */
406
    public function it_can_delete_someone_permanently_from_a_specific_list()
407
    {
408
        $email = '[email protected]';
409
410
        $subscriberHash = 'abc123';
411
412
        $this->mailChimpApi->shouldReceive('subscriberHash')
413
            ->once()
414
            ->withArgs([$email])
415
            ->andReturn($subscriberHash);
416
417
        $this->mailChimpApi
418
            ->shouldReceive('post')
419
            ->once()
420
            ->withArgs(["lists/456/members/{$subscriberHash}/actions/delete-permanent"]);
421
422
        $this->newsletter->deletePermanently('[email protected]', 'list2');
423
    }
424
425
    /** @test */
426
    public function it_exposes_the_api()
427
    {
428
        $api = $this->newsletter->getApi();
429
430
        $this->assertSame($this->mailChimpApi, $api);
431
    }
432
433
    /** @test */
434
    public function it_can_get_the_list_members()
435
    {
436
        $this->mailChimpApi
437
            ->shouldReceive('get')
438
            ->once()
439
            ->withArgs(['lists/123/members', []]);
440
441
        $this->newsletter->getMembers();
442
    }
443
444
    /** @test */
445 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...
446
    {
447
        $email = '[email protected]';
448
449
        $subscriberHash = 'abc123';
450
451
        $this->mailChimpApi->shouldReceive('subscriberHash')
452
            ->once()
453
            ->withArgs([$email])
454
            ->andReturn($subscriberHash);
455
456
        $this->mailChimpApi
457
            ->shouldReceive('get')
458
            ->once()
459
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
460
461
        $this->newsletter->getMember($email);
462
    }
463
464
    /** @test */
465 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...
466
    {
467
        $email = '[email protected]';
468
469
        $subscriberHash = 'abc123';
470
471
        $this->mailChimpApi->shouldReceive('subscriberHash')
472
            ->once()
473
            ->withArgs([$email])
474
            ->andReturn($subscriberHash);
475
476
        $this->mailChimpApi
477
            ->shouldReceive('get')
478
            ->once()
479
            ->withArgs(["lists/123/members/{$subscriberHash}/activity"]);
480
481
        $this->newsletter->getMemberActivity($email);
482
    }
483
484
    /** @test */
485 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...
486
    {
487
        $email = '[email protected]';
488
489
        $subscriberHash = 'abc123';
490
491
        $this->mailChimpApi->shouldReceive('subscriberHash')
492
            ->once()
493
            ->withArgs([$email])
494
            ->andReturn($subscriberHash);
495
496
        $this->mailChimpApi
497
            ->shouldReceive('get')
498
            ->once()
499
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
500
501
        $this->newsletter->getMember($email, 'list2');
502
    }
503
504
    /** @test */
505
    public function it_can_create_a_campaign()
506
    {
507
        $fromName = 'Spatie';
508
        $replyTo = '[email protected]';
509
        $subject = 'This is a subject';
510
        $html = '<b>This is the content</b>';
511
        $listName = 'list1';
512
        $options = ['extraOption' => 'extraValue'];
513
        $contentOptions = ['plain text' => 'this is the plain text content'];
514
515
        $campaignId = 'newCampaignId';
516
517
        $this->mailChimpApi
518
            ->shouldReceive('post')
519
            ->once()
520
            ->withArgs(
521
                [
522
                    'campaigns',
523
                    [
524
                        'type' => 'regular',
525
                        'recipients' => [
526
                            'list_id' => 123,
527
                        ],
528
                        'settings' => [
529
                            'subject_line' => $subject,
530
                            'from_name' => $fromName,
531
                            'reply_to' => $replyTo,
532
                        ],
533
                        'extraOption' => 'extraValue',
534
                    ],
535
                ]
536
            )
537
            ->andReturn(['id' => $campaignId]);
538
539
        $this->mailChimpApi
540
            ->shouldReceive('put')
541
            ->once()
542
            ->withArgs([
543
                "campaigns/{$campaignId}/content",
544
                [
545
                    'html' => $html,
546
                    'plain text' => 'this is the plain text content',
547
                ],
548
            ]);
549
550
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
551
    }
552
553
    /** @test */
554
    public function it_can_get_member_tags()
555
    {
556
        $email = '[email protected]';
557
558
        $subscriberHash = 'abc123';
559
560
        $this->mailChimpApi->shouldReceive('subscriberHash')
561
            ->once()
562
            ->withArgs([$email])
563
            ->andReturn($subscriberHash);
564
565
        $this->mailChimpApi
566
            ->shouldReceive('get')
567
            ->once()
568
            ->withArgs(["lists/123/members/{$subscriberHash}/tags"])
569
            ->andReturn('all-the-member-tags');
570
571
        $actual = $this->newsletter->getTags($email);
572
573
        $this->assertSame('all-the-member-tags', $actual);
574
    }
575
576
    /** @test */
577 View Code Duplication
    public function it_can_add_member_tags()
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...
578
    {
579
        $email = '[email protected]';
580
581
        $subscriberHash = 'abc123';
582
583
        $this->mailChimpApi->shouldReceive('subscriberHash')
584
            ->once()
585
            ->withArgs([$email])
586
            ->andReturn($subscriberHash);
587
588
        $this->mailChimpApi
589
            ->shouldReceive('post')
590
            ->once()
591
            ->withArgs(["lists/123/members/{$subscriberHash}/tags", ['tags' => [['name' => 'tag-1', 'status' => 'active'], ['name' => 'tag-2', 'status' => 'active']]]])
592
            ->andReturn('the-post-response');
593
594
        $actual = $this->newsletter->addTags(['tag-1', 'tag-2'], $email);
595
596
        $this->assertSame('the-post-response', $actual);
597
    }
598
599
    /** @test */
600 View Code Duplication
    public function it_can_remove_member_tags()
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...
601
    {
602
        $email = '[email protected]';
603
604
        $subscriberHash = 'abc123';
605
606
        $this->mailChimpApi->shouldReceive('subscriberHash')
607
            ->once()
608
            ->withArgs([$email])
609
            ->andReturn($subscriberHash);
610
611
        $this->mailChimpApi
612
            ->shouldReceive('post')
613
            ->once()
614
            ->withArgs(["lists/123/members/{$subscriberHash}/tags", ['tags' => [['name' => 'tag-1', 'status' => 'inactive'], ['name' => 'tag-2', 'status' => 'inactive']]]])
615
            ->andReturn('the-post-response');
616
617
        $actual = $this->newsletter->removeTags(['tag-1', 'tag-2'], $email);
618
619
        $this->assertSame('the-post-response', $actual);
620
    }
621
}
622