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 9cf2075..d1e353a 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "php": "^8.2", - "justbetter/laravel-magento-client": "^2.4", + "justbetter/laravel-magento-client": "^2.6.1", "justbetter/laravel-magento-products": "^1.4", "justbetter/laravel-magento-async": "^1.0", "laravel/framework": "^11.0", @@ -17,7 +17,8 @@ "laravel/pint": "^1.17", "orchestra/testbench": "^9.0", "phpstan/phpstan-mockery": "^1.1", - "phpunit/phpunit": "^10.5" + "phpunit/phpunit": "^10.5", + "pestphp/pest": "^2.0" }, "authors": [ { @@ -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/ProcessPrices.php b/src/Actions/ProcessPrices.php index 642ac68..8e412d0 100644 --- a/src/Actions/ProcessPrices.php +++ b/src/Actions/ProcessPrices.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Bus\PendingDispatch; +use JustBetter\MagentoClient\Client\Magento; use JustBetter\MagentoPrices\Contracts\ProcessesPrices; use JustBetter\MagentoPrices\Jobs\Retrieval\RetrievePriceJob; use JustBetter\MagentoPrices\Jobs\Update\UpdatePriceJob; @@ -13,6 +14,8 @@ class ProcessPrices implements ProcessesPrices { + public function __construct(protected Magento $magento) {} + public function process(): void { $repository = BaseRepository::resolve(); @@ -25,6 +28,10 @@ public function process(): void ->get() ->each(fn (Price $price): PendingDispatch => RetrievePriceJob::dispatch($price->sku)); + if (! $this->magento->available()) { + return; + } + if (config('magento-prices.async')) { $prices = Price::query() ->where('sync', '=', true) diff --git a/src/Jobs/Update/UpdatePriceJob.php b/src/Jobs/Update/UpdatePriceJob.php index fbbe934..413cc61 100644 --- a/src/Jobs/Update/UpdatePriceJob.php +++ b/src/Jobs/Update/UpdatePriceJob.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\MagentoPrices\Contracts\Update\Sync\UpdatesPrice; use JustBetter\MagentoPrices\Models\Price; @@ -39,4 +40,11 @@ public function tags(): array $this->price->sku, ]; } + + public function middleware(): array + { + return [ + new AvailableMiddleware, + ]; + } } diff --git a/src/Jobs/Update/UpdatePricesAsyncJob.php b/src/Jobs/Update/UpdatePricesAsyncJob.php index c9ceda2..347cf7e 100644 --- a/src/Jobs/Update/UpdatePricesAsyncJob.php +++ b/src/Jobs/Update/UpdatePricesAsyncJob.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\MagentoPrices\Contracts\Update\Async\UpdatesPricesAsync; use JustBetter\MagentoPrices\Models\Price; @@ -18,7 +19,7 @@ class UpdatePricesAsyncJob implements ShouldQueue use Queueable; use SerializesModels; - /** @param Collection $prices */ + /** @param Collection $prices */ public function __construct(public Collection $prices) { $this->onQueue(config('magento-prices.queue')); @@ -33,4 +34,11 @@ public function tags(): array { return $this->prices->pluck('sku')->toArray(); } + + public function middleware(): array + { + return [ + new AvailableMiddleware, + ]; + } } diff --git a/tests/Actions/ProcessPricesTest.php b/tests/Actions/ProcessPricesTest.php index 17104bd..d5e3cb0 100644 --- a/tests/Actions/ProcessPricesTest.php +++ b/tests/Actions/ProcessPricesTest.php @@ -3,12 +3,14 @@ namespace JustBetter\MagentoPrices\Tests\Actions; use Illuminate\Support\Facades\Bus; +use JustBetter\MagentoClient\Contracts\ChecksMagento; use JustBetter\MagentoPrices\Actions\ProcessPrices; use JustBetter\MagentoPrices\Jobs\Retrieval\RetrievePriceJob; use JustBetter\MagentoPrices\Jobs\Update\UpdatePriceJob; use JustBetter\MagentoPrices\Jobs\Update\UpdatePricesAsyncJob; use JustBetter\MagentoPrices\Models\Price; use JustBetter\MagentoPrices\Tests\TestCase; +use Mockery\MockInterface; use PHPUnit\Framework\Attributes\Test; class ProcessPricesTest extends TestCase @@ -66,4 +68,25 @@ public function it_dispatches_async_update_job(): void Bus::assertDispatched(UpdatePricesAsyncJob::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(); + }); + + Price::query()->create([ + 'sku' => '::sku::', + 'update' => true, + ]); + + /** @var ProcessPrices $action */ + $action = app(ProcessPrices::class); + $action->process(); + + Bus::assertNotDispatched(UpdatePriceJob::class); + } }