Refactored the codebase to change the namespaces of various actions and queries from `App\Actions` to `App\Cqs\Actions` and `App\Queries` to `App\Cqs\Queries`. This restructuring improves organization and clarity in the codebase.
124 lines
4.7 KiB
PHP
124 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Queries\Discounts;
|
|
|
|
use App\Contracts\QueryInput;
|
|
use App\Cqs\Queries\Discounts\GetDiscountFormDataQuery;
|
|
use App\Cqs\Queries\Discounts\GetDiscountFormDataQueryInput;
|
|
use App\Dto\DiscountDto;
|
|
use App\Dto\DiscountProductDto;
|
|
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('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);
|
|
});
|
|
});
|