2026-09-30 12:43:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Discounts;
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
use App\Contracts\ActionInput;
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Discounts\CreateDiscount;
|
|
|
|
|
use App\Cqs\Actions\Discounts\CreateDiscountInput;
|
2026-09-30 12:43:55 +02:00
|
|
|
use App\Models\Discount;
|
|
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Models\Range;
|
|
|
|
|
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;
|
|
|
|
|
use Domain\Models\ValueObjects\DatePeriod;
|
|
|
|
|
use Domain\Models\ValueObjects\DiscountValue;
|
|
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
|
|
|
|
|
|
|
|
|
describe('CreateDiscount action', function (): void {
|
|
|
|
|
beforeEach(function (): void {
|
2026-10-03 10:07:30 +02:00
|
|
|
/** @var \Tests\TestCase $this */
|
2026-09-30 12:43:55 +02:00
|
|
|
$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();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$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('creates and persists a percent discount in database with associated products', function () use ($createProduct): void {
|
2026-10-03 10:07:30 +02:00
|
|
|
/** @var \Tests\TestCase $this */
|
2026-09-30 12:43:55 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$product1 = $createProduct('Produit 1', $this->range);
|
|
|
|
|
$product2 = $createProduct('Produit 2', $this->range);
|
|
|
|
|
|
|
|
|
|
$action = new CreateDiscount();
|
|
|
|
|
$start = new \DateTimeImmutable('2026-06-01 10:00:00');
|
|
|
|
|
$end = new \DateTimeImmutable('2026-06-30 20:00:00');
|
|
|
|
|
$period = new DatePeriod($start, $end);
|
|
|
|
|
$value = DiscountValue::fromPercentage(20);
|
|
|
|
|
|
|
|
|
|
$input = new CreateDiscountInput(
|
|
|
|
|
type: DiscountType::PERCENT,
|
|
|
|
|
value: $value,
|
|
|
|
|
is_old_price_displayed: true,
|
|
|
|
|
period: $period,
|
|
|
|
|
associatedProductsIds: [(string) $product1->id, (string) $product2->id],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$discount = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($discount->id)->toBeGreaterThan(0)
|
2026-09-30 12:43:55 +02:00
|
|
|
->and($discount->type)->toBe(DiscountType::PERCENT)
|
|
|
|
|
->and($discount->is_old_price_displayed)->toBeTrue()
|
|
|
|
|
->and($discount->value->getPercentage())->toBe(20);
|
|
|
|
|
|
|
|
|
|
$fresh = Discount::query()->find($discount->id);
|
|
|
|
|
expect($fresh)->not->toBeNull()
|
|
|
|
|
->and($fresh?->type)->toBe(DiscountType::PERCENT);
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('creates and persists an amount discount in database', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$action = new CreateDiscount();
|
|
|
|
|
$value = DiscountValue::fromAmount(new Money(1000, new Currency('EUR')));
|
|
|
|
|
|
|
|
|
|
$input = new CreateDiscountInput(
|
|
|
|
|
type: DiscountType::AMOUNT,
|
|
|
|
|
value: $value,
|
|
|
|
|
is_old_price_displayed: false,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$discount = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($discount->id)->toBeGreaterThan(0)
|
2026-09-30 12:43:55 +02:00
|
|
|
->and($discount->type)->toBe(DiscountType::AMOUNT)
|
|
|
|
|
->and($discount->is_old_price_displayed)->toBeFalse();
|
|
|
|
|
|
|
|
|
|
$fresh = Discount::query()->find($discount->id);
|
|
|
|
|
expect($fresh)->not->toBeNull()
|
|
|
|
|
->and($fresh?->value->getAmount()->getAmount())->toBe('1000');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$action = new CreateDiscount();
|
|
|
|
|
$invalidInput = new class () implements ActionInput
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
expect(
|
|
|
|
|
/** @phpstan-ignore argument.type */
|
|
|
|
|
fn () => $action($invalidInput)
|
|
|
|
|
)
|
|
|
|
|
// THEN
|
|
|
|
|
->toThrow(\InvalidArgumentException::class);
|
|
|
|
|
});
|
|
|
|
|
});
|