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

102 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Actions\Discounts;
use App\Actions\Discounts\DeleteDiscount;
use App\Actions\Discounts\DeleteDiscountInput;
use App\Contracts\ActionInput;
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\DiscountValue;
use Money\Money;
describe('DeleteDiscount 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('deletes a discount and nullifies product discount_id', function () use ($createProduct): void {
/** @var \Tests\TestCase $this */
// GIVEN
$product = $createProduct('P1', $this->range);
$discount = new Discount();
$discount->type = DiscountType::PERCENT;
$discount->value = DiscountValue::fromPercentage(15);
$discount->is_old_price_displayed = true;
$discount->save();
Product::query()->where('id', $product->id)->update(['discount_id' => $discount->id]);
$action = new DeleteDiscount();
$input = new DeleteDiscountInput(discount: $discount);
// WHEN
$result = $action($input);
// THEN
expect($result)->toBeTrue();
$freshDiscount = Discount::query()->find($discount->id);
expect($freshDiscount)->toBeNull();
$freshProduct = Product::query()->find($product->id);
expect($freshProduct?->discount_id)->toBeNull();
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$action = new DeleteDiscount();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});