ldc-cocotte/tests/Integration/WithApp/App/Actions/Common/ExecuteMassActionTest.php
Jérémy Munsch d0efb0f527
refactor: update namespaces for actions and queries
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.
2026-11-10 15:13:26 +01:00

168 lines
5 KiB
PHP

<?php
declare(strict_types=1);
use App\Cqs\Actions\Common\ExecuteMassAction;
use App\Cqs\Actions\Common\ExecuteMassActionInput;
use App\Enums\MassActionType;
use App\Models\Product;
use App\Models\Range;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Enums\Units\OrderUnit;
use Money\Money;
function createMassActionProduct(): Product
{
$range = new Range();
$range->name = 'Test Range ' . uniqid();
$range->status = true;
$range->order = random_int(1, 9999);
$range->picture_alt = 'alt';
$range->picture_title = 'title';
$range->saveOrFail();
$product = new Product([
'range_id' => $range->id,
'name' => 'Test Product ' . uniqid(),
'brand' => ProductBrand::loue,
'invoicing_unit' => InvoicingUnit::PCE,
'sell_unit' => SellUnit::PCE,
'order_unit' => OrderUnit::PCE,
'tax_rate' => TaxRate::TVA_20,
'description' => 'Test description',
'status' => true,
]);
$product->price = Money::EUR(1000);
$product->short_description = 'Short test description';
$product->picture_alt = 'test alt';
$product->picture_title = 'test title';
$product->picture_large_alt = 'test alt';
$product->picture_large_title = 'test title';
$product->order = (int) Product::query()->max('order') + 1;
$product->star = false;
$product->saveOrFail();
return $product;
}
describe('ExecuteMassAction', function (): void {
it('publishes a list of products', function (): void {
// GIVEN
$p1 = createMassActionProduct();
$p1->update(['status' => false]);
$p2 = createMassActionProduct();
$p2->update(['status' => false]);
$action = new ExecuteMassAction();
$input = new ExecuteMassActionInput(
modelClass: Product::class,
action: MassActionType::Publish,
modelKeyList: [$p1->id, $p2->id],
);
// WHEN
$count = $action($input);
// THEN
expect($count)->toBe(2)
->and($p1->fresh()?->status)->toBeTrue()
->and($p2->fresh()?->status)->toBeTrue();
});
it('unpublishes a list of products', function (): void {
// GIVEN
$p1 = createMassActionProduct();
$p2 = createMassActionProduct();
$action = new ExecuteMassAction();
$input = new ExecuteMassActionInput(
modelClass: Product::class,
action: MassActionType::Unpublish,
modelKeyList: [$p1->id, $p2->id],
);
// WHEN
$count = $action($input);
// THEN
expect($count)->toBe(2)
->and($p1->fresh()?->status)->toBeFalse()
->and($p2->fresh()?->status)->toBeFalse();
});
it('stars a list of products', function (): void {
// GIVEN
$p1 = createMassActionProduct();
$p2 = createMassActionProduct();
$action = new ExecuteMassAction();
$input = new ExecuteMassActionInput(
modelClass: Product::class,
action: MassActionType::Star,
modelKeyList: [$p1->id, $p2->id],
);
// WHEN
$count = $action($input);
// THEN
expect($count)->toBe(2)
->and($p1->fresh()?->star)->toBeTrue()
->and($p2->fresh()?->star)->toBeTrue();
});
it('unstars a list of products', function (): void {
// GIVEN
$p1 = createMassActionProduct();
$p1->update(['star' => true]);
$p2 = createMassActionProduct();
$p2->update(['star' => true]);
$action = new ExecuteMassAction();
$input = new ExecuteMassActionInput(
modelClass: Product::class,
action: MassActionType::Unstar,
modelKeyList: [$p1->id, $p2->id],
);
// WHEN
$count = $action($input);
// THEN
expect($count)->toBe(2)
->and($p1->fresh()?->star)->toBeFalse()
->and($p2->fresh()?->star)->toBeFalse();
});
it('returns 0 when no model ids match', function (): void {
// GIVEN
$action = new ExecuteMassAction();
$input = new ExecuteMassActionInput(
modelClass: Product::class,
action: MassActionType::Publish,
modelKeyList: ['non-existing-id-1'],
);
// WHEN
$count = $action($input);
// THEN
expect($count)->toBe(0);
});
it('rejects star action when model does not implement ModelWithStar', function (): void {
// GIVEN — Range does not implement ModelWithStar
$action = new ExecuteMassAction();
$input = new ExecuteMassActionInput(
modelClass: Range::class,
action: MassActionType::Star,
modelKeyList: ['1'],
);
// WHEN & THEN
expect(fn () => $action($input))->toThrow(\InvalidArgumentException::class);
});
});