Skip to content

Commit 500dd0c

Browse files
committed
add endpoint http requests tests
1 parent c4c0f37 commit 500dd0c

6 files changed

+61
-18
lines changed

src/Concerns/InteractsWithHttpRequests.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Coderflex\LaravelSendy\Concerns;
44

5+
use Coderflex\LaravelSendy\Exceptions\InvalidApiKeyException;
6+
use Coderflex\LaravelSendy\Exceptions\InvalidApiUrlException;
57
use Exception;
68
use Illuminate\Support\Facades\Http;
79

@@ -46,11 +48,12 @@ protected function sendRequest(string $type, string $request, array $data = [],
4648

4749
throw_if(
4850
blank($apiKey),
49-
new Exception('API Key is not set in the config file.')
51+
InvalidApiKeyException::class,
5052
);
53+
5154
throw_if(
5255
blank($apiUrl),
53-
new Exception('API URL is not set in the config file.')
56+
InvalidApiUrlException::class,
5457
);
5558

5659
$payload = array_merge($data, [
@@ -68,6 +71,10 @@ protected function sendRequest(string $type, string $request, array $data = [],
6871
? $client->async()->{$type}($url, $payload)
6972
: $client->{$type}($url, $payload);
7073

74+
} catch (InvalidApiKeyException $th) {
75+
throw new InvalidApiKeyException('Error: '.$th->getMessage());
76+
} catch (InvalidApiUrlException $th) {
77+
throw new InvalidApiUrlException('Error: '.$th->getMessage());
7178
} catch (Exception $th) {
7279
throw new Exception('Error: '.$th->getMessage());
7380
}

src/Exceptions/CompaingException.php

-8
This file was deleted.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\Exceptions;
4+
5+
class InvalidApiKeyException extends \Exception
6+
{
7+
protected $message = 'The API key is invalid. Please check your configuration and try again.';
8+
9+
protected $code = 401;
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\Exceptions;
4+
5+
class InvalidApiUrlException extends \Exception
6+
{
7+
protected $message = 'The API URL is invalid. Please check your configuration and try again.';
8+
9+
protected $code = 401;
10+
}

src/Exceptions/SubscribersException.php

-8
This file was deleted.

tests/Resources/EndpointsTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Coderflex\LaravelSendy\Facades\LaravelSendy;
4+
use Illuminate\Support\Facades\Http;
5+
6+
it('throw and exception if the api key not defined', function () {
7+
config([
8+
'laravel-sendy.api_key' => null,
9+
'laravel-sendy.api_url' => 'https://sendy.test',
10+
]);
11+
12+
Http::fake([
13+
'https://sendy.test/api/brands/get-brands.php' => Http::response(true, 200),
14+
]);
15+
16+
$response = LaravelSendy::brands()->get();
17+
18+
})->throws(\Coderflex\LaravelSendy\Exceptions\InvalidApiKeyException::class);
19+
20+
it('throw and exception if the api url not defined', function () {
21+
Http::fake([
22+
'https://sendy.test/api/brands/get-brands.php' => Http::response(true, 200),
23+
]);
24+
25+
config([
26+
'laravel-sendy.api_key' => 'test_api_key',
27+
'laravel-sendy.api_url' => null,
28+
]);
29+
30+
$response = LaravelSendy::brands()->get();
31+
32+
})->throws(\Coderflex\LaravelSendy\Exceptions\InvalidApiUrlException::class);

0 commit comments

Comments
 (0)