ldc-cocotte/tests/Integration/WithApp/App/Queries/Discounts/GetDiscountFormDataQueryTest.php

125 lines
4.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Queries\Discounts;
use App\Contracts\QueryInput;
use App\Dto\DiscountDto;
use App\Dto\DiscountProductDto;
use App\Models\Discount;
use App\Models\Product;
use App\Models\Range;
use App\Queries\Discounts\GetDiscountFormDataQuery;
use App\Queries\Discounts\GetDiscountFormDataQueryInput;
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('GetDiscountFormDataQuery', 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('returns form data for create mode when discount is null', function () use ($createProduct): void {
/** @var \Tests\TestCase $this */
// GIVEN
$product1 = $createProduct('Poulet', $this->range);
$product2 = $createProduct('Canard', $this->range);
$query = new GetDiscountFormDataQuery();
$input = new GetDiscountFormDataQueryInput();
// WHEN
$output = $query($input);
// THEN
expect($output->discount)->toBeNull()
->and($output->discountTypes)->toHaveCount(count(DiscountType::cases()))
->and($output->selectedProds)->toBeEmpty()
->and($output->existingProds->pluck('id')->all())->toContain((string) $product1->id, (string) $product2->id)
->and($output->existingProds->first())->toBeInstanceOf(DiscountProductDto::class);
});
it('returns form data for edit mode when discount is provided', function () use ($createProduct): void {
/** @var \Tests\TestCase $this */
// GIVEN
$productAssociated = $createProduct('Associe', $this->range);
$productNotAssociated = $createProduct('NonAssocie', $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', $productAssociated->id)->update(['discount_id' => $discount->id]);
$query = new GetDiscountFormDataQuery();
$input = new GetDiscountFormDataQueryInput(discount: $discount);
// WHEN
$output = $query($input);
// THEN
expect($output->discount)->toBeInstanceOf(DiscountDto::class)
->and($output->discount?->id)->toBe($discount->id)
->and($output->discountTypes)->toHaveCount(count(DiscountType::cases()))
->and($output->selectedProds->pluck('id')->all())->toBe([(string) $productAssociated->id])
->and($output->existingProds->pluck('id')->all())->toContain((string) $productNotAssociated->id)
->and($output->existingProds->pluck('id')->all())->not->toContain((string) $productAssociated->id);
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$query = new GetDiscountFormDataQuery();
$invalidInput = new class () implements QueryInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $query($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});