Skip to content

Commit d34e3e7

Browse files
committed
update subscribers unit tests
1 parent 6bc7338 commit d34e3e7

File tree

6 files changed

+147
-24
lines changed

6 files changed

+147
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
4+
5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
6+
use Spatie\LaravelData\Data;
7+
use Spatie\LaravelData\Support\Validation\ValidationContext;
8+
9+
#[MergeValidationRules]
10+
class DeleteSubscriberDTO extends Data
11+
{
12+
public function __construct(
13+
public string $list_id,
14+
public string $email,
15+
) {}
16+
17+
public static function rules(ValidationContext $context): array
18+
{
19+
return [
20+
'email' => ['email'],
21+
];
22+
}
23+
}

src/DTOs/SubscribersDTO.php renamed to src/DTOs/Subscribers/SubscribeDTO.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
namespace Coderflex\LaravelSendy\DTOs;
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
44

55
use Spatie\LaravelData\Attributes\MergeValidationRules;
66
use Spatie\LaravelData\Data;
77
use Spatie\LaravelData\Support\Validation\ValidationContext;
88

99
#[MergeValidationRules]
10-
class SubscribersDTO extends Data
10+
class SubscribeDTO extends Data
1111
{
1212
public function __construct(
1313
public ?string $name,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
4+
5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
6+
use Spatie\LaravelData\Data;
7+
use Spatie\LaravelData\Support\Validation\ValidationContext;
8+
9+
#[MergeValidationRules]
10+
class SubscriberStatusDTO extends Data
11+
{
12+
public function __construct(
13+
public string $list_id,
14+
public string $email,
15+
) {}
16+
17+
public static function rules(ValidationContext $context): array
18+
{
19+
return [
20+
'email' => ['email'],
21+
];
22+
}
23+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
4+
5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
6+
use Spatie\LaravelData\Data;
7+
use Spatie\LaravelData\Support\Validation\ValidationContext;
8+
9+
#[MergeValidationRules]
10+
class UnsubscribeDTO extends Data
11+
{
12+
public function __construct(
13+
public string $list,
14+
public string $email,
15+
public ?bool $boolean = false, // plain text response
16+
) {}
17+
18+
public static function rules(ValidationContext $context): array
19+
{
20+
return [
21+
'email' => ['email'],
22+
];
23+
}
24+
}

src/Resources/Subscribers.php

+13-20
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,47 @@
22

33
namespace Coderflex\LaravelSendy\Resources;
44

5-
use Coderflex\LaravelSendy\DTOs\SubscribersDTO;
5+
use Coderflex\LaravelSendy\DTOs\Subscribers\DeleteSubscriberDTO;
6+
use Coderflex\LaravelSendy\DTOs\Subscribers\SubscribeDTO;
7+
use Coderflex\LaravelSendy\DTOs\Subscribers\SubscriberStatusDTO;
8+
use Coderflex\LaravelSendy\DTOs\Subscribers\UnsubscribeDTO;
69
use Coderflex\LaravelSendy\Facades\LaravelSendy;
710

811
class Subscribers
912
{
1013
public function subscribe(array $data, bool $async = false)
1114
{
12-
$data = SubscribersDTO::validate($data);
15+
$data = SubscribeDTO::validate($data);
1316

1417
return LaravelSendy::post('subscribe', $data, $async);
1518
}
1619

17-
public function unsubscribe(int $listId, string $email, bool $plainTextResponse, bool $async = false)
20+
public function unsubscribe(array $data, bool $async = false)
1821
{
19-
$data = http_build_query([
20-
'list' => $listId,
21-
'email' => $email,
22-
'boolean' => $plainTextResponse,
23-
]);
22+
$data = UnsubscribeDTO::validate($data);
2423

2524
return LaravelSendy::post('api/subscribers/unsubscribe.php', $data, $async);
2625
}
2726

28-
public function delete(int $listId, string $email, bool $async = false)
27+
public function delete(array $data, bool $async = false)
2928
{
30-
$data = http_build_query([
31-
'list_id' => $listId,
32-
'email' => $email,
33-
]);
29+
$data = DeleteSubscriberDTO::validate($data);
3430

3531
return LaravelSendy::post('api/subscribers/delete.php', $data, $async);
3632
}
3733

38-
public function status(int $listId, string $email, bool $async = false)
34+
public function status(array $data, bool $async = false)
3935
{
40-
$data = http_build_query([
41-
'list_id' => $listId,
42-
'email' => $email,
43-
]);
36+
$data = SubscriberStatusDTO::validate($data);
4437

4538
return LaravelSendy::post('api/subscribers/subscription-status.php', $data, $async);
4639
}
4740

4841
public function count(int $listId, bool $async = false)
4942
{
50-
$data = http_build_query([
43+
$data = [
5144
'list_id' => $listId,
52-
]);
45+
];
5346

5447
return LaravelSendy::post('api/subscribers/subscriber-count.php', $data, $async);
5548
}

tests/Resources/SubscribersTest.php

+62-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
it('can subscribe a user', function () {
1414
Http::fake([
15-
'https://sendy.test/subscribe' => Http::response(['success' => true], 200),
15+
'https://sendy.test/subscribe' => Http::response(true, 200),
1616
]);
1717

1818
$response = LaravelSendy::subscribers()->subscribe([
@@ -22,7 +22,7 @@
2222
'country' => 'UAE',
2323
]);
2424

25-
expect($response->json())->toBe(['success' => true]);
25+
expect($response->json())->toBe(1);
2626

2727
Http::assertSent(function ($request) {
2828
return $request->url() === 'https://sendy.test/subscribe' &&
@@ -31,3 +31,63 @@
3131
$request['api_key'] === 'test_api_key';
3232
});
3333
});
34+
35+
it('can unsubscribe a user', function () {
36+
Http::fake([
37+
'https://sendy.test/api/subscribers/unsubscribe.php' => Http::response(true, 200),
38+
]);
39+
40+
$response = LaravelSendy::subscribers()->unsubscribe([
41+
'list' => 123,
42+
'email' => '[email protected]',
43+
'boolean' => true,
44+
]);
45+
46+
expect($response->json())->toBe(1);
47+
48+
Http::assertSent(function ($request) {
49+
return $request->url() === 'https://sendy.test/api/subscribers/unsubscribe.php' &&
50+
$request['email'] === '[email protected]' &&
51+
$request['list'] === 123;
52+
});
53+
});
54+
55+
it('can delete a subscriber', function () {
56+
Http::fake([
57+
'https://sendy.test/api/subscribers/delete.php' => Http::response(true, 200),
58+
]);
59+
60+
$response = LaravelSendy::subscribers()->delete([
61+
'list_id' => 123,
62+
'email' => '[email protected]',
63+
]);
64+
65+
expect($response->json())->toBe(1);
66+
67+
Http::assertSent(fn ($request) => $request['email'] === '[email protected]' &&
68+
$request['list_id'] === 123
69+
);
70+
});
71+
72+
it('can get subscriber status', function () {
73+
Http::fake([
74+
'https://sendy.test/api/subscribers/subscription-status.php' => Http::response(['status' => 'Subscribed'], 200),
75+
]);
76+
77+
$response = LaravelSendy::subscribers()->status([
78+
'list_id' => 123,
79+
'email' => '[email protected]',
80+
]);
81+
82+
expect($response->json())->toBe(['status' => 'Subscribed']);
83+
});
84+
85+
it('can get subscriber count', function () {
86+
Http::fake([
87+
'https://sendy.test/api/subscribers/subscriber-count.php' => Http::response(25, 200),
88+
]);
89+
90+
$response = LaravelSendy::subscribers()->count(123);
91+
92+
expect($response->json())->toBe(25);
93+
});

0 commit comments

Comments
 (0)