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.
206 lines
6.2 KiB
PHP
206 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit;
|
|
|
|
use Camelia\Orm\Exception\MetadataException;
|
|
use Camelia\Orm\Repository;
|
|
use Camelia\Orm\Tests\Fixture\Articulo;
|
|
use Camelia\Orm\Tests\Fixture\Detalle;
|
|
use Camelia\Orm\Tests\Fixture\Etiqueta;
|
|
use Camelia\Orm\Tests\Fixture\Factura;
|
|
use Camelia\Orm\Tests\Fixture\Usuario;
|
|
use Camelia\Orm\Tests\Support\FakeConnection;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class EagerLoadingTest extends TestCase
|
|
{
|
|
private FakeConnection $connection;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->connection = new FakeConnection();
|
|
}
|
|
|
|
#[Test]
|
|
public function it_loads_belongs_to_in_one_batch_query(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[$this->facturaRow(1, 7), $this->facturaRow(2, 8)],
|
|
[$this->usuarioRow(7, 'Ana'), $this->usuarioRow(8, 'Bea')],
|
|
];
|
|
|
|
$facturas = $this->repository(Factura::class)->find(with: ['usuario']);
|
|
|
|
self::assertCount(2, $facturas);
|
|
$primera = $facturas[0];
|
|
self::assertInstanceOf(Factura::class, $primera);
|
|
self::assertInstanceOf(Usuario::class, $primera->usuario);
|
|
self::assertSame('Ana', $primera->usuario->nombre);
|
|
self::assertCount(2, $this->connection->selected);
|
|
self::assertStringContainsString('IN (?, ?)', $this->connection->selected[1]['sql']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_loads_has_many_grouped_by_parent(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[$this->facturaRow(1, 7)],
|
|
[
|
|
['codigo' => '10', 'factura_codigo' => '1', 'concepto' => 'A'],
|
|
['codigo' => '11', 'factura_codigo' => '1', 'concepto' => 'B'],
|
|
],
|
|
];
|
|
|
|
$facturas = $this->repository(Factura::class)->find(with: ['detalles']);
|
|
$factura = $facturas[0];
|
|
|
|
self::assertInstanceOf(Factura::class, $factura);
|
|
self::assertCount(2, $factura->detalles);
|
|
$detalle = $factura->detalles[0];
|
|
self::assertInstanceOf(Detalle::class, $detalle);
|
|
self::assertSame('A', $detalle->concepto);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_loads_belongs_to_many_through_the_pivot(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[['codigo' => '1', 'nombre' => 'Articulo']],
|
|
[
|
|
['pivot_owner' => '1', 'pivot_related' => '5'],
|
|
['pivot_owner' => '1', 'pivot_related' => '6'],
|
|
],
|
|
[
|
|
['codigo' => '5', 'nombre' => 'rojo'],
|
|
['codigo' => '6', 'nombre' => 'verde'],
|
|
],
|
|
];
|
|
|
|
$articulos = $this->repository(Articulo::class)->find(with: ['etiquetas']);
|
|
$articulo = $articulos[0];
|
|
|
|
self::assertInstanceOf(Articulo::class, $articulo);
|
|
self::assertCount(2, $articulo->etiquetas);
|
|
$etiqueta = $articulo->etiquetas[0];
|
|
self::assertInstanceOf(Etiqueta::class, $etiqueta);
|
|
self::assertSame('rojo', $etiqueta->nombre);
|
|
self::assertStringContainsString(
|
|
'`articulo_etiqueta`',
|
|
$this->connection->selected[1]['sql'],
|
|
);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_leaves_belongs_to_null_when_the_row_is_missing(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[$this->facturaRow(1, 99)],
|
|
[],
|
|
];
|
|
|
|
$facturas = $this->repository(Factura::class)->find(with: ['usuario']);
|
|
$factura = $facturas[0];
|
|
|
|
self::assertInstanceOf(Factura::class, $factura);
|
|
self::assertNull($factura->usuario);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refuses_an_unknown_relation(): void
|
|
{
|
|
$this->connection->rowBatches = [];
|
|
|
|
$this->expectException(MetadataException::class);
|
|
|
|
$this->repository(Factura::class)->find(with: ['usuario.inexistente']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_accepts_deeply_nested_paths_up_to_the_limit(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[['codigo' => '1', 'nombre' => 'Articulo']],
|
|
[['pivot_owner' => '1', 'pivot_related' => '5']],
|
|
[['codigo' => '5', 'nombre' => 'rojo']],
|
|
[$this->facturaRow(1, 7)],
|
|
[$this->usuarioRow(7, 'Ana')],
|
|
];
|
|
|
|
$articulos = $this->repository(Articulo::class)->find(with: ['etiquetas.facturas.usuario']);
|
|
|
|
self::assertCount(1, $articulos);
|
|
$articulo = $articulos[0];
|
|
self::assertInstanceOf(Articulo::class, $articulo);
|
|
self::assertCount(1, $articulo->etiquetas);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refuses_an_unknown_nested_relation(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[$this->facturaRow(1, 7)],
|
|
[$this->usuarioRow(7, 'Ana')],
|
|
];
|
|
|
|
$this->expectException(MetadataException::class);
|
|
|
|
$this->repository(Factura::class)->find(with: ['usuario.inexistente']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refreshes_relations_on_a_hydrated_model(): void
|
|
{
|
|
$this->connection->rowBatches = [
|
|
[$this->usuarioRow(7, 'Ana')],
|
|
];
|
|
|
|
$factura = new Factura();
|
|
$factura->codigo = 1;
|
|
$factura->usuarioCodigo = 7;
|
|
|
|
$this->repository(Factura::class)->refreshRelations($factura, ['usuario']);
|
|
|
|
self::assertInstanceOf(Usuario::class, $factura->usuario);
|
|
}
|
|
|
|
/**
|
|
* @param class-string<\Camelia\Orm\Model> $class
|
|
* @return Repository<\Camelia\Orm\Model>
|
|
*/
|
|
private function repository(string $class): Repository
|
|
{
|
|
return new Repository($this->connection, $class);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function facturaRow(int $codigo, int $usuarioCodigo): array
|
|
{
|
|
return [
|
|
'codigo' => (string) $codigo,
|
|
'usuario_codigo' => (string) $usuarioCodigo,
|
|
'importe_total' => '10.5',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function usuarioRow(int $codigo, string $nombre): array
|
|
{
|
|
return [
|
|
'codigo' => (string) $codigo,
|
|
'nombre' => $nombre,
|
|
'correo' => 'a@x',
|
|
'password_hash' => '',
|
|
'activo' => '1',
|
|
'apodo' => null,
|
|
'clave' => '',
|
|
];
|
|
}
|
|
}
|