Passed
Push — master ( b5dd0a...3bec69 )
by
unknown
07:48
created

NewsletterTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 388
Duplicated Lines 70.62 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 9
dl 274
loc 388
rs 10
c 0
b 0
f 0

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 DansMaCulotte\Newsletter\Test\Mailjet;
4
5
use Mailjet\Client;
6
use Mailjet\Resources;
7
use Mailjet\Response;
8
use Mockery;
9
use Mockery\Mock;
10
use PHPUnit\Framework\TestCase;
11
use DansMaCulotte\Newsletter\Drivers\MailjetDriver;
12
use DansMaCulotte\Newsletter\Exceptions\ApiError;
13
use DansMaCulotte\Newsletter\Newsletter;
14
15
class NewsletterTest extends TestCase
16
{
17
    /** @var MailjetDriver */
18
    protected $driver;
19
20
    /** @var Mockery\Mock */
21
    protected $client;
22
23
    /** @var \DansMaCulotte\Newsletter\Newsletter */
24
    protected $newsletter;
25
26
    /** @var Mock */
27
    protected $response;
28
29
30
    public function setUp() : void
31
    {
32
        $this->client = Mockery::mock(Client::class);
33
34
        $this->driver = new MailjetDriver([
35
            'key' => 'apikey',
36
            'secret' => 'apisecret',
37
        ], [
38
            'lists' => [
39
                'list1' => ['id' => 123],
40
                'list2' => ['id' => 456],
41
            ],
42
            'defaultList' => 'list1',
43
            'connection_timeout' => 20,
44
        ]);
45
46
        $this->driver->client = $this->client;
47
48
49
        $this->response = Mockery::mock(Response::class);
50
        $this->response->shouldReceive('getData')->andReturn([]);
51
        $this->response->shouldReceive('getReasonPhrase')->andReturn('Error');
52
        $this->response->shouldReceive('getStatus')->andReturn('400');
53
54
        $this->newsletter = new Newsletter($this->driver);
55
    }
56
57
    public function tearDown() : void
58
    {
59
        parent::tearDown();
60
61
        if ($container = Mockery::getContainer()) {
62
            $this->addToAssertionCount($container->mockery_getExpectationCount());
63
        }
64
65
        Mockery::close();
66
    }
67
68
    /** @test */
69
    public function it_can_subscribe_someone()
70
    {
71
        $this->response->shouldReceive('success')->andReturn(true);
72
73
        $this->client->shouldReceive('post')->withArgs([
74
            Resources::$ContactslistManagecontact,[
75
                'id' => '123',
76
                'body' => [
77
                    'Email' => '[email protected]',
78
                    'Action' => 'addnoforce',
79
                    'Name' => 'Martin',
80
                ]
81
            ]
82
83
        ])->andReturn($this->response);
84
85
        $this->newsletter->subscribe('[email protected]', ['Name' => 'Martin']);
86
    }
87
88
    /** @test */
89
    public function it_can_subscribe_or_update_someone()
90
    {
91
        $this->response->shouldReceive('success')->andReturn(true);
92
93
        $this->client->shouldReceive('post')->withArgs([
94
            Resources::$ContactslistManagecontact,[
95
                'id' => '123',
96
                'body' => [
97
                    'Email' => '[email protected]',
98
                    'Action' => 'addnoforce',
99
                    'Name' => 'Martin',
100
                ]
101
            ]
102
103
        ])->andReturn($this->response);
104
105
        $this->newsletter->subscribeOrUpdate('[email protected]', ['Name' => 'Martin']);
106
    }
107
108
    /** @test */
109
    public function it_will_trow_an_exception_when_subscribe_with_an_invalid_email()
110
    {
111
        $this->response->shouldReceive('success')->andReturn(false);
112
113
        $this->client->shouldReceive('post')->withArgs([
114
            Resources::$ContactslistManagecontact,[
115
                'id' => '123',
116
                'body' => [
117
                    'Email' => 'testtest.fr',
118
                    'Action' => 'addnoforce',
119
                    'Name' => 'Martin',
120
                ]
121
            ]
122
        ])->andReturn($this->response);
123
124
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
125
        $this->newsletter->subscribe('testtest.fr', ['Name' => 'Martin']);
126
    }
127
128
    /** @test */
129
    public function it_can_get_the_list_members()
130
    {
131
        $this->response->shouldReceive('success')->andReturn(true);
132
133
        $this->client->shouldReceive('get')->withArgs([
134
            Resources::$Contact,
135
            [
136
                'ContactsList' => '123'
137
            ]
138
        ])->andReturn($this->response);
139
140
        $this->newsletter->getMembers();
141
    }
142
143
    /** @test */
144
    public function it_will_trow_an_exceptions_when_get_members()
145
    {
146
        $this->response->shouldReceive('success')->andReturn(false);
147
148
        $this->client->shouldReceive('get')->withArgs([
149
            Resources::$Contact,
150
            [
151
                'ContactsList' => '123'
152
            ]
153
        ])->andReturn($this->response);
154
155
156
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
157
        $this->newsletter->getMembers();
158
    }
159
160
    /** @test */
161
    public function it_can_get_the_member()
162
    {
163
        $this->response->shouldReceive('success')->andReturn(true);
164
165
        $this->client->shouldReceive('get')->withArgs([
166
            Resources::$Contact,
167
            [
168
                'id' => '[email protected]'
169
            ]
170
        ])->andReturn($this->response);
171
172
        $this->newsletter->getMember('[email protected]');
173
    }
174
175
    /** @test */
176
    public function it_will_trow_an_exception_when_get_the_member()
177
    {
178
        $this->response->shouldReceive('success')->andReturn(false);
179
180
        $this->client->shouldReceive('get')->withArgs([
181
            Resources::$Contact,
182
            [
183
                'id' => 'testtest.fr'
184
            ]
185
        ])->andReturn($this->response);
186
187
188
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
189
        $this->newsletter->getMember('testtest.fr');
190
    }
191
192
    /** @test */
193
    public function it_can_unsubscribe_someone()
194
    {
195
        $this->response->shouldReceive('success')->andReturn(true);
196
197
        $this->client->shouldReceive('post')->withArgs([
198
            Resources::$ContactslistManagecontact,[
199
                'id' => '123',
200
                'body' => [
201
                    'Email' => '[email protected]',
202
                    'Action' => 'unsub',
203
                ]
204
            ]
205
206
        ])->andReturn($this->response);
207
        $this->newsletter->unsubscribe('[email protected]');
208
    }
209
210
    /** @test */
211
    public function it_will_trow_an_exception_when_unsubscribe_someone()
212
    {
213
        $this->response->shouldReceive('success')->andReturn(false);
214
215
        $this->client->shouldReceive('post')->withArgs([
216
            Resources::$ContactslistManagecontact,[
217
                'id' => '123',
218
                'body' => [
219
                    'Email' => 'testtest.fr',
220
                    'Action' => 'unsub',
221
                ]
222
            ]
223
224
        ])->andReturn($this->response);
225
226
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
227
        $this->newsletter->unsubscribe('testtest.fr');
228
    }
229
230
    /** @test */
231
    public function it_can_delete_someone()
232
    {
233
        $this->response->shouldReceive('success')->andReturn(true);
234
235
        $this->client->shouldReceive('post')->withArgs([
236
            Resources::$ContactslistManagecontact,[
237
                'id' => '123',
238
                'body' => [
239
                    'Email' => '[email protected]',
240
                    'Action' => 'remove',
241
                ]
242
            ]
243
244
        ])->andReturn($this->response);
245
246
        $this->newsletter->delete('[email protected]');
247
    }
248
249
    /** @test */
250
    public function it_will_trow_an_exception_when_delete_someone()
251
    {
252
        $this->response->shouldReceive('success')->andReturn(false);
253
254
        $this->client->shouldReceive('post')->withArgs([
255
            Resources::$ContactslistManagecontact,[
256
                'id' => '123',
257
                'body' => [
258
                    'Email' => '[email protected]',
259
                    'Action' => 'remove',
260
                ]
261
            ]
262
263
        ])->andReturn($this->response);
264
265
266
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
267
        $this->newsletter->delete('[email protected]');
268
    }
269
270
    /** @test */
271
    public function it_exposes_the_api()
272
    {
273
        $api = $this->newsletter->getApi();
274
275
        $this->assertSame($this->client, $api);
276
    }
277
278
    /** @test */
279
    public function it_can_check_if_member_is_subscribed()
280
    {
281
        $this->response->shouldReceive('success')->andReturn(true);
282
283
        $response = Mockery::mock(Response::class);
284
        $response->shouldReceive('success')->andReturn(true);
285
        $response->shouldReceive('getData')->andReturn([
286
            [
287
                'ListID' => 123,
288
                'IsUnsub' => false
289
            ]
290
        ]);
291
292
        $this->client->shouldReceive('get')->withArgs([
293
            Resources::$ContactGetcontactslists,
294
            [
295
                'id' => '[email protected]'
296
            ]
297
        ])->andReturn($response);
298
299
300
        $response = $this->newsletter->isSubscribed('[email protected]');
301
        $this->assertTrue($response);
302
    }
303
304
    /** @test */
305
    public function it_can_check_if_member_is_not_subscribed()
306
    {
307
        $this->response->shouldReceive('success')->andReturn(true);
308
309
        $response = Mockery::mock(Response::class);
310
        $response->shouldReceive('success')->andReturn(true);
311
        $response->shouldReceive('getData')->andReturn([
312
            [
313
                'ListID' => 124,
314
                'IsUnsub' => false
315
            ]
316
        ]);
317
318
        $this->client->shouldReceive('get')->withArgs([
319
            Resources::$ContactGetcontactslists,
320
            [
321
                'id' => '[email protected]'
322
            ]
323
        ])->andReturn($response);
324
325
326
        $response = $this->newsletter->isSubscribed('[email protected]');
327
        $this->assertFalse($response);
328
    }
329
330
    /** @test */
331
    public function it_will_trow_an_exception_when_check_if_member_is_subscribed()
332
    {
333
        $this->response->shouldReceive('success')->andReturn(false);
334
335
        $this->client->shouldReceive('get')->withArgs([
336
            Resources::$ContactGetcontactslists,
337
            [
338
                'id' => 'testtest.fr'
339
            ]
340
        ])->andReturn($this->response);
341
342
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
343
        $this->newsletter->isSubscribed('testtest.fr');
344
    }
345
346
    /** @test */
347
    public function it_can_check_if_list_has_member()
348
    {
349
        $this->response->shouldReceive('success')->andReturn(true);
350
351
        $response = Mockery::mock(Response::class);
352
        $response->shouldReceive('success')->andReturn(true);
353
        $response->shouldReceive('getData')->andReturn([
354
            [
355
                'Email' => '[email protected]',
356
            ]
357
        ]);
358
359
        $this->client->shouldReceive('get')->withArgs([
360
            Resources::$Contact,
361
            [
362
                'ContactsList' => '123'
363
            ]
364
        ])->andReturn($response);
365
366
        $response = $this->newsletter->hasMember('[email protected]');
367
        $this->assertTrue($response);
368
    }
369
370
371
    /** @test */
372
    public function it_can_check_if_list_does_not_have_member()
373
    {
374
        $this->response->shouldReceive('success')->andReturn(true);
375
376
        $this->client->shouldReceive('get')->withArgs([
377
            Resources::$Contact,
378
            [
379
                'ContactsList' => '123'
380
            ]
381
        ])->andReturn($this->response);
382
383
        $response = $this->newsletter->hasMember('[email protected]');
384
        $this->assertFalse($response);
385
    }
386
387
    /** @test */
388
    public function it_will_trow_an_exception_when_check_if_list_has_member()
389
    {
390
        $this->response->shouldReceive('success')->andReturn(false);
391
392
        $this->client->shouldReceive('get')->withArgs([
393
            Resources::$Contact,
394
            [
395
                'ContactsList' => '123'
396
            ]
397
        ])->andReturn($this->response);
398
399
        $this->expectExceptionObject(ApiError::responseError('Error', 'mailjet', 400));
400
        $this->newsletter->hasMember('[email protected]');
401
    }
402
}
403