ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php
Jérémy Munsch 0d5958aae6
fix(products): update discount logic for price display
Corrected the logic for displaying discounted prices in the product views.
2026-10-29 14:56:35 +01:00

213 lines
8.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Discount;
use App\Models\Page;
use App\Models\Product;
use App\Models\Range;
use App\Models\Slide;
use Database\Seeders\ContentSeeder;
use Domain\Enums\DiscountType;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\View;
use Money\Money;
use Ramsey\Uuid\Uuid;
describe('HomeController', function () {
it('can access home page', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
});
it('displays star products, slides and ranges', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
$range = new Range();
$range->name = 'Boucherie';
$range->slug = 'boucherie';
$range->picture = 'ranges/boucherie.jpg';
$range->picture_alt = 'Boucherie';
$range->picture_title = 'Boucherie';
$range->status = true;
$range->order = 1;
$range->save();
$slide = new Slide();
$slide->title = 'Super Promo';
$slide->status = true;
$slide->order = 1;
$slide->connected = false;
$slide->link = 'https://example.com';
$slide->date_start = Carbon::yesterday();
$slide->date_end = Carbon::tomorrow();
$slide->save();
$product = new Product();
$product->id = (string) Uuid::uuid7();
$product->name = 'Poulet Fermier';
$product->brand = ProductBrand::loue;
$product->slug = 'poulet-fermier';
$product->description = 'Délicieux poulet fermier';
$product->picture_alt = 'Poulet Fermier';
$product->picture_title = 'Poulet Fermier';
$product->picture_large_alt = 'Poulet Fermier';
$product->picture_large_title = 'Poulet Fermier';
$product->price = Money::EUR(1500);
$product->invoicing_unit = InvoicingUnit::KGM;
$product->sell_unit = SellUnit::KGM;
$product->tax_rate = TaxRate::TVA_5_5;
$product->stock = 10;
$product->range_id = $range->id;
$product->order = 1;
$product->star = true;
$product->status = true;
$product->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$response->assertSee('id="hero-carousel"', false);
$response->assertSee('Poulet Fermier');
$response->assertSee('Super Promo');
$response->assertSee('Boucherie');
});
it('does not display slider section when no slides are published', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
$expiredSlide = new Slide();
$expiredSlide->title = 'Expired Promo';
$expiredSlide->status = true;
$expiredSlide->order = 1;
$expiredSlide->connected = false;
$expiredSlide->link = 'https://example.com';
$expiredSlide->date_start = Carbon::now()->subDays(5);
$expiredSlide->date_end = Carbon::now()->subDays(2);
$expiredSlide->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$response->assertDontSee('id="hero-carousel"', false);
$response->assertDontSee('Expired Promo');
});
it('displays promo prices in red for discounted star products on homepage', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
$range = new Range();
$range->name = 'Boucherie';
$range->slug = 'boucherie';
$range->picture = 'ranges/boucherie.jpg';
$range->picture_alt = 'Boucherie';
$range->picture_title = 'Boucherie';
$range->status = true;
$range->order = 1;
$range->save();
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(10);
$discount->is_old_price_displayed = false;
$discount->date_start = Carbon::yesterday();
$discount->date_end = Carbon::tomorrow();
$discount->save();
$promoProduct = new Product();
$promoProduct->id = (string) Uuid::uuid7();
$promoProduct->name = 'Foie Gras Promo';
$promoProduct->brand = ProductBrand::loue;
$promoProduct->slug = 'foie-gras-promo';
$promoProduct->description = 'Foie Gras en promo';
$promoProduct->picture_alt = 'Foie Gras Promo';
$promoProduct->picture_title = 'Foie Gras Promo';
$promoProduct->picture_large_alt = 'Foie Gras Promo';
$promoProduct->picture_large_title = 'Foie Gras Promo';
$promoProduct->price = Money::EUR(2000);
$promoProduct->invoicing_unit = InvoicingUnit::PCE;
$promoProduct->sell_unit = SellUnit::PCE;
$promoProduct->tax_rate = TaxRate::TVA_5_5;
$promoProduct->stock = 10;
$promoProduct->range_id = $range->id;
$promoProduct->order = 1;
$promoProduct->star = true;
$promoProduct->status = true;
$promoProduct->discount_id = $discount->id;
$promoProduct->save();
$regularProduct = new Product();
$regularProduct->id = (string) Uuid::uuid7();
$regularProduct->name = 'Poulet Standard';
$regularProduct->brand = ProductBrand::loue;
$regularProduct->slug = 'poulet-standard';
$regularProduct->description = 'Poulet standard sans promo';
$regularProduct->picture_alt = 'Poulet Standard';
$regularProduct->picture_title = 'Poulet Standard';
$regularProduct->picture_large_alt = 'Poulet Standard';
$regularProduct->picture_large_title = 'Poulet Standard';
$regularProduct->price = Money::EUR(1500);
$regularProduct->invoicing_unit = InvoicingUnit::PCE;
$regularProduct->sell_unit = SellUnit::PCE;
$regularProduct->tax_rate = TaxRate::TVA_5_5;
$regularProduct->stock = 10;
$regularProduct->range_id = $range->id;
$regularProduct->order = 2;
$regularProduct->star = true;
$regularProduct->status = true;
$regularProduct->discount_id = null;
$regularProduct->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$content = $response->getContent();
expect($content)->toContain('Foie Gras Promo')
->and($content)->toContain('Poulet Standard');
// Promo product price span has text-danger class
expect($content)->toMatch('/Foie Gras Promo.*?<span class="fw-semibold h4\s+text-danger\s*">/s')
// Regular product price span does NOT have text-danger class
->and($content)->toMatch('/Poulet Standard.*?<span class="fw-semibold h4\s*">/s');
});
});