Completed
Pull Request — master (#56)
by
unknown
01:40
created

NewsletterTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 392
Duplicated Lines 55.36 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 217
loc 392
rs 10
c 1
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 6 1
A setUp() 0 19 1
A it_can_subscribe_someone() 17 17 1
A it_can_subscribe_or_update_someone() 17 17 1
A it_can_subscribe_someone_with_merge_fields() 22 22 1
A it_can_subscribe_or_update_someone_with_merge_fields() 22 22 1
A it_can_subscribe_someone_to_an_alternative_list() 19 19 1
A it_can_subscribe_or_update_someone_to_an_alternative_list() 0 19 1
A it_can_override_the_defaults_when_subscribing_someone() 19 19 1
A it_can_override_the_defaults_when_subscribing_or_updating_someone() 19 19 1
A it_can_unsubscribe_someone() 23 23 1
A it_can_unsubscribe_someone_from_a_specific_list() 23 23 1
A it_can_delete_someone() 0 18 1
A it_can_delete_someone_from_a_specific_list() 0 18 1
A it_exposes_the_api() 0 6 1
A it_can_get_the_member() 18 18 1
A it_can_get_the_member_from_a_specific_list() 18 18 1
A is_can_create_a_campaign() 0 47 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        $this->mailChimpApi->shouldReceive('put')->withArgs([
73
            $url,
74
            [
75
                'email_address' => $email,
76
                'status' => 'subscribed',
77
                'email_type' => 'html',
78
            ],
79
        ]);
80
81
        $this->newsletter->subscribeOrUpdate($email);
82
    }
83
84
    /** @test */
85 View Code Duplication
    public function it_can_subscribe_someone_with_merge_fields()
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...
86
    {
87
        $email = '[email protected]';
88
89
        $mergeFields = ['FNAME' => 'Freek'];
90
91
        $url = 'lists/123/members';
92
93
        $this->mailChimpApi->shouldReceive('post')
94
            ->once()
95
            ->withArgs([
96
                $url,
97
                [
98
                    'email_address' => $email,
99
                    'status' => 'subscribed',
100
                    'merge_fields' => $mergeFields,
101
                    'email_type' => 'html',
102
                ],
103
            ]);
104
105
        $this->newsletter->subscribe($email, $mergeFields);
106
    }
107
108
    /** @test */
109 View Code Duplication
    public function it_can_subscribe_or_update_someone_with_merge_fields()
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...
110
    {
111
        $email = '[email protected]';
112
113
        $mergeFields = ['FNAME' => 'Freek'];
114
115
        $url = 'lists/123/members';
116
117
        $this->mailChimpApi->shouldReceive('put')
118
            ->once()
119
            ->withArgs([
120
                $url,
121
                [
122
                    'email_address' => $email,
123
                    'status' => 'subscribed',
124
                    'merge_fields' => $mergeFields,
125
                    'email_type' => 'html',
126
                ],
127
            ]);
128
129
        $this->newsletter->subscribeOrUpdate($email, $mergeFields);
130
    }
131
132
    /** @test */
133 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...
134
    {
135
        $email = '[email protected]';
136
137
        $url = 'lists/456/members';
138
139
        $this->mailChimpApi->shouldReceive('post')
140
            ->once()
141
            ->withArgs([
142
                $url,
143
                [
144
                    'email_address' => $email,
145
                    'status' => 'subscribed',
146
                    'email_type' => 'html',
147
                ],
148
            ]);
149
150
        $this->newsletter->subscribe($email, [], 'list2');
151
    }
152
153
    /** @test */
154
    public function it_can_subscribe_or_update_someone_to_an_alternative_list()
155
    {
156
        $email = '[email protected]';
157
158
        $url = 'lists/456/members';
159
160
        $this->mailChimpApi->shouldReceive('put')
161
            ->once()
162
            ->withArgs([
163
                $url,
164
                [
165
                    'email_address' => $email,
166
                    'status' => 'subscribed',
167
                    'email_type' => 'html',
168
                ],
169
            ]);
170
171
        $this->newsletter->subscribeOrUpdate($email, [], 'list2');
172
    }
173
174
    /** @test */
175 View Code Duplication
    public function it_can_override_the_defaults_when_subscribing_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...
176
    {
177
        $email = '[email protected]';
178
179
        $url = 'lists/123/members';
180
181
        $this->mailChimpApi->shouldReceive('post')
182
            ->once()
183
            ->withArgs([
184
                $url,
185
                [
186
                    'email_address' => $email,
187
                    'status' => 'pending',
188
                    'email_type' => 'text',
189
                ],
190
            ]);
191
192
        $this->newsletter->subscribe($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
193
    }
194
195
    /** @test */
196 View Code Duplication
    public function it_can_override_the_defaults_when_subscribing_or_updating_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...
197
    {
198
        $email = '[email protected]';
199
200
        $url = 'lists/123/members';
201
202
        $this->mailChimpApi->shouldReceive('put')
203
            ->once()
204
            ->withArgs([
205
                $url,
206
                [
207
                    'email_address' => $email,
208
                    'status' => 'pending',
209
                    'email_type' => 'text',
210
                ],
211
            ]);
212
213
        $this->newsletter->subscribeOrUpdate($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
214
    }
215
216
    /** @test */
217 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...
218
    {
219
        $email = '[email protected]';
220
221
        $subscriberHash = 'abc123';
222
223
        $this->mailChimpApi->shouldReceive('subscriberHash')
224
            ->once()
225
            ->withArgs([$email])
226
            ->andReturn($subscriberHash);
227
228
        $this->mailChimpApi
229
            ->shouldReceive('patch')
230
            ->once()
231
            ->withArgs([
232
                "lists/123/members/{$subscriberHash}",
233
                [
234
                    'status' => 'unsubscribed',
235
                ],
236
            ]);
237
238
        $this->newsletter->unsubscribe('[email protected]');
239
    }
240
241
    /** @test */
242 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...
243
    {
244
        $email = '[email protected]';
245
246
        $subscriberHash = 'abc123';
247
248
        $this->mailChimpApi->shouldReceive('subscriberHash')
249
            ->once()
250
            ->withArgs([$email])
251
            ->andReturn($subscriberHash);
252
253
        $this->mailChimpApi
254
            ->shouldReceive('patch')
255
            ->once()
256
            ->withArgs([
257
                "lists/456/members/{$subscriberHash}",
258
                [
259
                    'status' => 'unsubscribed',
260
                ],
261
            ]);
262
263
        $this->newsletter->unsubscribe('[email protected]', 'list2');
264
    }
265
266
    /** @test */
267
    public function it_can_delete_someone()
268
    {
269
        $email = '[email protected]';
270
271
        $subscriberHash = 'abc123';
272
273
        $this->mailChimpApi->shouldReceive('subscriberHash')
274
            ->once()
275
            ->withArgs([$email])
276
            ->andReturn($subscriberHash);
277
278
        $this->mailChimpApi
279
            ->shouldReceive('delete')
280
            ->once()
281
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
282
283
        $this->newsletter->delete('[email protected]');
284
    }
285
286
    /** @test */
287
    public function it_can_delete_someone_from_a_specific_list()
288
    {
289
        $email = '[email protected]';
290
291
        $subscriberHash = 'abc123';
292
293
        $this->mailChimpApi->shouldReceive('subscriberHash')
294
            ->once()
295
            ->withArgs([$email])
296
            ->andReturn($subscriberHash);
297
298
        $this->mailChimpApi
299
            ->shouldReceive('delete')
300
            ->once()
301
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
302
303
        $this->newsletter->delete('[email protected]', 'list2');
304
    }
305
306
    /** @test */
307
    public function it_exposes_the_api()
308
    {
309
        $api = $this->newsletter->getApi();
310
311
        $this->assertSame($this->mailChimpApi, $api);
312
    }
313
314
    /** @test */
315 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...
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('get')
328
            ->once()
329
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
330
331
        $this->newsletter->getMember($email);
332
    }
333
334
    /** @test */
335 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...
336
    {
337
        $email = '[email protected]';
338
339
        $subscriberHash = 'abc123';
340
341
        $this->mailChimpApi->shouldReceive('subscriberHash')
342
            ->once()
343
            ->withArgs([$email])
344
            ->andReturn($subscriberHash);
345
346
        $this->mailChimpApi
347
            ->shouldReceive('get')
348
            ->once()
349
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
350
351
        $this->newsletter->getMember($email, 'list2');
352
    }
353
354
    /** @test */
355
    public function is_can_create_a_campaign()
356
    {
357
        $fromName = 'Spatie';
358
        $replyTo = '[email protected]';
359
        $subject = 'This is a subject';
360
        $html = '<b>This is the content</b>';
361
        $listName = 'list1';
362
        $options = ['extraOption' => 'extraValue'];
363
        $contentOptions = ['plain text' => 'this is the plain text content'];
364
365
        $campaignId = 'newCampaignId';
366
367
        $this->mailChimpApi
368
            ->shouldReceive('post')
369
            ->once()
370
            ->withArgs(
371
                [
372
                    'campaigns',
373
                    [
374
                        'type' => 'regular',
375
                        'recipients' => [
376
                            'list_id' => 123,
377
                        ],
378
                        'settings' => [
379
                            'subject_line' => $subject,
380
                            'from_name' => $fromName,
381
                            'reply_to' => $replyTo,
382
                        ],
383
                        'extraOption' => 'extraValue',
384
                    ],
385
                ]
386
            )
387
            ->andReturn(['id' => $campaignId]);
388
389
        $this->mailChimpApi
390
            ->shouldReceive('put')
391
            ->once()
392
            ->withArgs([
393
                "campaigns/{$campaignId}/content",
394
                [
395
                    'html' => $html,
396
                    'plain text' => 'this is the plain text content',
397
                ],
398
            ]);
399
400
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
401
    }
402
}
403