Using an enum as a criteria value reached PDO as an object and failed with "Object of class X could not be converted to string". Filtering by a state column - the most natural thing to do with an enum-mapped property - was impossible. CriteriaCompiler now unwraps \BackedEnum both for a plain value and inside an an IN list. Covered by CriteriaCompilerEnumTest.
54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit\Query;
|
|
|
|
use Camelia\Orm\Metadata\MetadataFactory;
|
|
use Camelia\Orm\Query\CriteriaCompiler;
|
|
use Camelia\Orm\Query\MySqlDialect;
|
|
use Camelia\Orm\Tests\Fixture\ArticuloConEstado;
|
|
use Camelia\Orm\Tests\Fixture\EstadoPedido;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Regression: an enum used as a criteria value reached PDO as an object and failed with
|
|
* "Object of class X could not be converted to string". A backed enum must be compared by its
|
|
* scalar value, both alone and inside an IN list.
|
|
*/
|
|
final class CriteriaCompilerEnumTest extends TestCase
|
|
{
|
|
private CriteriaCompiler $compiler;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->compiler = new CriteriaCompiler(new MySqlDialect());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_an_enum_criterion_to_its_scalar_value(): void
|
|
{
|
|
$metadata = new MetadataFactory()->forClass(ArticuloConEstado::class);
|
|
|
|
[$sql, $params] = $this->compiler->compile($metadata, [
|
|
'estado' => EstadoPedido::Confirmado,
|
|
]);
|
|
|
|
self::assertSame('`estado` = ?', $sql);
|
|
self::assertSame(['confirmado'], $params);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_a_list_of_enums_to_their_scalar_values(): void
|
|
{
|
|
$metadata = new MetadataFactory()->forClass(ArticuloConEstado::class);
|
|
|
|
[, $params] = $this->compiler->compile($metadata, [
|
|
'estado' => [EstadoPedido::Pendiente, EstadoPedido::Confirmado],
|
|
]);
|
|
|
|
self::assertSame(['pendiente', 'confirmado'], $params);
|
|
}
|
|
}
|