Completed
Push — master ( e83cc7...65dda4 )
by Freek
02:35
created

it_can_subscribe_or_update_someone_with_merge_fields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 18
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\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 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...
246
    {
247
        $email = '[email protected]';
248
249
        $subscriberHash = 'abc123';
250
251
        $this->mailChimpApi->shouldReceive('subscriberHash')
252
            ->once()
253
            ->withArgs([$email])
254
            ->andReturn($subscriberHash);
255
256
        $this->mailChimpApi
257
            ->shouldReceive('patch')
258
            ->once()
259
            ->withArgs([
260
                "lists/123/members/{$subscriberHash}",
261
                [
262
                    'status' => 'unsubscribed',
263
                ],
264
            ]);
265
266
        $this->newsletter->unsubscribe('[email protected]');
267
    }
268
269
    /** @test */
270 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...
271
    {
272
        $email = '[email protected]';
273
274
        $subscriberHash = 'abc123';
275
276
        $this->mailChimpApi->shouldReceive('subscriberHash')
277
            ->once()
278
            ->withArgs([$email])
279
            ->andReturn($subscriberHash);
280
281
        $this->mailChimpApi
282
            ->shouldReceive('patch')
283
            ->once()
284
            ->withArgs([
285
                "lists/456/members/{$subscriberHash}",
286
                [
287
                    'status' => 'unsubscribed',
288
                ],
289
            ]);
290
291
        $this->newsletter->unsubscribe('[email protected]', 'list2');
292
    }
293
294
    /** @test */
295
    public function it_can_delete_someone()
296
    {
297
        $email = '[email protected]';
298
299
        $subscriberHash = 'abc123';
300
301
        $this->mailChimpApi->shouldReceive('subscriberHash')
302
            ->once()
303
            ->withArgs([$email])
304
            ->andReturn($subscriberHash);
305
306
        $this->mailChimpApi
307
            ->shouldReceive('delete')
308
            ->once()
309
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
310
311
        $this->newsletter->delete('[email protected]');
312
    }
313
314
    /** @test */
315
    public function it_can_delete_someone_from_a_specific_list()
316
    {
317
        $email = '[email protected]';
318
319
        $subscriberHash = 'abc123';
320
321
        $this->mailChimpApi->shouldReceive('subscriberHash')
322
            ->once()
323
            ->withArgs([$email])
324
            ->andReturn($subscriberHash);
325
326
        $this->mailChimpApi
327
            ->shouldReceive('delete')
328
            ->once()
329
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
330
331
        $this->newsletter->delete('[email protected]', 'list2');
332
    }
333
334
    /** @test */
335
    public function it_exposes_the_api()
336
    {
337
        $api = $this->newsletter->getApi();
338
339
        $this->assertSame($this->mailChimpApi, $api);
340
    }
341
342
    /** @test */
343 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...
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('get')
356
            ->once()
357
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
358
359
        $this->newsletter->getMember($email);
360
    }
361
362
    /** @test */
363 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...
364
    {
365
        $email = '[email protected]';
366
367
        $subscriberHash = 'abc123';
368
369
        $this->mailChimpApi->shouldReceive('subscriberHash')
370
            ->once()
371
            ->withArgs([$email])
372
            ->andReturn($subscriberHash);
373
374
        $this->mailChimpApi
375
            ->shouldReceive('get')
376
            ->once()
377
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
378
379
        $this->newsletter->getMember($email, 'list2');
380
    }
381
382
    /** @test */
383
    public function is_can_create_a_campaign()
384
    {
385
        $fromName = 'Spatie';
386
        $replyTo = '[email protected]';
387
        $subject = 'This is a subject';
388
        $html = '<b>This is the content</b>';
389
        $listName = 'list1';
390
        $options = ['extraOption' => 'extraValue'];
391
        $contentOptions = ['plain text' => 'this is the plain text content'];
392
393
        $campaignId = 'newCampaignId';
394
395
        $this->mailChimpApi
396
            ->shouldReceive('post')
397
            ->once()
398
            ->withArgs(
399
                [
400
                    'campaigns',
401
                    [
402
                        'type' => 'regular',
403
                        'recipients' => [
404
                            'list_id' => 123,
405
                        ],
406
                        'settings' => [
407
                            'subject_line' => $subject,
408
                            'from_name' => $fromName,
409
                            'reply_to' => $replyTo,
410
                        ],
411
                        'extraOption' => 'extraValue',
412
                    ],
413
                ]
414
            )
415
            ->andReturn(['id' => $campaignId]);
416
417
        $this->mailChimpApi
418
            ->shouldReceive('put')
419
            ->once()
420
            ->withArgs([
421
                "campaigns/{$campaignId}/content",
422
                [
423
                    'html' => $html,
424
                    'plain text' => 'this is the plain text content',
425
                ],
426
            ]);
427
428
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
429
    }
430
}
431