ldc-cocotte/tests/Feature/App/Http/Controllers/Bo/DiscountsControllerTest.php

283 lines
9.8 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\Bo;
use App\Models\Customer;
2026-06-14 21:32:07 +02:00
use App\Models\Discount;
use App\Models\Product;
use App\Models\Range;
2026-06-14 21:32:07 +02:00
use App\Models\User;
use Domain\Enums\DiscountType;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Enums\Units\OrderUnit;
2026-06-14 21:32:07 +02:00
use Domain\Enums\UserRole;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Support\Facades\Gate;
use Money\Money;
2026-06-14 21:32:07 +02:00
use function Pest\Laravel\from;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
use function Pest\Laravel\postJson;
describe('DiscountsController', function (): void {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
$this->actingAs($this->user, 'backend');
$this->range = new Range();
$this->range->name = 'Gamme ' . uniqid();
$this->range->status = true;
$this->range->order = random_int(1, 9999);
$this->range->picture_alt = 'alt';
$this->range->picture_title = 'title';
$this->range->save();
2026-06-14 21:32:07 +02:00
});
$createProduct = function (string $name, Range $range): Product {
$product = new Product([
'range_id' => $range->id,
'name' => $name . ' ' . 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();
return $product;
};
it('can access index', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(20);
$discount->is_old_price_displayed = true;
$discount->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get('/bo/discounts');
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.discounts.index');
$response->assertSee("discount={$discount->id}");
2026-06-14 21:32:07 +02:00
});
it('can access create', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get('/bo/discounts/create');
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.discounts.create');
});
it('can store a discount with associated products', function () use ($createProduct): void {
/** @var \Tests\TestCase $this */
// GIVEN
$product1 = $createProduct('Produit 1', $this->range);
$product2 = $createProduct('Produit 2', $this->range);
2026-06-14 21:32:07 +02:00
$data = [
'type' => DiscountType::PERCENT->value,
'value' => 15,
'is_old_price_displayed' => true,
'date_start' => null,
'date_end' => null,
'client_timezone' => 'UTC',
'associated_products' => [(string) $product1->id, (string) $product2->id],
2026-06-14 21:32:07 +02:00
];
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->post('/bo/discounts', $data);
// THEN
$discount = Discount::query()->first();
2026-06-14 21:32:07 +02:00
expect($discount)->not->toBeNull()
->and($discount?->type)->toBe(DiscountType::PERCENT)
->and($discount?->value->getPercentage())->toBe(15);
2026-06-14 21:32:07 +02:00
$freshProduct1 = Product::query()->find($product1->id);
$freshProduct2 = Product::query()->find($product2->id);
expect($freshProduct1?->discount_id)->toBe($discount?->id)
->and($freshProduct2?->discount_id)->toBe($discount?->id);
$response->assertRedirect("/bo/discounts/{$discount?->id}/edit");
2026-06-14 21:32:07 +02:00
});
it('can access edit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(10);
$discount->is_old_price_displayed = true;
$discount->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get("/bo/discounts/{$discount->id}/edit");
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.discounts.edit');
});
it('can update a discount and sync products', function () use ($createProduct): void {
/** @var \Tests\TestCase $this */
// GIVEN
$product1 = $createProduct('P1', $this->range);
$product2 = $createProduct('P2', $this->range);
2026-06-14 21:32:07 +02:00
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(10);
$discount->is_old_price_displayed = true;
$discount->save();
Product::query()->where('id', $product1->id)->update(['discount_id' => $discount->id]);
2026-06-14 21:32:07 +02:00
$data = [
'type' => DiscountType::PERCENT->value,
'value' => 20,
'is_old_price_displayed' => false,
'date_start' => null,
'date_end' => null,
'client_timezone' => 'UTC',
'associated_products' => [(string) $product2->id],
2026-06-14 21:32:07 +02:00
];
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->put("/bo/discounts/{$discount->id}", $data);
// THEN
2026-06-14 21:32:07 +02:00
$discount->refresh();
expect($discount->is_old_price_displayed)->toBeFalse()
->and($discount->value->getPercentage())->toBe(20);
$freshP1 = Product::query()->find($product1->id);
$freshP2 = Product::query()->find($product2->id);
expect($freshP1?->discount_id)->toBeNull()
->and($freshP2?->discount_id)->toBe($discount->id);
2026-06-14 21:32:07 +02:00
$response->assertRedirect("/bo/discounts/{$discount->id}/edit");
});
it('can delete a discount', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(10);
$discount->is_old_price_displayed = true;
$discount->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->delete("/bo/discounts/{$discount->id}");
// THEN
expect(Discount::query()->find($discount->id))->toBeNull();
2026-06-14 21:32:07 +02:00
$response->assertRedirect('/bo/discounts');
});
describe('authorization rejections', function (): void {
it('redirects unauthenticated guest to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
auth('backend')->logout();
// WHEN
$response = $this->get('/bo/discounts');
// THEN
$response->assertRedirect('/bo/login');
});
it('redirects frontend customer trying to access bo to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
auth('backend')->logout();
$customer = new Customer();
$this->actingAs($customer, 'frontend');
// WHEN
$response = $this->get('/bo/discounts');
// THEN
$response->assertRedirect('/bo/login');
});
it('forbids create and store when policy denies access', function (): void {
// GIVEN
$deniedPolicy = new class ()
{
public function create(): bool
{
return false;
}
};
Gate::policy(Discount::class, $deniedPolicy::class);
// WHEN
$createResponse = get('/bo/discounts/create');
$storeResponse = post('/bo/discounts', [
'type' => DiscountType::PERCENT->value,
'value' => 10,
'is_old_price_displayed' => true,
'client_timezone' => 'Europe/Paris',
]);
// THEN
$createResponse->assertForbidden();
$storeResponse->assertForbidden();
});
});
it('redirects back with session validation errors on invalid web store payload', function (): void {
// WHEN
$response = from('/bo/discounts/create')->post('/bo/discounts', [
'type' => DiscountType::PERCENT->value,
'value' => 150,
'client_timezone' => 'Europe/Paris',
]);
// THEN
$response->assertRedirect('/bo/discounts/create')
->assertSessionHasErrors('value');
});
it('returns 422 JSON validation errors on invalid JSON store payload', function (): void {
// WHEN
$response = postJson('/bo/discounts', [
'type' => DiscountType::PERCENT->value,
'value' => 150,
'client_timezone' => 'Europe/Paris',
]);
// THEN
$response->assertUnprocessable()
->assertJsonValidationErrors('value');
});
2026-06-14 21:32:07 +02:00
});