Tests / PHP 8.4 (push) Failing after 4m31s
Repository::castDatabaseValue() called TypeCaster::cast() with the enum's class name as the "type", so any row of a model declaring #[Enum] threw "Cannot assign string to property X of type Y" the moment it was read. Writes worked, which is why this survived 1.0.0: the select path is only exercised on read. castDatabaseValue() now delegates to TypeCaster::castEnum() when the property declares one, and keeps the null passthrough. Covered by EnumHydrationTest with a scalar value and a null.
45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit;
|
|
|
|
use Camelia\Orm\Orm;
|
|
use Camelia\Orm\Tests\Fixture\ArticuloConEstado;
|
|
use Camelia\Orm\Tests\Fixture\EstadoArticulo;
|
|
use Camelia\Orm\Tests\Support\FakeConnection;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Regression: reading a row whose model declares #[Enum] threw a TypeError because
|
|
* Repository::castDatabaseValue() called TypeCaster::cast() instead of castEnum().
|
|
*/
|
|
final class EnumHydrationTest extends TestCase
|
|
{
|
|
#[Test]
|
|
public function it_hydrates_an_enum_column_from_its_scalar_value(): void
|
|
{
|
|
$connection = new FakeConnection();
|
|
$connection->rows = [['codigo' => '3', 'nombre' => 'Taladro', 'estado' => 'publicado']];
|
|
|
|
$models = Orm::fromConnection($connection)->repository(ArticuloConEstado::class)->find();
|
|
|
|
self::assertCount(1, $models);
|
|
self::assertInstanceOf(ArticuloConEstado::class, $models[0]);
|
|
self::assertSame(EstadoArticulo::Publicado, $models[0]->estado);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_hydrates_a_nullable_enum_column_as_null(): void
|
|
{
|
|
$connection = new FakeConnection();
|
|
$connection->rows = [['codigo' => '4', 'nombre' => 'Sierra', 'estado' => null]];
|
|
|
|
$models = Orm::fromConnection($connection)->repository(ArticuloConEstado::class)->find();
|
|
|
|
self::assertCount(1, $models);
|
|
self::assertNull($models[0]->estado);
|
|
}
|
|
}
|