129 lines
4.7 KiB
PHP
129 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\App\Jobs;
|
|
|
|
use App\Contracts\ActionInputEmpty;
|
|
use App\Contracts\Executor;
|
|
use App\Cqs\Actions\ProductIncos\SynchronizeProductIncosWithArcadiaApi;
|
|
use App\Dto\ArcadiaIncoDataDto;
|
|
use App\Jobs\FetchAndUpsertArcadiaProductIncos;
|
|
use App\Models\Product;
|
|
use App\Models\ProductInco;
|
|
use App\Models\Range;
|
|
use App\Services\ArcadiaIncoService;
|
|
use Domain\Enums\Nutriscore;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\SellUnit;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Models\ValueObjects\NutritionalValues;
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Money\Money;
|
|
use Psr\Log\NullLogger;
|
|
|
|
describe('FetchAndUpsertArcadiaProductIncos', function (): void {
|
|
it('implements ShouldQueue and ShouldBeUnique with expected lock configuration', function (): void {
|
|
// GIVEN
|
|
$job = new FetchAndUpsertArcadiaProductIncos();
|
|
|
|
// THEN
|
|
expect(class_implements($job))->toHaveKeys([ShouldQueue::class, ShouldBeUnique::class])
|
|
->and($job->uniqueFor)->toBe(3600)
|
|
->and($job->uniqueId())->toBe('fetch-and-upsert-arcadia-product-incos');
|
|
});
|
|
|
|
it('executes SynchronizeProductIncosWithArcadiaApi via Executor in handle', function (): void {
|
|
// GIVEN
|
|
/** @var Executor&\Mockery\MockInterface $executor */
|
|
$executor = \Mockery::mock(Executor::class);
|
|
/** @var ArcadiaIncoService&\Mockery\MockInterface $arcadiaService */
|
|
$arcadiaService = \Mockery::mock(ArcadiaIncoService::class);
|
|
$action = new SynchronizeProductIncosWithArcadiaApi($arcadiaService, new NullLogger());
|
|
$job = new FetchAndUpsertArcadiaProductIncos();
|
|
|
|
$executor->shouldReceive('execute')
|
|
->once()
|
|
->with($action, \Mockery::type(ActionInputEmpty::class))
|
|
->andReturn(0);
|
|
|
|
// WHEN
|
|
$job->handle($executor, $action);
|
|
|
|
// THEN
|
|
expect(\Mockery::getContainer()->mockery_getExpectationCount())->toBeGreaterThan(0);
|
|
});
|
|
|
|
it('synchronizes product incos via dispatchSync using mocked ArcadiaIncoService', function (): void {
|
|
// GIVEN
|
|
$range = new Range();
|
|
$range->name = 'Gamme ' . uniqid();
|
|
$range->status = true;
|
|
$range->order = random_int(1, 9999);
|
|
$range->picture_alt = 'alt';
|
|
$range->picture_title = 'title';
|
|
$range->save();
|
|
|
|
$product = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => 'Produit ' . uniqid(),
|
|
'brand' => ProductBrand::loue,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_20,
|
|
'description' => 'Description test',
|
|
'status' => true,
|
|
]);
|
|
$product->order = random_int(1, 999999);
|
|
$product->price = Money::EUR(1000);
|
|
$product->short_description = 'Court';
|
|
$product->picture_alt = 'alt';
|
|
$product->picture_title = 'title';
|
|
$product->picture_large_alt = 'alt';
|
|
$product->picture_large_title = 'title';
|
|
$product->save();
|
|
|
|
$inco = new ProductInco();
|
|
$inco->product_id = (string) $product->id;
|
|
$inco->no_art = 1001;
|
|
$inco->ingredients = 'Anciens ingrédients';
|
|
$inco->save();
|
|
|
|
$data = new ArcadiaIncoDataDto(
|
|
noArt: 1001,
|
|
name: 'P1',
|
|
libelle: 'Produit 1',
|
|
brand: 'LDC',
|
|
ingredients: '<p>Nouveaux ingrédients</p>',
|
|
preparation: null,
|
|
servings: null,
|
|
allergens: 'Gluten',
|
|
sellingArgument: null,
|
|
nutriscore: Nutriscore::A,
|
|
nutriscoreArgs: null,
|
|
nutrition: new NutritionalValues(refWeightG: 100.0, kj: 550.0),
|
|
);
|
|
|
|
/** @var ArcadiaIncoService&\Mockery\MockInterface $arcadiaService */
|
|
$arcadiaService = \Mockery::mock(ArcadiaIncoService::class);
|
|
$arcadiaService->shouldReceive('fetchInco')
|
|
->with(1001)
|
|
->once()
|
|
->andReturn($data);
|
|
|
|
app()->instance(ArcadiaIncoService::class, $arcadiaService);
|
|
|
|
// WHEN
|
|
FetchAndUpsertArcadiaProductIncos::dispatchSync();
|
|
|
|
// THEN
|
|
$fresh = ProductInco::query()->find($inco->id);
|
|
expect($fresh?->ingredients)->toBe('<p>Nouveaux ingrédients</p>')
|
|
->and($fresh?->allergens)->toBe('Gluten')
|
|
->and($fresh?->nutriscore)->toBe(Nutriscore::A);
|
|
});
|
|
});
|