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