diff --git a/.github/workflows/analyse.yml b/.github/workflows/analyse.yml new file mode 100644 index 0000000..9de01e6 --- /dev/null +++ b/.github/workflows/analyse.yml @@ -0,0 +1,36 @@ +name: Analyse + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + phpstan: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.0 + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v4 + with: + path: vendor + key: ${{ runner.os }}-php-8.0-${{ hashFiles('**/composer.json') }} + restore-keys: | + ${{ runner.os }}-php-8.0- + + - name: Install dependencies + run: | + composer install --no-interaction --no-progress + + - name: Run analyse phpstan + run: vendor/bin/phpstan analyse src tests --error-format github diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..781bef0 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,72 @@ +name: Tests + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + phpunit: + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + php: [8.1, 8.2, 8.3] + laravel: [8.*, 9.*, 10.*, 11.*] + include: + - laravel: 8.* + testbench: 6.* + - laravel: 9.* + testbench: 7.* + - laravel: 10.* + testbench: 8.* + - laravel: 11.* + testbench: 9.* + exclude: + - laravel: 8.* + php: 8.2 + - laravel: 8.* + php: 8.3 + - laravel: 9.* + php: 8.3 + - laravel: 11.* + php: 8.1 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: curl, pdo, sqlite, pdo_sqlite + + - name: Install SQLite 3 + run: | + sudo apt-get update + sudo apt-get install sqlite3 + + - name: Validate composer.json + run: composer validate + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v4 + with: + path: vendor + key: ${{ runner.os }}-php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php-${{ matrix.php }}-laravel-${{ matrix.laravel }}- + + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --prefer-stable --prefer-dist --no-interaction + + - name: Run test phpunit + run: vendor/bin/phpunit --stop-on-error --stop-on-failure diff --git a/composer.json b/composer.json index 265e63f..a2fcae4 100644 --- a/composer.json +++ b/composer.json @@ -15,12 +15,15 @@ } ], "require": { - "php": "^7.2", - "guzzlehttp/guzzle": "^6.5" + "php": "^7.3|^8.0", + "illuminate/config": "^8.0|^9.0|^10.0|^11.0", + "illuminate/events": "^8.0|^9.0|^10.0|^11.0", + "illuminate/http": "^8.0|^9.0|^10.0|^11.0" }, "require-dev": { - "phpunit/phpunit": "^8.0", - "orchestra/testbench": "^3.8.0|^4.0", + "guzzlehttp/guzzle": "^7.0|^8.0|^9.0", + "phpunit/phpunit": "^9.0|^10.0", + "orchestra/testbench": "^6.0|^7.0|^8.0|^9.0", "plesk/api-php-lib": "^1.0" }, "suggest": { @@ -39,7 +42,6 @@ "scripts": { "test": "vendor/bin/phpunit", "test-coverage": "vendor/bin/phpunit --coverage-txt" - }, "config": { "sort-packages": true diff --git a/src/Services/NotificationManager/NotificationManager.php b/src/Services/NotificationManager/NotificationManager.php index ccb4f0d..dcf9cc7 100644 --- a/src/Services/NotificationManager/NotificationManager.php +++ b/src/Services/NotificationManager/NotificationManager.php @@ -2,8 +2,7 @@ namespace DescomLib\Services\NotificationManager; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\RequestException; +use Illuminate\Support\Facades\Http; use DescomLib\Exceptions\PermanentException; use DescomLib\Exceptions\TemporaryException; use DescomLib\Services\NotificationManager\Events\NotificationFailed; @@ -11,7 +10,6 @@ class NotificationManager { - protected $client = null; protected $url = null; protected $token = null; @@ -20,24 +18,10 @@ class NotificationManager */ public function __construct() { - $this->client = new Client(); $this->url = config('descom_lib.notification_manager.url'); $this->token = config('descom_lib.notification_manager.token'); } - /** - * To mock - * - * @param GuzzleHttp\Client $client - * @return self - */ - public function setClient(Client $client) - { - $this->client = $client; - - return $this; - } - /** * Send request to Notification Manager service * @@ -47,35 +31,27 @@ public function setClient(Client $client) public function send(array $data): ?object { try { - $response = $this->client->post( - $this->url, - [ - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => $this->token - ], - 'http_errors' => false, - 'connect_timeout' => 30, - 'json' => $data - ] - ); + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => $this->token, + ])->timeout(30)->post($this->url, $data); - if ($response->getStatusCode() < 300) { - return json_decode($response->getBody()->getContents()); + if ($response->successful()) { + return $response->object(); // Retorna como un objeto } - if ($response->getStatusCode() == 503) { + if ($response->status() === 503) { Event::dispatch(new NotificationFailed( $data, - new TemporaryException("Temporal error", $response->getStatusCode()) + new TemporaryException("Temporary error", $response->status()) )); } else { Event::dispatch(new NotificationFailed( $data, - new PermanentException("Permanent error", $response->getStatusCode()) + new PermanentException("Permanent error", $response->status()) )); } - } catch (RequestException $e) { + } catch (\Exception $e) { Event::dispatch(new NotificationFailed( $data, new TemporaryException($e->getMessage(), $e->getCode()) diff --git a/tests/Services/NotificationManager/NotificationManagerTest.php b/tests/Services/NotificationManager/NotificationManagerTest.php index 25644ba..38288f6 100644 --- a/tests/Services/NotificationManager/NotificationManagerTest.php +++ b/tests/Services/NotificationManager/NotificationManagerTest.php @@ -3,15 +3,12 @@ namespace Tests; use Tests\TestCase; -use GuzzleHttp\Client; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Response; -use GuzzleHttp\Handler\MockHandler; use DescomLib\Exceptions\PermanentException; use DescomLib\Exceptions\TemporaryException; use DescomLib\Services\NotificationManager\Events\NotificationFailed; use DescomLib\Services\NotificationManager\NotificationManager; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Http; class NotificationManagerTest extends TestCase { @@ -22,12 +19,12 @@ public function testLoggedEmail() 'message' => 'Notificaciones enviadas con éxito' ]; - $mock = new MockHandler([new Response(200, [], json_encode($responseExpected))]); - $handlerStack = HandlerStack::create($mock); - $client = new Client(['handler' => $handlerStack]); + // Simula la respuesta HTTP + Http::fake([ + '*' => Http::response($responseExpected, 200) + ]); $notificationManager = new NotificationManager; - $notificationManager->setClient($client); $response = $notificationManager->send($data); @@ -40,12 +37,12 @@ public function testLoggedEmailPermanentException() $data = []; - $mock = new MockHandler([new Response(404, [], null)]); - $handlerStack = HandlerStack::create($mock); - $client = new Client(['handler' => $handlerStack]); + // Simula la respuesta HTTP con un código 404 + Http::fake([ + '*' => Http::response([], 404) + ]); $notificationManager = new NotificationManager; - $notificationManager->setClient($client); $notificationManager->send($data); @@ -60,12 +57,12 @@ public function testLoggedEmailTemporaryException() $data = []; - $mock = new MockHandler([new Response(503, [], null)]); - $handlerStack = HandlerStack::create($mock); - $client = new Client(['handler' => $handlerStack]); + // Simula la respuesta HTTP con un código 503 + Http::fake([ + '*' => Http::response([], 503) + ]); $notificationManager = new NotificationManager; - $notificationManager->setClient($client); $notificationManager->send($data);