ldc-cocotte/tests/Integration/WithApp/App/Cqs/Actions/Discounts/UpdateDiscountTest.php

128 lines
4.6 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Actions\Discounts;
use App\Contracts\ActionInput;
use App\Cqs\Actions\Discounts\UpdateDiscount;
use App\Cqs\Actions\Discounts\UpdateDiscountInput;
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\Money;
describe('UpdateDiscount action', function (): void {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
$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('updates and persists a discount in database with synchronised products', function () use ($createProduct): void {
/** @var \Tests\TestCase $this */
// GIVEN
$product1 = $createProduct('P1', $this->range);
$product2 = $createProduct('P2', $this->range);
$product3 = $createProduct('P3', $this->range);
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(10);
$discount->is_old_price_displayed = true;
$discount->save();
Product::query()->whereIn('id', [$product1->id, $product2->id])->update(['discount_id' => $discount->id]);
$action = new UpdateDiscount();
$start = new \DateTimeImmutable('2026-07-01 10:00:00');
$end = new \DateTimeImmutable('2026-07-31 20:00:00');
$period = new DatePeriod($start, $end);
$value = DiscountValue::fromPercentage(30);
// Keep product2, remove product1, add product3
$input = new UpdateDiscountInput(
discount: $discount,
type: DiscountType::PERCENT,
value: $value,
is_old_price_displayed: false,
period: $period,
associatedProductsIds: [(string) $product2->id, (string) $product3->id],
);
// WHEN
$updated = $action($input);
// THEN
expect($updated->id)->toBe($discount->id)
->and($updated->value->getPercentage())->toBe(30)
->and($updated->is_old_price_displayed)->toBeFalse();
$fresh = Discount::query()->find($discount->id);
expect($fresh)->not->toBeNull()
->and($fresh?->value->getPercentage())->toBe(30)
->and($fresh?->is_old_price_displayed)->toBeFalse();
$freshP1 = Product::query()->find($product1->id);
$freshP2 = Product::query()->find($product2->id);
$freshP3 = Product::query()->find($product3->id);
expect($freshP1?->discount_id)->toBeNull()
->and($freshP2?->discount_id)->toBe($discount->id)
->and($freshP3?->discount_id)->toBe($discount->id);
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$action = new UpdateDiscount();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});