161 lines
5.4 KiB
PHP
161 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
use App\Cqs\Queries\Products\GetPublishedProductWithPricingQuery;
|
|
use App\Cqs\Queries\Products\GetPublishedProductWithPricingQueryInput;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use App\Services\ProductDtoAssemblerService;
|
|
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\Slug;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Infrastructure\Adapters\Repositories\EloquentProductRepository;
|
|
use Money\Money;
|
|
|
|
function assemblerTestProduct(string $name, bool $status = true): Product
|
|
{
|
|
/** @var int $sequence */
|
|
static $sequence = 0;
|
|
$sequence++;
|
|
|
|
$range = new Range();
|
|
$range->name = "Gamme {$name}";
|
|
$range->status = true;
|
|
$range->order = $sequence;
|
|
$range->picture_alt = 'alt';
|
|
$range->picture_title = 'title';
|
|
$range->save();
|
|
|
|
$product = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => $name,
|
|
'brand' => ProductBrand::loue,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_20,
|
|
'description' => 'Description',
|
|
'status' => $status,
|
|
]);
|
|
$product->order = $sequence;
|
|
$product->price = Money::EUR(1000);
|
|
$product->picture_alt = 'alt';
|
|
$product->picture_title = 'title';
|
|
$product->picture_large_alt = 'alt';
|
|
$product->picture_large_title = 'title';
|
|
$product->save();
|
|
|
|
return $product;
|
|
}
|
|
|
|
describe('ProductDtoAssemblerService', function (): void {
|
|
it('maps several products with a constant number of queries', function (): void {
|
|
// GIVEN
|
|
$products = collect(range(1, 5))
|
|
->map(fn (int $i): Product => assemblerTestProduct("Produit {$i}"));
|
|
$fresh = Product::query()->whereIn('id', $products->pluck('id'))->get();
|
|
|
|
$assembler = new ProductDtoAssemblerService(new EloquentProductRepository());
|
|
|
|
// WHEN
|
|
DB::enableQueryLog();
|
|
$dtos = $assembler->toDtosWithPricing($fresh);
|
|
$queryCount = count(DB::getQueryLog());
|
|
DB::disableQueryLog();
|
|
|
|
// THEN
|
|
expect($dtos)->toHaveCount(5)
|
|
->and($queryCount)->toBeLessThanOrEqual(3); // range + inco + validDiscount (0 requêtes sur products)
|
|
});
|
|
|
|
it('executes zero queries when relations are already eager-loaded on products', function (): void {
|
|
// GIVEN
|
|
$products = collect(range(1, 5))
|
|
->map(fn (int $i): Product => assemblerTestProduct("Produit Préchargé {$i}"));
|
|
$eagerLoaded = Product::query()
|
|
->with(['range', 'inco', 'validDiscount'])
|
|
->whereIn('id', $products->pluck('id'))
|
|
->get();
|
|
|
|
$assembler = new ProductDtoAssemblerService(new EloquentProductRepository());
|
|
|
|
// WHEN
|
|
DB::enableQueryLog();
|
|
$dtos = $assembler->toDtosWithPricing($eagerLoaded);
|
|
$queryCount = count(DB::getQueryLog());
|
|
DB::disableQueryLog();
|
|
|
|
// THEN
|
|
expect($dtos)->toHaveCount(5)
|
|
->and($queryCount)->toBe(0);
|
|
});
|
|
|
|
it('preserves input order and flags a product without INCO as not orderable', function (): void {
|
|
// GIVEN
|
|
$first = assemblerTestProduct('Premier');
|
|
$second = assemblerTestProduct('Second');
|
|
|
|
$assembler = new ProductDtoAssemblerService(new EloquentProductRepository());
|
|
|
|
// WHEN
|
|
$dtos = $assembler->toDtos([$second, $first]);
|
|
|
|
// THEN
|
|
expect($dtos[0]->name)->toBe('Second')
|
|
->and($dtos[1]->name)->toBe('Premier')
|
|
->and($dtos[0]->is_orderable)->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('GetPublishedProductWithPricingQuery', function (): void {
|
|
it('returns the pricing DTO of a published product', function (): void {
|
|
// GIVEN
|
|
$product = assemblerTestProduct('Produit Publié');
|
|
|
|
// WHEN
|
|
$query = new GetPublishedProductWithPricingQuery(new ProductDtoAssemblerService(new EloquentProductRepository()));
|
|
$dto = $query(
|
|
new GetPublishedProductWithPricingQueryInput(new Slug((string) $product->slug))
|
|
);
|
|
|
|
// THEN
|
|
assert($dto !== null);
|
|
expect($dto->product->name)->toBe('Produit Publié')
|
|
->and($dto->pricing->hasDiscount)->toBeFalse();
|
|
});
|
|
|
|
it('returns null for an unpublished product', function (): void {
|
|
// GIVEN
|
|
$product = assemblerTestProduct('Produit Masqué', status: false);
|
|
|
|
// WHEN
|
|
$query = new GetPublishedProductWithPricingQuery(new ProductDtoAssemblerService(new EloquentProductRepository()));
|
|
$dto = $query(
|
|
new GetPublishedProductWithPricingQueryInput(new Slug((string) $product->slug))
|
|
);
|
|
|
|
// THEN
|
|
expect($dto)->toBeNull();
|
|
});
|
|
|
|
it('returns null for an unknown slug', function (): void {
|
|
// GIVEN
|
|
$slug = new Slug('slug-inexistant');
|
|
|
|
// WHEN
|
|
$query = new GetPublishedProductWithPricingQuery(new ProductDtoAssemblerService(new EloquentProductRepository()));
|
|
$dto = $query(
|
|
new GetPublishedProductWithPricingQueryInput($slug)
|
|
);
|
|
|
|
// THEN
|
|
expect($dto)->toBeNull();
|
|
});
|
|
});
|