Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Magento availability check #25

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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",
Expand All @@ -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"
VincentBean marked this conversation as resolved.
Show resolved Hide resolved
},
"authors": [
{
Expand Down Expand Up @@ -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/ProcessPrices.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\MagentoPrices\Contracts\ProcessesPrices;
use JustBetter\MagentoPrices\Jobs\Retrieval\RetrievePriceJob;
use JustBetter\MagentoPrices\Jobs\Update\UpdatePriceJob;
Expand All @@ -13,6 +14,8 @@

class ProcessPrices implements ProcessesPrices
{
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 (Price $price): PendingDispatch => RetrievePriceJob::dispatch($price->sku));

if (! $this->magento->available()) {
return;
}

if (config('magento-prices.async')) {
$prices = Price::query()
->where('sync', '=', true)
Expand Down
8 changes: 8 additions & 0 deletions src/Jobs/Update/UpdatePriceJob.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\MagentoPrices\Contracts\Update\Sync\UpdatesPrice;
use JustBetter\MagentoPrices\Models\Price;

Expand Down Expand Up @@ -39,4 +40,11 @@ public function tags(): array
$this->price->sku,
];
}

public function middleware(): array
{
return [
new AvailableMiddleware,
];
}
}
10 changes: 9 additions & 1 deletion src/Jobs/Update/UpdatePricesAsyncJob.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\MagentoPrices\Contracts\Update\Async\UpdatesPricesAsync;
use JustBetter\MagentoPrices\Models\Price;

Expand All @@ -18,7 +19,7 @@ class UpdatePricesAsyncJob implements ShouldQueue
use Queueable;
use SerializesModels;

/** @param Collection<int, Price> $prices */
/** @param Collection<int, Price> $prices */
public function __construct(public Collection $prices)
{
$this->onQueue(config('magento-prices.queue'));
Expand All @@ -33,4 +34,11 @@ public function tags(): array
{
return $this->prices->pluck('sku')->toArray();
}

public function middleware(): array
{
return [
new AvailableMiddleware,
];
}
}
23 changes: 23 additions & 0 deletions tests/Actions/ProcessPricesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}