ldc-cocotte/tests/Integration/WithApp/App/Casts/MoneyCastTest.php

91 lines
2 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Casts;
2026-06-14 21:32:07 +02:00
use App\Casts\MoneyCast;
use Illuminate\Database\Eloquent\Model;
use Money\Currency;
use Money\Money;
it('gets a money from string', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$money = $cast->get($model, 'price', '1000', []);
// THEN
expect($money->getAmount())->toBe('1000')
->and($money->getCurrency()->getCode())->toBe('EUR');
2026-06-14 21:32:07 +02:00
});
it('gets a money from int', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$money = $cast->get($model, 'price', 1000, []);
// THEN
expect($money->getAmount())->toBe('1000');
2026-06-14 21:32:07 +02:00
});
it('throws exception when getting invalid money type', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'price', 'abc', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not numeric.'
);
it('throws exception when setting invalid money type', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
/** @phpstan-ignore argument.type */
2026-06-14 21:32:07 +02:00
$cast->set($model, 'price', '1000', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not an instance of Money.'
);
it('sets a money', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
$money = new Money(1000, new Currency('EUR'));
2026-06-14 21:32:07 +02:00
// WHEN
$value = $cast->set($model, 'price', $money, []);
// THEN
expect($value)->toBe('1000');
});
it('throws exception when getting float', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'price', 10.5, []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value must be a string or integer.'
);