get($model, 'product', null, []); // THEN })->throws(\InvalidArgumentException::class, 'The value cannot be null.'); it('throws exception when getting non-string', function () { // GIVEN $cast = new OrderLineProductCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->get($model, 'product', 123, []); // THEN })->throws(\InvalidArgumentException::class, 'The value must be a string.'); it('gets an order line product', function () { // GIVEN $cast = new OrderLineProductCast(); $model = \Mockery::mock(Model::class); $uuid = Uuid::uuid7()->toString(); $json = json_encode([ 'inco_id' => 123, 'original_id' => $uuid, 'name' => 'Product Name', 'weight' => 1000, 'weight_variable' => false, 'brand' => ProductBrand::loue->value, 'order_unit' => OrderUnit::PCE->value, 'price' => '1000', 'discounted_price' => '800', 'price_kg_ttc' => '1000', 'tax_rate' => TaxRate::TVA_5_5->percent(), 'picture' => 'pic.jpg', ]); // WHEN $product = $cast->get($model, 'product', $json, []); // THEN expect($product)->toBeInstanceOf(OrderLineProduct::class) ->and($product->incoId->value)->toBe(123) ->and($product->originalId->value->toString())->toBe($uuid) ->and($product->name)->toBe('Product Name') ->and($product->weight->value)->toBe(1000) ->and($product->weight->isVariable)->toBeFalse() ->and($product->taxRate)->toBe(TaxRate::TVA_5_5) ->and($product->picture)->toBe('pic.jpg'); }); it('gets an order line product with integer tax rate and null picture', function () { // GIVEN $cast = new OrderLineProductCast(); $model = \Mockery::mock(Model::class); $uuid = Uuid::uuid7()->toString(); $json = json_encode([ 'inco_id' => 123, 'original_id' => $uuid, 'name' => 'Product Name', 'weight' => 1000, 'weight_variable' => false, 'brand' => ProductBrand::loue->value, 'order_unit' => OrderUnit::PCE->value, 'price' => '1000', 'discounted_price' => '800', 'price_kg_ttc' => '1000', 'tax_rate' => 20, // integer from json_decode for 20% 'picture' => null, ]); // WHEN $product = $cast->get($model, 'product', $json, []); // THEN expect($product)->toBeInstanceOf(OrderLineProduct::class) ->and($product->taxRate)->toBe(TaxRate::TVA_20) ->and($product->picture)->toBeNull(); }); it('throws exception when setting invalid type', function () { // GIVEN $cast = new OrderLineProductCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->set($model, 'product', 'invalid', []); // THEN })->throws( \InvalidArgumentException::class, 'The given value is not an instance of OrderLineProduct.' ); it('sets an order line product', function () { // GIVEN $cast = new OrderLineProductCast(); $model = \Mockery::mock(Model::class); $currency = resolve(Currency::class); $product = new OrderLineProduct( incoId: new IncoId(123), originalId: new ProductId(Uuid::uuid7()), name: 'Product Name', weight: new ProductWeight(1000, false), brand: ProductBrand::cases()[0], // Pick first enum case orderUnit: OrderUnit::cases()[0], // Pick first enum case price: new Money(1000, $currency), discountedPrice: new Money(800, $currency), priceKgTtc: new Money(1000, $currency), taxRate: TaxRate::TVA_20, picture: 'pic.jpg' ); // WHEN $value = $cast->set($model, 'product', $product, []); // THEN expect($value)->toBeJson(); });