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); }); });