From 6a1ac33db4a0431615a14e45d91ba35648712e75 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Mon, 9 Sep 2024 14:27:48 +0200 Subject: [PATCH] Add Magento availability check --- .github/workflows/coverage.yml | 2 +- composer.json | 10 +++++++--- src/Actions/ProcessStocks.php | 7 +++++++ src/Jobs/Update/UpdateStockAsyncJob.php | 8 ++++++++ src/Jobs/Update/UpdateStockJob.php | 8 ++++++++ tests/Actions/ProcessStocksTest.php | 23 +++++++++++++++++++++++ 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 0d9d3b0..0532e49 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -32,7 +32,7 @@ jobs: - name: Install dependencies run: | composer config allow-plugins.pestphp/pest-plugin true - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" pestphp/pest --no-interaction --no-update + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests run: XDEBUG_MODE=coverage php vendor/bin/pest --coverage --min=100 diff --git a/composer.json b/composer.json index 96cafac..3308913 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": "^8.2", "justbetter/laravel-magento-async": "^1.0", - "justbetter/laravel-magento-client": "^2.4", + "justbetter/laravel-magento-client": "^2.6.1", "justbetter/laravel-magento-products": "^1.4", "laravel/framework": "11.*", "spatie/laravel-activitylog": "^4.7" @@ -24,7 +24,8 @@ "larastan/larastan": "^2.9", "laravel/pint": "^1.6", "phpstan/phpstan-mockery": "^1.1", - "phpunit/phpunit": "^10.0" + "phpunit/phpunit": "^10.0", + "pestphp/pest": "^2.0" }, "autoload": { "psr-4": { @@ -48,7 +49,10 @@ "fix-style": "pint" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "extra": { "laravel": { diff --git a/src/Actions/ProcessStocks.php b/src/Actions/ProcessStocks.php index 4f7432f..469a7da 100644 --- a/src/Actions/ProcessStocks.php +++ b/src/Actions/ProcessStocks.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Bus\PendingDispatch; +use JustBetter\MagentoClient\Client\Magento; use JustBetter\MagentoStock\Contracts\ProcessesStocks; use JustBetter\MagentoStock\Jobs\Retrieval\RetrieveStockJob; use JustBetter\MagentoStock\Jobs\Update\UpdateStockAsyncJob; @@ -13,6 +14,8 @@ class ProcessStocks implements ProcessesStocks { + public function __construct(protected Magento $magento) {} + public function process(): void { $repository = BaseRepository::resolve(); @@ -25,6 +28,10 @@ public function process(): void ->get() ->each(fn (Stock $stock): PendingDispatch => RetrieveStockJob::dispatch($stock->sku)); + if (! $this->magento->available()) { + return; + } + if (config('magento-stock.async')) { $stocks = Stock::query() ->where('sync', '=', true) diff --git a/src/Jobs/Update/UpdateStockAsyncJob.php b/src/Jobs/Update/UpdateStockAsyncJob.php index 42f6ab3..f702c1f 100644 --- a/src/Jobs/Update/UpdateStockAsyncJob.php +++ b/src/Jobs/Update/UpdateStockAsyncJob.php @@ -8,6 +8,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; +use JustBetter\MagentoClient\Jobs\Middleware\AvailableMiddleware; use JustBetter\MagentoStock\Contracts\Update\Async\UpdatesStockAsync; class UpdateStockAsyncJob implements ShouldQueue @@ -26,4 +27,11 @@ public function handle(UpdatesStockAsync $stock): void { $stock->update($this->stocks); } + + public function middleware(): array + { + return [ + new AvailableMiddleware, + ]; + } } diff --git a/src/Jobs/Update/UpdateStockJob.php b/src/Jobs/Update/UpdateStockJob.php index 639d7bb..b393541 100644 --- a/src/Jobs/Update/UpdateStockJob.php +++ b/src/Jobs/Update/UpdateStockJob.php @@ -8,6 +8,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use JustBetter\MagentoClient\Jobs\Middleware\AvailableMiddleware; use JustBetter\MagentoStock\Contracts\Update\Sync\UpdatesStock; use JustBetter\MagentoStock\Models\Stock; use Throwable; @@ -41,6 +42,13 @@ public function tags(): array ]; } + public function middleware(): array + { + return [ + new AvailableMiddleware, + ]; + } + /** @codeCoverageIgnore */ public function failed(Throwable $exception): void { diff --git a/tests/Actions/ProcessStocksTest.php b/tests/Actions/ProcessStocksTest.php index 6010d45..09cefca 100644 --- a/tests/Actions/ProcessStocksTest.php +++ b/tests/Actions/ProcessStocksTest.php @@ -3,12 +3,14 @@ namespace JustBetter\MagentoStock\Tests\Actions; use Illuminate\Support\Facades\Bus; +use JustBetter\MagentoClient\Contracts\ChecksMagento; use JustBetter\MagentoStock\Actions\ProcessStocks; use JustBetter\MagentoStock\Jobs\Retrieval\RetrieveStockJob; use JustBetter\MagentoStock\Jobs\Update\UpdateStockAsyncJob; use JustBetter\MagentoStock\Jobs\Update\UpdateStockJob; use JustBetter\MagentoStock\Models\Stock; use JustBetter\MagentoStock\Tests\TestCase; +use Mockery\MockInterface; use PHPUnit\Framework\Attributes\Test; class ProcessStocksTest extends TestCase @@ -67,4 +69,25 @@ public function it_dispatches_async_update_job(): void Bus::assertDispatched(UpdateStockAsyncJob::class); } + + #[Test] + public function it_does_not_dispatch_update_jobs_if_magento_is_unavailable(): void + { + Bus::fake(); + + $this->mock(ChecksMagento::class, function (MockInterface $mock): void { + $mock->shouldReceive('available')->andReturnFalse(); + }); + + Stock::query()->create([ + 'sku' => '::sku::', + 'update' => true, + ]); + + /** @var ProcessStocks $action */ + $action = app(ProcessStocks::class); + $action->process(); + + Bus::assertNotDispatched(UpdateStockJob::class); + } }