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 #13

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -34,7 +34,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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"justbetter/laravel-magento-client": "^2.4",
"justbetter/laravel-magento-client": "^2.6.1",
"laravel/framework": "^10.0|^11.0"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions src/Actions/CheckMagentoExistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function exists(string $sku): bool
$magentoProduct = MagentoProduct::findBySku($sku);

if ($magentoProduct === null) {
throw_if(! $this->magento->available(), 'Magento unavailable');
$response = $this->getMagentoProduct($sku);

$response->throwIf(! in_array($response->status(), [200, 404]));
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/RetrieveMagentoSkus.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function retrieve(int $page): Enumerable
->paginate($page, config('magento-products.page_size', 50))
->get();

return $this->magento->get('products', $search)->collect('items')->pluck('sku');
return $this->magento->get('products', $search)->throw()->collect('items')->pluck('sku');
}

public static function bind(): void
Expand Down
2 changes: 2 additions & 0 deletions src/Actions/RetrieveProductData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function retrieve(string $sku, bool $force = false, ?string $store = null
$product = MagentoProduct::findBySku($sku, $store);

if ($product === null) {
throw_if(! $this->magento->available(), 'Magento unavailable');

$magentoProductResponse = $this->getMagentoProduct($sku, $store);

$magentoProductResponse->throwIf($magentoProductResponse->serverError());
Expand Down
8 changes: 8 additions & 0 deletions src/Jobs/CheckKnownProductsExistenceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use JustBetter\MagentoClient\Jobs\Middleware\AvailableMiddleware;
use JustBetter\MagentoProducts\Contracts\ChecksKnownProducts;

class CheckKnownProductsExistenceJob implements ShouldQueue
Expand All @@ -27,4 +28,11 @@ public function handle(ChecksKnownProducts $checksKnownProducts): void
{
$checksKnownProducts->handle($this->skus);
}

public function middleware(): array
{
return [
new AvailableMiddleware,
];
}
}
8 changes: 8 additions & 0 deletions src/Jobs/DiscoverMagentoProductsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use JustBetter\MagentoClient\Jobs\Middleware\AvailableMiddleware;
use JustBetter\MagentoProducts\Contracts\DiscoversMagentoProducts;

class DiscoverMagentoProductsJob implements ShouldBeUnique, ShouldQueue
Expand Down Expand Up @@ -50,4 +51,11 @@ public function uniqueId(): int
{
return $this->page;
}

public function middleware(): array
{
return [
new AvailableMiddleware,
];
}
}
43 changes: 36 additions & 7 deletions tests/Actions/CheckMagentoExistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use JustBetter\MagentoClient\Contracts\ChecksMagento;
use JustBetter\MagentoProducts\Actions\CheckMagentoExistence;
use JustBetter\MagentoProducts\Models\MagentoProduct;
use JustBetter\MagentoProducts\Tests\TestCase;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;
use RuntimeException;

class CheckMagentoExistenceTest extends TestCase
{
Expand All @@ -27,19 +31,24 @@ protected function setUp(): void
])->preventStrayRequests();
}

public function test_existing_product(): void
#[Test]
public function existing_product(): void
{
MagentoProduct::query()->create(['sku' => '123', 'exists_in_magento' => true, 'last_checked' => now()->subHour()]);
MagentoProduct::query()->create([
'sku' => '123', 'exists_in_magento' => true, 'last_checked' => now()->subHour(),
]);

$this->assertTrue($this->action->exists('123'));
}

public function test_urlencode(): void
#[Test]
public function urlencode(): void
{
$this->assertTrue($this->action->exists('123+456'));
}

public function test_new_existing_product(): void
#[Test]
public function new_existing_product(): void
{
$this->assertTrue($this->action->exists('123'));
$this->assertTrue(MagentoProduct::query()->where('sku', '123')->first()->exists_in_magento); /** @phpstan-ignore-line */
Expand All @@ -48,7 +57,8 @@ public function test_new_existing_product(): void
});
}

public function test_new_non_existing_product(): void
#[Test]
public function new_non_existing_product(): void
{
$this->assertFalse($this->action->exists('456'));
$this->assertFalse(MagentoProduct::query()->where('sku', '456')->first()->exists_in_magento); /** @phpstan-ignore-line */
Expand All @@ -57,12 +67,31 @@ public function test_new_non_existing_product(): void
});
}

public function test_existing_last_checked(): void
#[Test]
public function existing_last_checked(): void
{
MagentoProduct::query()->create(['sku' => '123', 'exists_in_magento' => true, 'last_checked' => now()->subHours(3)]);
MagentoProduct::query()->create([
'sku' => '123', 'exists_in_magento' => true, 'last_checked' => now()->subHours(3),
]);

$this->assertTrue($this->action->exists('123'));

Http::assertNothingSent();
}

#[Test]
public function it_throws_exception_when_magento_is_not_available(): void
{
$this->mock(ChecksMagento::class, function (MockInterface $mock): void {
$mock->shouldReceive('available')->andReturnFalse();
});

/** @var CheckMagentoExistence $action */
$action = app(CheckMagentoExistence::class);

$this->expectException(RuntimeException::class);
$action->exists('456');

Http::assertNothingSent();
}
}
41 changes: 34 additions & 7 deletions tests/Actions/RetrieveProductDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use JustBetter\MagentoClient\Contracts\ChecksMagento;
use JustBetter\MagentoProducts\Actions\RetrieveProductData;
use JustBetter\MagentoProducts\Models\MagentoProduct;
use JustBetter\MagentoProducts\Tests\TestCase;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;
use RuntimeException;

class RetrieveProductDataTest extends TestCase
{
Expand All @@ -28,7 +32,8 @@ protected function setUp(): void
]);
}

public function test_it_retrieves_existing_product(): void
#[Test]
public function it_retrieves_existing_product(): void
{
MagentoProduct::query()->create(['sku' => '123', 'data' => ['test'], 'last_checked' => now()->subHour()]);

Expand All @@ -37,7 +42,8 @@ public function test_it_retrieves_existing_product(): void
$this->assertEquals(['test'], $data);
}

public function test_it_retrieves_new_product(): void
#[Test]
public function it_retrieves_new_product(): void
{
$data = $this->action->retrieve('123+456');

Expand All @@ -48,7 +54,8 @@ public function test_it_retrieves_new_product(): void
});
}

public function test_it_retrieves_product_for_store(): void
#[Test]
public function it_retrieves_product_for_store(): void
{
$data = $this->action->retrieve('789', false, 'some_store');

Expand All @@ -64,7 +71,8 @@ public function test_it_retrieves_product_for_store(): void
});
}

public function test_it_retrieves_missing_product(): void
#[Test]
public function it_retrieves_missing_product(): void
{
$data = $this->action->retrieve('404');

Expand All @@ -75,7 +83,8 @@ public function test_it_retrieves_missing_product(): void
});
}

public function test_it_rechecks_on_interval(): void
#[Test]
public function it_rechecks_on_interval(): void
{
MagentoProduct::query()->create(['sku' => '123', 'data' => ['test'], 'last_checked' => now()->subHours(3)]);

Expand All @@ -88,7 +97,8 @@ public function test_it_rechecks_on_interval(): void
});
}

public function test_it_forces_recheck(): void
#[Test]
public function it_forces_recheck(): void
{
MagentoProduct::query()->create(['sku' => '123', 'data' => ['test'], 'last_checked' => now()->subHour()]);

Expand All @@ -101,7 +111,8 @@ public function test_it_forces_recheck(): void
});
}

public function test_it_returns_null_when_check_fails(): void
#[Test]
public function it_returns_null_when_check_fails(): void
{
MagentoProduct::query()->create(['sku' => '404', 'data' => ['test'], 'last_checked' => now()->subHour()]);

Expand All @@ -113,4 +124,20 @@ public function test_it_returns_null_when_check_fails(): void
return $request->url() == 'magento/rest/all/V1/products/404';
});
}

#[Test]
public function it_throws_exception_when_magento_is_not_available(): void
{
$this->mock(ChecksMagento::class, function (MockInterface $mock): void {
$mock->shouldReceive('available')->andReturnFalse();
});

/** @var RetrieveProductData $action */
$action = app(RetrieveProductData::class);

$this->expectException(RuntimeException::class);
$action->retrieve('456');

Http::assertNothingSent();
}
}