84 lines
2.1 KiB
PHP
84 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Casts\NutritionalValuesCast;
|
|
use Domain\Models\ValueObjects\NutritionalValues;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
it('gets nutritional values', function () {
|
|
// GIVEN
|
|
$cast = new NutritionalValuesCast();
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
// WHEN
|
|
$values = $cast->get($model, 'nutritional', null, [
|
|
'nv_ref_weight_g' => 100.5,
|
|
'nv_kj' => 200.0,
|
|
'nv_kcal' => null,
|
|
]);
|
|
|
|
// THEN
|
|
expect($values)->toBeInstanceOf(NutritionalValues::class)
|
|
->and($values->refWeightG)->toBe(100.5)
|
|
->and($values->kj)->toBe(200.0)
|
|
->and($values->kcal)->toBeNull();
|
|
});
|
|
|
|
it('throws exception when getting invalid float type', function () {
|
|
// GIVEN
|
|
$cast = new NutritionalValuesCast();
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
// WHEN
|
|
$cast->get($model, 'nutritional', null, [
|
|
'nv_ref_weight_g' => 'abc',
|
|
]);
|
|
|
|
// THEN
|
|
})->throws(
|
|
\Exception::class,
|
|
'Expected `nv_ref_weight_g` to be numeric of null'
|
|
);
|
|
|
|
it('sets nutritional values', function () {
|
|
// GIVEN
|
|
$cast = new NutritionalValuesCast();
|
|
$model = \Mockery::mock(Model::class);
|
|
$values = new NutritionalValues(refWeightG: 100.5, kj: 200.0);
|
|
|
|
// WHEN
|
|
$array = $cast->set($model, 'nutritional', $values, []);
|
|
|
|
// THEN
|
|
expect($array)->toBeArray()
|
|
->and($array['nv_ref_weight_g'])->toBe(100.5)
|
|
->and($array['nv_kj'])->toBe(200.0);
|
|
});
|
|
|
|
it('sets default when null', function () {
|
|
// GIVEN
|
|
$cast = new NutritionalValuesCast();
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
// WHEN
|
|
$array = $cast->set($model, 'nutritional', null, []);
|
|
|
|
// THEN
|
|
expect($array)->toBeArray()
|
|
->and($array['nv_ref_weight_g'])->toBeNull();
|
|
});
|
|
|
|
it('throws exception when setting invalid type', function () {
|
|
// GIVEN
|
|
$cast = new NutritionalValuesCast();
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
// WHEN
|
|
$cast->set($model, 'nutritional', 'invalid', []);
|
|
|
|
// THEN
|
|
})->throws(
|
|
\InvalidArgumentException::class,
|
|
'The given value is not a NutritionalValues instance.'
|
|
);
|