get( $model, 'discount', 10, ['type' => DiscountType::PERCENT->value] ); // THEN expect($value)->toBeInstanceOf(DiscountValue::class) ->and($value->value())->toBe(10); }); it('gets a discount value from amount', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $value = $cast->get( $model, 'discount', 1000, ['type' => DiscountType::AMOUNT->value] ); // THEN expect($value)->toBeInstanceOf(DiscountValue::class) ->and($value->value())->toBeInstanceOf(Money::class) ->and($value->value()->getAmount())->toBe('1000'); }); it('gets a discount value from amount string', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $value = $cast->get( $model, 'discount', '1000', ['type' => (string) DiscountType::AMOUNT->value] ); // THEN expect($value)->toBeInstanceOf(DiscountValue::class) ->and($value->value())->toBeInstanceOf(Money::class) ->and($value->value()->getAmount())->toBe('1000'); }); it('throws exception when type is missing', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->get($model, 'discount', 10, []); // THEN })->throws( \UnexpectedValueException::class, 'The discount type attribute is missing to cast the value.' ); it('throws exception when type is invalid', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->get($model, 'discount', 10, ['type' => 'invalid']); // THEN })->throws( \UnexpectedValueException::class, 'Unknown discount type raw value: invalid' ); it('throws exception when value is invalid', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->get( $model, 'discount', 'invalid', ['type' => DiscountType::PERCENT->value] ); // THEN })->throws( \UnexpectedValueException::class, 'The discount value must be an integer or a numeric string.' ); it('throws exception when setting null', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->set($model, 'discount', null, []); // THEN })->throws(\InvalidArgumentException::class, 'The value cannot be null.'); it('throws exception when setting invalid type', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); // WHEN $cast->set($model, 'discount', 10, []); // THEN })->throws( \InvalidArgumentException::class, 'The value must be an instance of DiscountValue.' ); it('sets a discount value percentage', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); $discount = DiscountValue::fromPercentage(10); // WHEN $value = $cast->set($model, 'discount', $discount, []); // THEN expect($value)->toBe(10); }); it('sets a discount value amount', function () { // GIVEN $cast = new DiscountValueCast(); $model = \Mockery::mock(Model::class); $currency = resolve(Currency::class); $discount = DiscountValue::fromAmount(new Money(1000, $currency)); // WHEN $value = $cast->set($model, 'discount', $discount, []); // THEN expect($value)->toBe(1000); });