Completed
Push — master ( 7426f2...097ac5 )
by Freek
01:17
created

NewsletterTest::it_can_get_the_member_activity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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 PHPUnit_Framework_TestCase
12
{
13
    /** @var Mockery\Mock */
14
    protected $mailChimpApi;
15
16
    /** @var \Spatie\Newsletter\Newsletter */
17
    protected $newsletter;
18
19
    public function setUp()
20
    {
21
        $this->mailChimpApi = Mockery::mock(MailChimp::class);
22
23
        $this->mailChimpApi->shouldReceive('getLastError')->andReturn(false);
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()
40
    {
41
        Mockery::close();
42
43
        parent::tearDown();
44
    }
45
46
    /** @test */
47 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...
48
    {
49
        $email = '[email protected]';
50
51
        $url = 'lists/123/members';
52
53
        $this->mailChimpApi->shouldReceive('post')->withArgs([
54
            $url,
55
            [
56
                'email_address' => $email,
57
                'status' => 'subscribed',
58
                'email_type' => 'html',
59
            ],
60
        ]);
61
62
        $this->newsletter->subscribe($email);
63
    }
64
65
    /** @test */
66 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...
67
    {
68
        $email = '[email protected]';
69
70
        $url = 'lists/123/members';
71
72
        $subscriberHash = 'abc123';
73
74
        $this->mailChimpApi->shouldReceive('subscriberHash')
75
            ->once()
76
            ->withArgs([$email])
77
            ->andReturn($subscriberHash);
78
79
        $this->mailChimpApi->shouldReceive('put')->withArgs([
80
            "{$url}/{$subscriberHash}",
81
            [
82
                'email_address' => $email,
83
                'status' => 'subscribed',
84
                'email_type' => 'html',
85
            ],
86
        ]);
87
88
        $this->newsletter->subscribeOrUpdate($email);
89
    }
90
91
    /** @test */
92
    public function it_can_subscribe_someone_with_merge_fields()
93
    {
94
        $email = '[email protected]';
95
96
        $mergeFields = ['FNAME' => 'Freek'];
97
98
        $url = 'lists/123/members';
99
100
        $this->mailChimpApi->shouldReceive('post')
101
            ->once()
102
            ->withArgs([
103
                $url,
104
                [
105
                    'email_address' => $email,
106
                    'status' => 'subscribed',
107
                    'merge_fields' => $mergeFields,
108
                    'email_type' => 'html',
109
                ],
110
            ]);
111
112
        $this->newsletter->subscribe($email, $mergeFields);
113
    }
114
115
    /** @test */
116
    public function it_can_subscribe_or_update_someone_with_merge_fields()
117
    {
118
        $email = '[email protected]';
119
120
        $mergeFields = ['FNAME' => 'Freek'];
121
122
        $url = 'lists/123/members';
123
124
        $subscriberHash = 'abc123';
125
126
        $this->mailChimpApi->shouldReceive('subscriberHash')
127
            ->once()
128
            ->withArgs([$email])
129
            ->andReturn($subscriberHash);
130
131
        $this->mailChimpApi->shouldReceive('put')
132
            ->once()
133
            ->withArgs([
134
                "{$url}/{$subscriberHash}",
135
                [
136
                    'email_address' => $email,
137
                    'status' => 'subscribed',
138
                    'merge_fields' => $mergeFields,
139
                    'email_type' => 'html',
140
                ],
141
            ]);
142
143
        $this->newsletter->subscribeOrUpdate($email, $mergeFields);
144
    }
145
146
    /** @test */
147 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...
148
    {
149
        $email = '[email protected]';
150
151
        $url = 'lists/456/members';
152
153
        $this->mailChimpApi->shouldReceive('post')
154
            ->once()
155
            ->withArgs([
156
                $url,
157
                [
158
                    'email_address' => $email,
159
                    'status' => 'subscribed',
160
                    'email_type' => 'html',
161
                ],
162
            ]);
163
164
        $this->newsletter->subscribe($email, [], 'list2');
165
    }
166
167
    /** @test */
168 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...
169
    {
170
        $email = '[email protected]';
171
172
        $url = 'lists/456/members';
173
174
        $subscriberHash = 'abc123';
175
176
        $this->mailChimpApi->shouldReceive('subscriberHash')
177
            ->once()
178
            ->withArgs([$email])
179
            ->andReturn($subscriberHash);
180
181
        $this->mailChimpApi->shouldReceive('put')
182
            ->once()
183
            ->withArgs([
184
                "{$url}/{$subscriberHash}",
185
                [
186
                    'email_address' => $email,
187
                    'status' => 'subscribed',
188
                    'email_type' => 'html',
189
                ],
190
            ]);
191
192
        $this->newsletter->subscribeOrUpdate($email, [], 'list2');
193
    }
194
195
    /** @test */
196
    public function it_can_override_the_defaults_when_subscribing_someone()
197
    {
198
        $email = '[email protected]';
199
200
        $url = 'lists/123/members';
201
202
        $this->mailChimpApi->shouldReceive('post')
203
            ->once()
204
            ->withArgs([
205
                $url,
206
                [
207
                    'email_address' => $email,
208
                    'status' => 'pending',
209
                    'email_type' => 'text',
210
                ],
211
            ]);
212
213
        $this->newsletter->subscribe($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
214
    }
215
216
    /** @test */
217
    public function it_can_override_the_defaults_when_subscribing_or_updating_someone()
218
    {
219
        $email = '[email protected]';
220
221
        $url = 'lists/123/members';
222
223
        $subscriberHash = 'abc123';
224
225
        $this->mailChimpApi->shouldReceive('subscriberHash')
226
            ->once()
227
            ->withArgs([$email])
228
            ->andReturn($subscriberHash);
229
230
        $this->mailChimpApi->shouldReceive('put')
231
            ->once()
232
            ->withArgs([
233
                "{$url}/{$subscriberHash}",
234
                [
235
                    'email_address' => $email,
236
                    'status' => 'pending',
237
                    'email_type' => 'text',
238
                ],
239
            ]);
240
241
        $this->newsletter->subscribeOrUpdate($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
242
    }
243
244
    /** @test */
245
    public function it_can_change_the_email_address_of_a_subscriber()
246
    {
247
        $email = '[email protected]';
248
        $newEmail = '[email protected]';
249
250
        $url = 'lists/123/members';
251
252
        $subscriberHash = 'abc123';
253
254
        $this->mailChimpApi->shouldReceive('subscriberHash')
255
            ->once()
256
            ->withArgs([$email])
257
            ->andReturn($subscriberHash);
258
259
        $this->mailChimpApi
260
            ->shouldReceive('patch')
261
            ->once()
262
            ->withArgs([
263
                "{$url}/{$subscriberHash}",
264
                [
265
                    'email_address' => $newEmail,
266
                ],
267
            ]);
268
269
        $this->newsletter->updateEmailAddress($email, $newEmail);
270
    }
271
272
    /** @test */
273 View Code Duplication
    public function it_can_unsubscribe_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...
274
    {
275
        $email = '[email protected]';
276
277
        $subscriberHash = 'abc123';
278
279
        $this->mailChimpApi->shouldReceive('subscriberHash')
280
            ->once()
281
            ->withArgs([$email])
282
            ->andReturn($subscriberHash);
283
284
        $this->mailChimpApi
285
            ->shouldReceive('patch')
286
            ->once()
287
            ->withArgs([
288
                "lists/123/members/{$subscriberHash}",
289
                [
290
                    'status' => 'unsubscribed',
291
                ],
292
            ]);
293
294
        $this->newsletter->unsubscribe('[email protected]');
295
    }
296
297
    /** @test */
298 View Code Duplication
    public function it_can_unsubscribe_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...
299
    {
300
        $email = '[email protected]';
301
302
        $subscriberHash = 'abc123';
303
304
        $this->mailChimpApi->shouldReceive('subscriberHash')
305
            ->once()
306
            ->withArgs([$email])
307
            ->andReturn($subscriberHash);
308
309
        $this->mailChimpApi
310
            ->shouldReceive('patch')
311
            ->once()
312
            ->withArgs([
313
                "lists/456/members/{$subscriberHash}",
314
                [
315
                    'status' => 'unsubscribed',
316
                ],
317
            ]);
318
319
        $this->newsletter->unsubscribe('[email protected]', 'list2');
320
    }
321
322
    /** @test */
323
    public function it_can_delete_someone()
324
    {
325
        $email = '[email protected]';
326
327
        $subscriberHash = 'abc123';
328
329
        $this->mailChimpApi->shouldReceive('subscriberHash')
330
            ->once()
331
            ->withArgs([$email])
332
            ->andReturn($subscriberHash);
333
334
        $this->mailChimpApi
335
            ->shouldReceive('delete')
336
            ->once()
337
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
338
339
        $this->newsletter->delete('[email protected]');
340
    }
341
342
    /** @test */
343
    public function it_can_delete_someone_from_a_specific_list()
344
    {
345
        $email = '[email protected]';
346
347
        $subscriberHash = 'abc123';
348
349
        $this->mailChimpApi->shouldReceive('subscriberHash')
350
            ->once()
351
            ->withArgs([$email])
352
            ->andReturn($subscriberHash);
353
354
        $this->mailChimpApi
355
            ->shouldReceive('delete')
356
            ->once()
357
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
358
359
        $this->newsletter->delete('[email protected]', 'list2');
360
    }
361
362
    /** @test */
363
    public function it_exposes_the_api()
364
    {
365
        $api = $this->newsletter->getApi();
366
367
        $this->assertSame($this->mailChimpApi, $api);
368
    }
369
370
    /** @test */
371
    public function it_can_get_the_list_members()
372
    {
373
        $this->mailChimpApi
374
            ->shouldReceive('get')
375
            ->once()
376
            ->withArgs(['lists/123/members', []]);
377
378
        $this->newsletter->getMembers();
379
    }
380
381
    /** @test */
382 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...
383
    {
384
        $email = '[email protected]';
385
386
        $subscriberHash = 'abc123';
387
388
        $this->mailChimpApi->shouldReceive('subscriberHash')
389
            ->once()
390
            ->withArgs([$email])
391
            ->andReturn($subscriberHash);
392
393
        $this->mailChimpApi
394
            ->shouldReceive('get')
395
            ->once()
396
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
397
398
        $this->newsletter->getMember($email);
399
    }
400
401
    /** @test */
402
    public function it_can_get_the_member_activity()
403
    {
404
        $email = '[email protected]';
405
406
        $subscriberHash = 'abc123';
407
408
        $this->mailChimpApi->shouldReceive('subscriberHash')
409
            ->once()
410
            ->withArgs([$email])
411
            ->andReturn($subscriberHash);
412
413
        $this->mailChimpApi
414
            ->shouldReceive('get')
415
            ->once()
416
            ->withArgs(["lists/123/members/{$subscriberHash}/activity"]);
417
418
        $this->newsletter->getMemberActivity($email);
419
    }
420
421
    /** @test */
422 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...
423
    {
424
        $email = '[email protected]';
425
426
        $subscriberHash = 'abc123';
427
428
        $this->mailChimpApi->shouldReceive('subscriberHash')
429
            ->once()
430
            ->withArgs([$email])
431
            ->andReturn($subscriberHash);
432
433
        $this->mailChimpApi
434
            ->shouldReceive('get')
435
            ->once()
436
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
437
438
        $this->newsletter->getMember($email, 'list2');
439
    }
440
441
    /** @test */
442
    public function is_can_create_a_campaign()
443
    {
444
        $fromName = 'Spatie';
445
        $replyTo = '[email protected]';
446
        $subject = 'This is a subject';
447
        $html = '<b>This is the content</b>';
448
        $listName = 'list1';
449
        $options = ['extraOption' => 'extraValue'];
450
        $contentOptions = ['plain text' => 'this is the plain text content'];
451
452
        $campaignId = 'newCampaignId';
453
454
        $this->mailChimpApi
455
            ->shouldReceive('post')
456
            ->once()
457
            ->withArgs(
458
                [
459
                    'campaigns',
460
                    [
461
                        'type' => 'regular',
462
                        'recipients' => [
463
                            'list_id' => 123,
464
                        ],
465
                        'settings' => [
466
                            'subject_line' => $subject,
467
                            'from_name' => $fromName,
468
                            'reply_to' => $replyTo,
469
                        ],
470
                        'extraOption' => 'extraValue',
471
                    ],
472
                ]
473
            )
474
            ->andReturn(['id' => $campaignId]);
475
476
        $this->mailChimpApi
477
            ->shouldReceive('put')
478
            ->once()
479
            ->withArgs([
480
                "campaigns/{$campaignId}/content",
481
                [
482
                    'html' => $html,
483
                    'plain text' => 'this is the plain text content',
484
                ],
485
            ]);
486
487
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
488
    }
489
}
490