Completed
Push — master ( f34b50...86fc0d )
by Freek
04:55 queued 33s
created

NewsletterTest::it_can_remove_member_tags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.584
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_exposes_the_api()
387
    {
388
        $api = $this->newsletter->getApi();
389
390
        $this->assertSame($this->mailChimpApi, $api);
391
    }
392
393
    /** @test */
394
    public function it_can_get_the_list_members()
395
    {
396
        $this->mailChimpApi
397
            ->shouldReceive('get')
398
            ->once()
399
            ->withArgs(['lists/123/members', []]);
400
401
        $this->newsletter->getMembers();
402
    }
403
404
    /** @test */
405 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...
406
    {
407
        $email = '[email protected]';
408
409
        $subscriberHash = 'abc123';
410
411
        $this->mailChimpApi->shouldReceive('subscriberHash')
412
            ->once()
413
            ->withArgs([$email])
414
            ->andReturn($subscriberHash);
415
416
        $this->mailChimpApi
417
            ->shouldReceive('get')
418
            ->once()
419
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
420
421
        $this->newsletter->getMember($email);
422
    }
423
424
    /** @test */
425 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...
426
    {
427
        $email = '[email protected]';
428
429
        $subscriberHash = 'abc123';
430
431
        $this->mailChimpApi->shouldReceive('subscriberHash')
432
            ->once()
433
            ->withArgs([$email])
434
            ->andReturn($subscriberHash);
435
436
        $this->mailChimpApi
437
            ->shouldReceive('get')
438
            ->once()
439
            ->withArgs(["lists/123/members/{$subscriberHash}/activity"]);
440
441
        $this->newsletter->getMemberActivity($email);
442
    }
443
444
    /** @test */
445 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...
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/456/members/{$subscriberHash}"]);
460
461
        $this->newsletter->getMember($email, 'list2');
462
    }
463
464
    /** @test */
465
    public function it_can_create_a_campaign()
466
    {
467
        $fromName = 'Spatie';
468
        $replyTo = '[email protected]';
469
        $subject = 'This is a subject';
470
        $html = '<b>This is the content</b>';
471
        $listName = 'list1';
472
        $options = ['extraOption' => 'extraValue'];
473
        $contentOptions = ['plain text' => 'this is the plain text content'];
474
475
        $campaignId = 'newCampaignId';
476
477
        $this->mailChimpApi
478
            ->shouldReceive('post')
479
            ->once()
480
            ->withArgs(
481
                [
482
                    'campaigns',
483
                    [
484
                        'type' => 'regular',
485
                        'recipients' => [
486
                            'list_id' => 123,
487
                        ],
488
                        'settings' => [
489
                            'subject_line' => $subject,
490
                            'from_name' => $fromName,
491
                            'reply_to' => $replyTo,
492
                        ],
493
                        'extraOption' => 'extraValue',
494
                    ],
495
                ]
496
            )
497
            ->andReturn(['id' => $campaignId]);
498
499
        $this->mailChimpApi
500
            ->shouldReceive('put')
501
            ->once()
502
            ->withArgs([
503
                "campaigns/{$campaignId}/content",
504
                [
505
                    'html' => $html,
506
                    'plain text' => 'this is the plain text content',
507
                ],
508
            ]);
509
510
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
511
    }
512
513
    /** @test */
514
    public function it_can_get_member_tags()
515
    {
516
        $email = '[email protected]';
517
518
        $subscriberHash = 'abc123';
519
520
        $this->mailChimpApi->shouldReceive('subscriberHash')
521
            ->once()
522
            ->withArgs([$email])
523
            ->andReturn($subscriberHash);
524
525
        $this->mailChimpApi
526
            ->shouldReceive('get')
527
            ->once()
528
            ->withArgs(["lists/123/members/{$subscriberHash}/tags"])
529
            ->andReturn('all-the-member-tags');
530
531
        $actual = $this->newsletter->getTags($email);
532
533
        $this->assertSame('all-the-member-tags', $actual);
534
    }
535
536
    /** @test */
537 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...
538
    {
539
        $email = '[email protected]';
540
541
        $subscriberHash = 'abc123';
542
543
        $this->mailChimpApi->shouldReceive('subscriberHash')
544
            ->once()
545
            ->withArgs([$email])
546
            ->andReturn($subscriberHash);
547
548
        $this->mailChimpApi
549
            ->shouldReceive('post')
550
            ->once()
551
            ->withArgs(["lists/123/members/{$subscriberHash}/tags", ['tags' => ['tag-1' => 'active', 'tag-2' => 'active']]])
552
            ->andReturn('the-post-response');
553
554
        $actual = $this->newsletter->addTags(['tag-1', 'tag-2'], $email);
555
556
        $this->assertSame('the-post-response', $actual);
557
    }
558
559
    /** @test */
560 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...
561
    {
562
        $email = '[email protected]';
563
564
        $subscriberHash = 'abc123';
565
566
        $this->mailChimpApi->shouldReceive('subscriberHash')
567
            ->once()
568
            ->withArgs([$email])
569
            ->andReturn($subscriberHash);
570
571
        $this->mailChimpApi
572
            ->shouldReceive('post')
573
            ->once()
574
            ->withArgs(["lists/123/members/{$subscriberHash}/tags", ['tags' => ['tag-1' => 'inactive', 'tag-2' => 'inactive']]])
575
            ->andReturn('the-post-response');
576
577
        $actual = $this->newsletter->removeTags(['tag-1', 'tag-2'], $email);
578
579
        $this->assertSame('the-post-response', $actual);
580
    }
581
}
582