Completed
Push — master ( b846df...63f83e )
by Freek
03:08
created

NewsletterTest::it_can_unsubscribe_someone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
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('delete')
145
            ->once()
146
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
147
148
        $this->newsletter->unsubscribe('[email protected]');
149
    }
150
151
    /** @test */
152 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...
153
    {
154
        $email = '[email protected]';
155
156
        $subscriberHash = 'abc123';
157
158
        $this->mailChimpApi->shouldReceive('subscriberHash')
159
            ->once()
160
            ->withArgs([$email])
161
            ->andReturn($subscriberHash);
162
163
        $this->mailChimpApi
164
            ->shouldReceive('delete')
165
            ->once()
166
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
167
168
        $this->newsletter->unsubscribe('[email protected]', 'list2');
169
    }
170
171
    /** @test */
172
    public function it_exposes_the_api()
173
    {
174
        $api = $this->newsletter->getApi();
175
176
        $this->assertSame($this->mailChimpApi, $api);
177
    }
178
179
    /** @test */
180 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...
181
    {
182
        $email = '[email protected]';
183
184
        $subscriberHash = 'abc123';
185
186
        $this->mailChimpApi->shouldReceive('subscriberHash')
187
            ->once()
188
            ->withArgs([$email])
189
            ->andReturn($subscriberHash);
190
191
        $this->mailChimpApi
192
            ->shouldReceive('get')
193
            ->once()
194
            ->withArgs(["lists/123/members/{$subscriberHash}"]);
195
196
        $this->newsletter->getMember($email);
197
    }
198
199
    /** @test */
200 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...
201
    {
202
        $email = '[email protected]';
203
204
        $subscriberHash = 'abc123';
205
206
        $this->mailChimpApi->shouldReceive('subscriberHash')
207
            ->once()
208
            ->withArgs([$email])
209
            ->andReturn($subscriberHash);
210
211
        $this->mailChimpApi
212
            ->shouldReceive('get')
213
            ->once()
214
            ->withArgs(["lists/456/members/{$subscriberHash}"]);
215
216
        $this->newsletter->getMember($email, 'list2');
217
    }
218
219
    /** @test */
220
    public function is_can_create_a_campaign()
221
    {
222
        $fromName = 'Spatie';
223
        $replyTo = '[email protected]';
224
        $subject = 'This is a subject';
225
        $html = '<b>This is the content</b>';
226
        $listName = 'list1';
227
        $options = ['extraOption' => 'extraValue'];
228
        $contentOptions = ['plain text' => 'this is the plain text content'];
229
230
        $campaignId = 'newCampaignId';
231
232
        $this->mailChimpApi
233
            ->shouldReceive('post')
234
            ->once()
235
            ->withArgs(
236
                [
237
                    'campaigns',
238
                    [
239
                        'type' => 'regular',
240
                        'recipients' => [
241
                            'list_id' => 123,
242
                        ],
243
                        'settings' => [
244
                            'subject_line' => $subject,
245
                            'from_name' => $fromName,
246
                            'reply_to' => $replyTo,
247
                        ],
248
                        'extraOption' => 'extraValue',
249
                    ],
250
                ]
251
            )
252
            ->andReturn(['id' => $campaignId]);
253
254
        $this->mailChimpApi
255
            ->shouldReceive('put')
256
            ->once()
257
            ->withArgs([
258
                "campaigns/{$campaignId}/content",
259
                [
260
                    'html' => $html,
261
                    'plain text' => 'this is the plain text content',
262
                ],
263
            ]);
264
265
        $this->newsletter->createCampaign($fromName, $replyTo, $subject, $html, $listName, $options, $contentOptions);
266
    }
267
}
268