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.
177 lines
4.9 KiB
PHP
177 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit;
|
|
|
|
use Camelia\Orm\Query\Cond;
|
|
use Camelia\Orm\Query\GroupCond;
|
|
use Camelia\Orm\Repository;
|
|
use Camelia\Orm\Tests\Fixture\Usuario;
|
|
use Camelia\Orm\Tests\Support\FakeConnection;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class QueryTest extends TestCase
|
|
{
|
|
private FakeConnection $connection;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->connection = new FakeConnection();
|
|
}
|
|
|
|
#[Test]
|
|
public function it_builds_a_select_with_where(): void
|
|
{
|
|
$sql = $this->repository(Usuario::class)->query()->where('activo', true)->toSql();
|
|
|
|
self::assertStringContainsString('WHERE (`activo` = ?)', $sql['sql']);
|
|
self::assertSame([true], $sql['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_is_immutable(): void
|
|
{
|
|
$base = $this->repository(Usuario::class)->query()->where('activo', true);
|
|
$derived = $base->where('nombre', 'Ana');
|
|
|
|
self::assertCount(1, $base->toSql()['params']);
|
|
self::assertCount(2, $derived->toSql()['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_places_where_before_order_and_limit(): void
|
|
{
|
|
$sql = $this->repository(Usuario::class)
|
|
->query()
|
|
->where('activo', true)
|
|
->orderByDesc('nombre')
|
|
->limit(5)
|
|
->toSql()['sql'];
|
|
|
|
self::assertStringContainsString('WHERE (`activo` = ?) ORDER BY', $sql);
|
|
self::assertStringContainsString('LIMIT 5', $sql);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_builds_or_groups(): void
|
|
{
|
|
$sql = $this->repository(Usuario::class)
|
|
->query()
|
|
->whereGroup(static function (GroupCond $group): void {
|
|
$group->or('nombre', 'Ana');
|
|
$group->or('nombre', 'Bea');
|
|
})
|
|
->toSql();
|
|
|
|
self::assertStringContainsString('WHERE (`nombre` = ? OR `nombre` = ?)', $sql['sql']);
|
|
self::assertCount(2, $sql['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_combines_groups_with_and(): void
|
|
{
|
|
$sql = $this->repository(Usuario::class)
|
|
->query()
|
|
->where('activo', true)
|
|
->whereGroup(static function (GroupCond $group): void {
|
|
$group->or('nombre', 'Ana');
|
|
})
|
|
->toSql()['sql'];
|
|
|
|
self::assertStringContainsString('(`activo` = ?) AND', $sql);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_supports_where_in_and_null_helpers(): void
|
|
{
|
|
$sql = $this->repository(Usuario::class)
|
|
->query()
|
|
->whereIn('codigo', [1, 2])
|
|
->whereNull('apodo')
|
|
->toSql();
|
|
|
|
self::assertStringContainsString('`codigo` IN (?, ?)', $sql['sql']);
|
|
self::assertStringContainsString('`apodo` IS NULL', $sql['sql']);
|
|
self::assertSame([1, 2], $sql['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_plucks_a_single_column(): void
|
|
{
|
|
$this->connection->rows = [['nombre' => 'Ana'], ['nombre' => 'Bea']];
|
|
|
|
$values = $this->repository(Usuario::class)->query()->pluck('nombre');
|
|
|
|
self::assertSame(['Ana', 'Bea'], $values);
|
|
self::assertStringContainsString(
|
|
'SELECT `nombre` FROM',
|
|
$this->connection->selected[0]['sql'],
|
|
);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_counts(): void
|
|
{
|
|
$this->connection->scalars = [4];
|
|
|
|
self::assertSame(
|
|
4,
|
|
$this->repository(Usuario::class)->query()->where('activo', true)->count(),
|
|
);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_the_first_row_or_null(): void
|
|
{
|
|
$this->connection->rows = [];
|
|
|
|
self::assertNull($this->repository(Usuario::class)->query()->first());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_fails_first_or_fail_when_empty(): void
|
|
{
|
|
$this->connection->rows = [];
|
|
|
|
$this->expectException(\Camelia\Orm\Exception\RecordNotFoundException::class);
|
|
|
|
$this->repository(Usuario::class)->query()->firstOrFail();
|
|
}
|
|
|
|
#[Test]
|
|
public function it_pages(): void
|
|
{
|
|
$this->connection->scalars = [0];
|
|
$this->connection->rows = [];
|
|
|
|
$page = $this->repository(Usuario::class)->query()->page(2, 5);
|
|
|
|
self::assertSame(2, $page->page);
|
|
self::assertSame(0, $page->total);
|
|
self::assertStringContainsString('LIMIT 5 OFFSET 5', $this->connection->selected[1]['sql']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_accepts_a_cond_in_where(): void
|
|
{
|
|
$sql = $this->repository(Usuario::class)
|
|
->query()
|
|
->where('codigo', Cond::between(1, 9))
|
|
->toSql();
|
|
|
|
self::assertStringContainsString('BETWEEN ? AND ?', $sql['sql']);
|
|
self::assertSame([1, 9], $sql['params']);
|
|
}
|
|
|
|
/**
|
|
* @param class-string<\Camelia\Orm\Model> $class
|
|
* @return Repository<\Camelia\Orm\Model>
|
|
*/
|
|
private function repository(string $class): Repository
|
|
{
|
|
return new Repository($this->connection, $class);
|
|
}
|
|
}
|