ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/Ajax/BasketControllerTest.php

199 lines
6.7 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Controllers\Fo\Ajax;
use App\Models\Customer;
2026-06-14 21:32:07 +02:00
use App\Models\Page;
use App\Models\Product;
use App\Models\Range;
2026-06-14 21:32:07 +02:00
use Database\Seeders\ContentSeeder;
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\BasketProduct;
use Domain\Models\ValueObjects\DiscountRate;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Services\BasketService;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\View;
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
use Infrastructure\Adapters\Repositories\EloquentProductRepository;
use Infrastructure\Adapters\Repositories\SessionBasketRepository;
use Infrastructure\Adapters\Serializers\BasketSerializer;
use Infrastructure\Adapters\Services\AppSessionService;
use Money\Money;
use Shared\Helpers\UuidHelper;
2026-06-14 21:32:07 +02:00
describe('BasketController', function () {
it('can access post /ajax/basket/add', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/ajax/basket/add', []);
// THEN
$response->assertStatus(403);
});
it('can access post /ajax/basket/empty', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/ajax/basket/empty', []);
// THEN
$response->assertStatus(200);
});
it('can access get /ajax/basket/get', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->get('/ajax/basket/get');
// THEN
$response->assertStatus(200);
});
it('can access post /ajax/basket/remove', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/ajax/basket/remove', []);
// THEN
$response->assertStatus(302);
});
it('serializes multi-item basket without N+1 queries and respects product publication status', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = Customer::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'email' => new Email('basket-nplusone@example.com'),
'hashcode' => UuidHelper::v4()->toString(),
]);
$range = new Range();
$range->name = 'Gamme Test Panier';
$range->status = true;
$range->order = 1;
$range->picture_alt = 'alt';
$range->picture_title = 'title';
$range->save();
/** @var \Illuminate\Contracts\Session\Session $session */
$session = Session::driver();
$basketRepository = new SessionBasketRepository($session, 'basket_', new BasketSerializer());
$basketService = new BasketService(
$basketRepository,
new EloquentProductRepository(),
new AppSessionService('domain_session_uuid'),
new LaravelDomainEventDispatcher(),
);
$basket = $basketService->getBasket();
for ($i = 1; $i <= 5; $i++) {
$product = new Product([
'range_id' => $range->id,
'name' => "Produit Panier {$i}",
'brand' => ProductBrand::loue,
'invoicing_unit' => InvoicingUnit::PCE,
'sell_unit' => SellUnit::PCE,
'order_unit' => OrderUnit::PCE,
'tax_rate' => TaxRate::TVA_20,
'description' => 'Description',
'status' => $i !== 5,
]);
$product->order = $i;
$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();
$basket->addOrUpdate(new BasketProduct(
id: new ProductId(UuidHelper::fromString((string) $product->id)),
name: $product->name,
qty: 1,
unitPriceHt: Money::EUR(1000),
unitDiscount: Money::EUR(0),
taxRate: TaxRate::TVA_20,
weightGrams: 1000,
brand: ProductBrand::loue,
incoId: null,
orderUnit: OrderUnit::PCE,
slug: $product->slug,
picture: null,
description: 'Description',
weightVariable: false,
discountRate: DiscountRate::zero(),
priceKgTtc: Money::EUR(1200),
));
}
$basketRepository->persist($basket);
// WHEN
$this->actingAs($customer, 'frontend');
DB::flushQueryLog();
DB::enableQueryLog();
$response = $this->getJson('/ajax/basket/get');
$queries = DB::getQueryLog();
DB::disableQueryLog();
// THEN
$response->assertStatus(200)
->assertJsonCount(5, 'data.items');
/** @var list<array<string, mixed>> $items */
$items = $response->json('data.items');
expect($items[0]['unitPriceHt'])->not->toBeNull()
->and($items[4]['unitPriceHt'])->toBeNull()
->and(count($queries))->toBe(1);
});
2026-06-14 21:32:07 +02:00
});