Skip to content

Commit

Permalink
Add Magento availability check
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentBean committed Sep 9, 2024
1 parent dd142da commit 6a1ac33
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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": {
Expand All @@ -48,7 +49,10 @@
"fix-style": "pint"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand Down
7 changes: 7 additions & 0 deletions src/Actions/ProcessStocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,8 @@

class ProcessStocks implements ProcessesStocks
{
public function __construct(protected Magento $magento) {}

public function process(): void
{
$repository = BaseRepository::resolve();
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/Jobs/Update/UpdateStockAsyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +27,11 @@ public function handle(UpdatesStockAsync $stock): void
{
$stock->update($this->stocks);
}

public function middleware(): array
{
return [
new AvailableMiddleware,
];
}
}
8 changes: 8 additions & 0 deletions src/Jobs/Update/UpdateStockJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,13 @@ public function tags(): array
];
}

public function middleware(): array
{
return [
new AvailableMiddleware,
];
}

/** @codeCoverageIgnore */
public function failed(Throwable $exception): void
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Actions/ProcessStocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6a1ac33

Please sign in to comment.