Skip to content

Commit 1103dc6

Browse files
committed
Apply suggested changes
1 parent dc574bc commit 1103dc6

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

system/Entity/Cast/FloatCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
use CodeIgniter\DataCaster\Exceptions\CastException;
16+
use CodeIgniter\Entity\Exceptions\CastException;
1717

1818
class FloatCast extends BaseCast
1919
{

tests/system/DataConverter/DataConverterTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,4 +1040,20 @@ public static function provideEnumExceptions(): iterable
10401040
],
10411041
];
10421042
}
1043+
1044+
public function testInvalidFloatRoundingMode(): void
1045+
{
1046+
$this->expectException(CastException::class);
1047+
$this->expectExceptionMessage('Invalid rounding mode "wrong" for float casting.');
1048+
1049+
$converter = $this->createDataConverter([
1050+
'id' => 'int',
1051+
'temp' => 'float[2,wrong]',
1052+
]);
1053+
1054+
$converter->fromDataSource([
1055+
'id' => '123456',
1056+
'temp' => 123.456
1057+
]);
1058+
}
10431059
}

tests/system/Entity/EntityTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,42 @@ public function testCastFloatWithPrecisionAndRoundingMode(): void
447447

448448
$this->assertIsFloat($entity->fifteenth);
449449
$this->assertEqualsWithDelta(3.13, $entity->fifteenth, PHP_FLOAT_EPSILON);
450+
451+
$entity->sixteenth = 20.0005;
452+
453+
$this->assertIsFloat($entity->sixteenth);
454+
$this->assertEqualsWithDelta(20.000, $entity->sixteenth, PHP_FLOAT_EPSILON);
455+
456+
$entity->sixteenth = '20.0005';
457+
458+
$this->assertIsFloat($entity->sixteenth);
459+
$this->assertEqualsWithDelta(20.000, $entity->sixteenth, PHP_FLOAT_EPSILON);
460+
461+
$entity->seventeenth = 1.25;
462+
463+
$this->assertIsFloat($entity->seventeenth);
464+
$this->assertEqualsWithDelta(1.3, $entity->seventeenth, PHP_FLOAT_EPSILON);
465+
466+
$entity->seventeenth = '1.25';
467+
468+
$this->assertIsFloat($entity->seventeenth);
469+
$this->assertEqualsWithDelta(1.3, $entity->seventeenth, PHP_FLOAT_EPSILON);
470+
}
471+
472+
public function testCastFloatWithInvalidRoundingMode(): void
473+
{
474+
$this->expectException(CastException::class);
475+
$this->expectExceptionMessage('Invalid rounding mode "invalidMode" for float casting.');
476+
477+
$entity = new class () extends Entity {
478+
protected $casts = [
479+
'temp' => 'float[1,invalidMode]'
480+
];
481+
};
482+
483+
$entity->temp = '4.548';
484+
485+
$entity->temp; // @phpstan-ignore expr.resultUnused
450486
}
451487

452488
public function testCastDouble(): void
@@ -1782,6 +1818,8 @@ private function getCastEntity($data = null): object
17821818
'thirteenth' => 'uri',
17831819
'fourteenth' => 'float[2]',
17841820
'fifteenth' => 'float[2,down]',
1821+
'sixteenth' => 'float[3,even]',
1822+
'seventeenth' => 'float[1,odd]',
17851823
];
17861824

17871825
public function setSeventh(string $seventh): void

0 commit comments

Comments
 (0)