Completed
Push — master ( 85169d...dbd430 )
by Freek
01:54
created

it_can_delete_someone_from_a_specific_list()   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 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
    public function it_can_subscribe_someone_with_merge_fields()
67
    {
68
        $email = '[email protected]';
69
70
        $mergeFields = ['FNAME' => 'Freek'];
71
72
        $url = 'lists/123/members';
73
74
        $this->mailChimpApi->shouldReceive('post')
75
            ->once()
76
            ->withArgs([
77
                $url,
78
                [
79
                    'email_address' => $email,
80
                    'status' => 'subscribed',
81
                    'merge_fields' => $mergeFields,
82
                    'email_type' => 'html',
83
                ],
84
            ]);
85
86
        $this->newsletter->subscribe($email, $mergeFields);
87
    }
88
89
    /** @test */
90 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...
91
    {
92
        $email = '[email protected]';
93
94
        $url = 'lists/456/members';
95
96
        $this->mailChimpApi->shouldReceive('post')
97
            ->once()
98
            ->withArgs([
99
                $url,
100
                [
101
                    'email_address' => $email,
102
                    'status' => 'subscribed',
103
                    'email_type' => 'html',
104
                ],
105
            ]);
106
107
        $this->newsletter->subscribe($email, [], 'list2');
108
    }
109
110
    /** @test */
111
    public function it_can_override_the_defaults_when_subscribing_someone()
112
    {
113
        $email = '[email protected]';
114
115
        $url = 'lists/123/members';
116
117
        $this->mailChimpApi->shouldReceive('post')
118
            ->once()
119
            ->withArgs([
120
                $url,
121
                [
122
                    'email_address' => $email,
123
                    'status' => 'pending',
124
                    'email_type' => 'text',
125
                ],
126
            ]);
127
128
        $this->newsletter->subscribe($email, [], '', ['email_type' => 'text', 'status' => 'pending']);
129
    }
130
131
    /** @test */
132 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...
133
    {
134
        $email = '[email protected]';
135
136
        $subscriberHash = 'abc123';
137
138
        $this->mailChimpApi->shouldReceive('subscriberHash')
139
            ->once()
140
            ->withArgs([$email])
141
            ->andReturn($subscriberHash);
142
143
        $this->mailChimpApi
144
            ->shouldReceive('patch')
145
            ->once()
146
            ->withArgs([
147
                "lists/123/members/{$subscriberHash}",
148
                [
149
                    'status' => 'unsubscribed',
150
                ],
151
            ]);
152
153
        $this->newsletter->unsubscribe('[email protected]');
154
    }
155
156
    /** @test */
157 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...
158
    {
159
        $email = '[email protected]';
160
161
        $subscriberHash = 'abc123';
162
163
        $this->mailChimpApi->shouldReceive('subscriberHash')
164
            ->once()
165
            ->withArgs([$email])
166
            ->andReturn($subscriberHash);
167
168
        $this->mailChimpApi
169
            ->shouldReceive('patch')
170
            ->once()
171
            ->withArgs([
172
                "lists/456/members/{$subscriberHash}",
173
                [
174
                    'status' => 'unsubscribed',
175
                ],
176
            ]);
177
178
        $this->newsletter->unsubscribe('[email protected]', 'list2');
179
    }
180
181
    /** @test */
182
    public function it_can_delete_someone()
183
    {
184
        $email = '[email protected]';
185
186
        $subscriberHash = 'abc123';
187
188
        $this->mailChimpApi->shouldReceive('subscriberHash')
189
            ->once()
190
            ->withArgs([$email])
191
            ->andReturn($subscriberHash);
192
193
        $this->mailChimpApi
194
            ->shouldReceive('delete')
195
            ->once()
196
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
197
198
        $this->newsletter->delete('[email protected]');
199
    }
200
201
    /** @test */
202
    public function it_can_delete_someone_from_a_specific_list()
203
    {
204
        $email = '[email protected]';
205
206
        $subscriberHash = 'abc123';
207
208
        $this->mailChimpApi->shouldReceive('subscriberHash')
209
            ->once()
210
            ->withArgs([$email])
211
            ->andReturn($subscriberHash);
212
213
        $this->mailChimpApi
214
            ->shouldReceive('delete')
215
            ->once()
216
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
217
218
        $this->newsletter->delete('[email protected]', 'list2');
219
    }
220
221
    /** @test */
222
    public function it_exposes_the_api()
223
    {
224
        $api = $this->newsletter->getApi();
225
226
        $this->assertSame($this->mailChimpApi, $api);
227
    }
228
229
    /** @test */
230 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...
231
    {
232
        $email = '[email protected]';
233
234
        $subscriberHash = 'abc123';
235
236
        $this->mailChimpApi->shouldReceive('subscriberHash')
237
            ->once()
238
            ->withArgs([$email])
239
            ->andReturn($subscriberHash);
240
241
        $this->mailChimpApi
242
            ->shouldReceive('get')
243
            ->once()
244
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
245
246
        $this->newsletter->getMember($email);
247
    }
248
249
    /** @test */
250 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...
251
    {
252
        $email = '[email protected]';
253
254
        $subscriberHash = 'abc123';
255
256
        $this->mailChimpApi->shouldReceive('subscriberHash')
257
            ->once()
258
            ->withArgs([$email])
259
            ->andReturn($subscriberHash);
260
261
        $this->mailChimpApi
262
            ->shouldReceive('get')
263
            ->once()
264
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
265
266
        $this->newsletter->getMember($email, 'list2');
267
    }
268
269
    /** @test */
270
    public function is_can_create_a_campaign()
271
    {
272
        $fromName = 'Spatie';
273
        $replyTo = '[email protected]';
274
        $subject = 'This is a subject';
275
        $html = '<b>This is the content</b>';
276
        $listName = 'list1';
277
        $options = ['extraOption' => 'extraValue'];
278
        $contentOptions = ['plain text' => 'this is the plain text content'];
279
280
        $campaignId = 'newCampaignId';
281
282
        $this->mailChimpApi
283
            ->shouldReceive('post')
284
            ->once()
285
            ->withArgs(
286
                [
287
                    'campaigns',
288
                    [
289
                        'type' => 'regular',
290
                        'recipients' => [
291
                            'list_id' => 123,
292
                        ],
293
                        'settings' => [
294
                            'subject_line' => $subject,
295
                            'from_name' => $fromName,
296
                            'reply_to' => $replyTo,
297
                        ],
298
                        'extraOption' => 'extraValue',
299
                    ],
300
                ]
301
            )
302
            ->andReturn(['id' => $campaignId]);
303
304
        $this->mailChimpApi
305
            ->shouldReceive('put')
306
            ->once()
307
            ->withArgs([
308
                "campaigns/{$campaignId}/content",
309
                [
310
                    'html' => $html,
311
                    'plain text' => 'this is the plain text content',
312
                ],
313
            ]);
314
315
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
316
    }
317
}
318