172 lines
5.2 KiB
PHP
172 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Common;
|
|
|
|
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';
|
|
/** @var int $maxOrder */
|
|
$maxOrder = Product::query()->max('order') ?? 0;
|
|
$product->order = $maxOrder + 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);
|
|
});
|
|
});
|