feat: initial commit of camelia/orm
Attribute-mapped Active Record ORM for MariaDB/MySQL over PDO. - Attribute-driven metadata: TableName, PrimaryKey, AutoIncrement, Column, Guarded, Ignore, Required, Enum, Nullable, Length, Precision, Unique, UniqueGroup, Index, MaxLength. - Relationship mapping: BelongsTo, HasOne, HasMany, BelongsToMany with batched eager loading and a 3-level depth limit. - Orm entry point with fromArray, fromPdo, fromEnv and nested transactions. - Immutable Query with Cond operators and GroupCond grouping. - Repository read and write API, pagination, and typed exception hierarchy. - Declares PHP >=8.3; Mago and PHPStan pin the parser to 8.3. - MIT license.
This commit is contained in:
commit
66e93e3070
97 files changed
+12188
No files matched your search
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Unit\Query;
|
||||
|
||||
use Camelia\Orm\Exception\CriteriaException;
|
||||
use Camelia\Orm\Metadata\MetadataFactory;
|
||||
use Camelia\Orm\Query\Cond;
|
||||
use Camelia\Orm\Query\CriteriaCompiler;
|
||||
use Camelia\Orm\Query\MySqlDialect;
|
||||
use Camelia\Orm\Tests\Fixture\Usuario;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CriteriaCompilerTest extends TestCase
|
||||
{
|
||||
private CriteriaCompiler $compiler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->compiler = new CriteriaCompiler(new MySqlDialect());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_an_empty_fragment_without_criteria(): void
|
||||
{
|
||||
[$sql, $params] = $this->compiler->compile($this->metadata(), []);
|
||||
|
||||
self::assertSame('', $sql);
|
||||
self::assertSame([], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_scalar_equality(): void
|
||||
{
|
||||
[$sql, $params] = $this->compiler->compile($this->metadata(), ['activo' => true]);
|
||||
|
||||
self::assertSame('`activo` = ?', $sql);
|
||||
self::assertSame([true], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_accepts_column_names_as_keys(): void
|
||||
{
|
||||
[$sql] = $this->compiler->compile($this->metadata(), ['password_hash' => 'x']);
|
||||
|
||||
self::assertSame('`password_hash` = ?', $sql);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_arrays_as_in(): void
|
||||
{
|
||||
[$sql, $params] = $this->compiler->compile($this->metadata(), ['codigo' => [1, 2]]);
|
||||
|
||||
self::assertSame('`codigo` IN (?, ?)', $sql);
|
||||
self::assertSame([1, 2], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_an_empty_array_as_always_false(): void
|
||||
{
|
||||
[$sql, $params] = $this->compiler->compile($this->metadata(), ['codigo' => []]);
|
||||
|
||||
self::assertSame('1 = 0', $sql);
|
||||
self::assertSame([], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_null_as_is_null(): void
|
||||
{
|
||||
[$sql, $params] = $this->compiler->compile($this->metadata(), ['apodo' => null]);
|
||||
|
||||
self::assertSame('`apodo` IS NULL', $sql);
|
||||
self::assertSame([], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_combines_criteria_with_and(): void
|
||||
{
|
||||
[$sql, $params] = $this->compiler->compile($this->metadata(), [
|
||||
'activo' => true,
|
||||
'nombre' => 'ana',
|
||||
]);
|
||||
|
||||
self::assertSame('`activo` = ? AND `nombre` = ?', $sql);
|
||||
self::assertSame([true, 'ana'], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_every_cond_operator(): void
|
||||
{
|
||||
$cases = [
|
||||
['neq', Cond::neq(1), '`codigo` <> ?'],
|
||||
['gt', Cond::gt(1), '`codigo` > ?'],
|
||||
['gte', Cond::gte(1), '`codigo` >= ?'],
|
||||
['lt', Cond::lt(1), '`codigo` < ?'],
|
||||
['lte', Cond::lte(1), '`codigo` <= ?'],
|
||||
['like', Cond::like('%a%'), '`codigo` LIKE ?'],
|
||||
['notLike', Cond::notLike('%a%'), '`codigo` NOT LIKE ?'],
|
||||
['between', Cond::between(1, 2), '`codigo` BETWEEN ? AND ?'],
|
||||
['notBetween', Cond::notBetween(1, 2), '`codigo` NOT BETWEEN ? AND ?'],
|
||||
['isNull', Cond::isNull(), '`codigo` IS NULL'],
|
||||
['isNotNull', Cond::isNotNull(), '`codigo` IS NOT NULL'],
|
||||
['in', Cond::in([1, 2]), '`codigo` IN (?, ?)'],
|
||||
['notIn', Cond::notIn([1, 2]), '`codigo` NOT IN (?, ?)'],
|
||||
];
|
||||
|
||||
foreach ($cases as [$label, $cond, $expected]) {
|
||||
[$sql] = $this->compiler->compile($this->metadata(), ['codigo' => $cond]);
|
||||
self::assertSame($expected, $sql, $label);
|
||||
}
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_raw_conditions(): void
|
||||
{
|
||||
[$sql, $params] =
|
||||
$this->compiler->compile($this->metadata(), ['codigo' => Cond::raw('codigo > ?', [
|
||||
10,
|
||||
])]);
|
||||
|
||||
self::assertSame('(codigo > ?)', $sql);
|
||||
self::assertSame([10], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_an_unknown_column_naming_the_valid_ones(): void
|
||||
{
|
||||
$this->expectException(CriteriaException::class);
|
||||
$this->expectExceptionMessage('Unknown column "inexistente"');
|
||||
|
||||
$this->compiler->compile($this->metadata(), ['inexistente' => 1]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_an_empty_in_cond(): void
|
||||
{
|
||||
$this->expectException(CriteriaException::class);
|
||||
|
||||
$this->compiler->compile($this->metadata(), ['codigo' => Cond::in([])]);
|
||||
}
|
||||
|
||||
private function metadata(): \Camelia\Orm\Metadata\ClassMetadata
|
||||
{
|
||||
return (new MetadataFactory())->forClass(Usuario::class);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user