Skip to content

Commit f06d7bd

Browse files
authored
Use illuminate http (#8)
* use Illuminate/http * Add github workflows
1 parent 801b429 commit f06d7bd

File tree

5 files changed

+139
-56
lines changed

5 files changed

+139
-56
lines changed

.github/workflows/analyse.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Analyse
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
phpstan:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: 8.0
21+
22+
- name: Cache Composer packages
23+
id: composer-cache
24+
uses: actions/cache@v4
25+
with:
26+
path: vendor
27+
key: ${{ runner.os }}-php-8.0-${{ hashFiles('**/composer.json') }}
28+
restore-keys: |
29+
${{ runner.os }}-php-8.0-
30+
31+
- name: Install dependencies
32+
run: |
33+
composer install --no-interaction --no-progress
34+
35+
- name: Run analyse phpstan
36+
run: vendor/bin/phpstan analyse src tests --error-format github

.github/workflows/tests.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
phpunit:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [8.1, 8.2, 8.3]
17+
laravel: [8.*, 9.*, 10.*, 11.*]
18+
include:
19+
- laravel: 8.*
20+
testbench: 6.*
21+
- laravel: 9.*
22+
testbench: 7.*
23+
- laravel: 10.*
24+
testbench: 8.*
25+
- laravel: 11.*
26+
testbench: 9.*
27+
exclude:
28+
- laravel: 8.*
29+
php: 8.2
30+
- laravel: 8.*
31+
php: 8.3
32+
- laravel: 9.*
33+
php: 8.3
34+
- laravel: 11.*
35+
php: 8.1
36+
37+
steps:
38+
- name: Checkout code
39+
uses: actions/checkout@v4
40+
41+
- name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php }}
45+
extensions: curl, pdo, sqlite, pdo_sqlite
46+
47+
- name: Install SQLite 3
48+
run: |
49+
sudo apt-get update
50+
sudo apt-get install sqlite3
51+
52+
- name: Validate composer.json
53+
run: composer validate
54+
55+
- name: Cache Composer packages
56+
id: composer-cache
57+
uses: actions/cache@v4
58+
with:
59+
path: vendor
60+
key: ${{ runner.os }}-php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-${{ hashFiles('**/composer.lock') }}
61+
restore-keys: |
62+
${{ runner.os }}-php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-
63+
64+
65+
- name: Install dependencies
66+
if: steps.composer-cache.outputs.cache-hit != 'true'
67+
run: |
68+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
69+
composer update --prefer-stable --prefer-dist --no-interaction
70+
71+
- name: Run test phpunit
72+
run: vendor/bin/phpunit --stop-on-error --stop-on-failure

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^7.2",
19-
"guzzlehttp/guzzle": "^6.5"
18+
"php": "^7.3|^8.0",
19+
"illuminate/config": "^8.0|^9.0|^10.0|^11.0",
20+
"illuminate/events": "^8.0|^9.0|^10.0|^11.0",
21+
"illuminate/http": "^8.0|^9.0|^10.0|^11.0"
2022
},
2123
"require-dev": {
22-
"phpunit/phpunit": "^8.0",
23-
"orchestra/testbench": "^3.8.0|^4.0",
24+
"guzzlehttp/guzzle": "^7.0|^8.0|^9.0",
25+
"phpunit/phpunit": "^9.0|^10.0",
26+
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
2427
"plesk/api-php-lib": "^1.0"
2528
},
2629
"suggest": {
@@ -39,7 +42,6 @@
3942
"scripts": {
4043
"test": "vendor/bin/phpunit",
4144
"test-coverage": "vendor/bin/phpunit --coverage-txt"
42-
4345
},
4446
"config": {
4547
"sort-packages": true

src/Services/NotificationManager/NotificationManager.php

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
namespace DescomLib\Services\NotificationManager;
44

5-
use GuzzleHttp\Client;
6-
use GuzzleHttp\Exception\RequestException;
5+
use Illuminate\Support\Facades\Http;
76
use DescomLib\Exceptions\PermanentException;
87
use DescomLib\Exceptions\TemporaryException;
98
use DescomLib\Services\NotificationManager\Events\NotificationFailed;
109
use Illuminate\Support\Facades\Event;
1110

1211
class NotificationManager
1312
{
14-
protected $client = null;
1513
protected $url = null;
1614
protected $token = null;
1715

@@ -20,24 +18,10 @@ class NotificationManager
2018
*/
2119
public function __construct()
2220
{
23-
$this->client = new Client();
2421
$this->url = config('descom_lib.notification_manager.url');
2522
$this->token = config('descom_lib.notification_manager.token');
2623
}
2724

28-
/**
29-
* To mock
30-
*
31-
* @param GuzzleHttp\Client $client
32-
* @return self
33-
*/
34-
public function setClient(Client $client)
35-
{
36-
$this->client = $client;
37-
38-
return $this;
39-
}
40-
4125
/**
4226
* Send request to Notification Manager service
4327
*
@@ -47,35 +31,27 @@ public function setClient(Client $client)
4731
public function send(array $data): ?object
4832
{
4933
try {
50-
$response = $this->client->post(
51-
$this->url,
52-
[
53-
'headers' => [
54-
'Accept' => 'application/json',
55-
'Authorization' => $this->token
56-
],
57-
'http_errors' => false,
58-
'connect_timeout' => 30,
59-
'json' => $data
60-
]
61-
);
34+
$response = Http::withHeaders([
35+
'Accept' => 'application/json',
36+
'Authorization' => $this->token,
37+
])->timeout(30)->post($this->url, $data);
6238

63-
if ($response->getStatusCode() < 300) {
64-
return json_decode($response->getBody()->getContents());
39+
if ($response->successful()) {
40+
return $response->object(); // Retorna como un objeto
6541
}
6642

67-
if ($response->getStatusCode() == 503) {
43+
if ($response->status() === 503) {
6844
Event::dispatch(new NotificationFailed(
6945
$data,
70-
new TemporaryException("Temporal error", $response->getStatusCode())
46+
new TemporaryException("Temporary error", $response->status())
7147
));
7248
} else {
7349
Event::dispatch(new NotificationFailed(
7450
$data,
75-
new PermanentException("Permanent error", $response->getStatusCode())
51+
new PermanentException("Permanent error", $response->status())
7652
));
7753
}
78-
} catch (RequestException $e) {
54+
} catch (\Exception $e) {
7955
Event::dispatch(new NotificationFailed(
8056
$data,
8157
new TemporaryException($e->getMessage(), $e->getCode())

tests/Services/NotificationManager/NotificationManagerTest.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
namespace Tests;
44

55
use Tests\TestCase;
6-
use GuzzleHttp\Client;
7-
use GuzzleHttp\HandlerStack;
8-
use GuzzleHttp\Psr7\Response;
9-
use GuzzleHttp\Handler\MockHandler;
106
use DescomLib\Exceptions\PermanentException;
117
use DescomLib\Exceptions\TemporaryException;
128
use DescomLib\Services\NotificationManager\Events\NotificationFailed;
139
use DescomLib\Services\NotificationManager\NotificationManager;
1410
use Illuminate\Support\Facades\Event;
11+
use Illuminate\Support\Facades\Http;
1512

1613
class NotificationManagerTest extends TestCase
1714
{
@@ -22,12 +19,12 @@ public function testLoggedEmail()
2219
'message' => 'Notificaciones enviadas con éxito'
2320
];
2421

25-
$mock = new MockHandler([new Response(200, [], json_encode($responseExpected))]);
26-
$handlerStack = HandlerStack::create($mock);
27-
$client = new Client(['handler' => $handlerStack]);
22+
// Simula la respuesta HTTP
23+
Http::fake([
24+
'*' => Http::response($responseExpected, 200)
25+
]);
2826

2927
$notificationManager = new NotificationManager;
30-
$notificationManager->setClient($client);
3128

3229
$response = $notificationManager->send($data);
3330

@@ -40,12 +37,12 @@ public function testLoggedEmailPermanentException()
4037

4138
$data = [];
4239

43-
$mock = new MockHandler([new Response(404, [], null)]);
44-
$handlerStack = HandlerStack::create($mock);
45-
$client = new Client(['handler' => $handlerStack]);
40+
// Simula la respuesta HTTP con un código 404
41+
Http::fake([
42+
'*' => Http::response([], 404)
43+
]);
4644

4745
$notificationManager = new NotificationManager;
48-
$notificationManager->setClient($client);
4946

5047
$notificationManager->send($data);
5148

@@ -60,12 +57,12 @@ public function testLoggedEmailTemporaryException()
6057

6158
$data = [];
6259

63-
$mock = new MockHandler([new Response(503, [], null)]);
64-
$handlerStack = HandlerStack::create($mock);
65-
$client = new Client(['handler' => $handlerStack]);
60+
// Simula la respuesta HTTP con un código 503
61+
Http::fake([
62+
'*' => Http::response([], 503)
63+
]);
6664

6765
$notificationManager = new NotificationManager;
68-
$notificationManager->setClient($client);
6966

7067
$notificationManager->send($data);
7168

0 commit comments

Comments
 (0)