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,234 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Unit;
|
||||
|
||||
use Camelia\Orm\Exception\RecordNotFoundException;
|
||||
use Camelia\Orm\Pagination\Page;
|
||||
use Camelia\Orm\Query\Cond;
|
||||
use Camelia\Orm\Repository;
|
||||
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 RepositoryTest extends TestCase
|
||||
{
|
||||
private FakeConnection $connection;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->connection = new FakeConnection();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_hydrates_rows_into_models(): void
|
||||
{
|
||||
$this->connection->rows = [
|
||||
[
|
||||
'codigo' => '5',
|
||||
'nombre' => 'Ana',
|
||||
'correo' => 'a@x',
|
||||
'password_hash' => 'h',
|
||||
'activo' => '1',
|
||||
'apodo' => null,
|
||||
'clave' => 'k',
|
||||
],
|
||||
];
|
||||
|
||||
$result = $this->repository(Usuario::class)->find();
|
||||
|
||||
self::assertCount(1, $result);
|
||||
$usuario = $result[0];
|
||||
self::assertInstanceOf(Usuario::class, $usuario);
|
||||
self::assertSame(5, $usuario->codigo);
|
||||
self::assertSame('Ana', $usuario->nombre);
|
||||
self::assertTrue($usuario->activo);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_when_find_first_matches_nothing(): void
|
||||
{
|
||||
$this->connection->rows = [];
|
||||
|
||||
self::assertNull($this->repository(Usuario::class)->findFirst());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_throws_when_find_by_pk_fails(): void
|
||||
{
|
||||
$this->connection->rows = [];
|
||||
|
||||
$this->expectException(RecordNotFoundException::class);
|
||||
|
||||
$this->repository(Usuario::class)->findByPkOrFail(99);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_finds_by_primary_key_through_a_delegated_criteria(): void
|
||||
{
|
||||
$this->connection->rows = [];
|
||||
|
||||
$this->repository(Usuario::class)->findByPk(4);
|
||||
|
||||
self::assertStringContainsString(
|
||||
'WHERE `codigo` = ?',
|
||||
$this->connection->selected[0]['sql'],
|
||||
);
|
||||
self::assertSame([4], $this->connection->selected[0]['params']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_counts_through_a_scalar_query(): void
|
||||
{
|
||||
$this->connection->scalars = [7];
|
||||
|
||||
self::assertSame(7, $this->repository(Usuario::class)->count(['activo' => true]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_reports_existence(): void
|
||||
{
|
||||
$this->connection->scalars = [1];
|
||||
|
||||
self::assertTrue($this->repository(Usuario::class)->exists(['activo' => true]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_aggregates(): void
|
||||
{
|
||||
$this->connection->scalars = [10.5, null];
|
||||
|
||||
self::assertSame(10.5, $this->repository(Usuario::class)->sum('codigo'));
|
||||
self::assertNull($this->repository(Usuario::class)->max('codigo'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_pages_with_total_and_items(): void
|
||||
{
|
||||
$this->connection->scalars = [3];
|
||||
$this->connection->rows = [
|
||||
[
|
||||
'codigo' => '1',
|
||||
'nombre' => 'A',
|
||||
'correo' => '',
|
||||
'password_hash' => '',
|
||||
'activo' => '1',
|
||||
'apodo' => null,
|
||||
'clave' => '',
|
||||
],
|
||||
];
|
||||
|
||||
$page = $this->repository(Usuario::class)->page(
|
||||
['activo' => true],
|
||||
[['nombre', 'DESC']],
|
||||
1,
|
||||
2,
|
||||
);
|
||||
|
||||
self::assertInstanceOf(Page::class, $page);
|
||||
self::assertSame(3, $page->total);
|
||||
self::assertSame(2, $page->totalPages);
|
||||
self::assertCount(1, $page->items);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_creates_a_model_and_inserts_it(): void
|
||||
{
|
||||
$this->connection->lastInsertId = '9';
|
||||
|
||||
$usuario = $this->repository(Usuario::class)->create(['nombre' => 'Ana', 'clave' => 'abc']);
|
||||
|
||||
self::assertInstanceOf(Usuario::class, $usuario);
|
||||
self::assertSame(9, $usuario->codigo);
|
||||
self::assertStringStartsWith('INSERT INTO `usuarios`', $this->connection->lastSql());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_finds_or_creates(): void
|
||||
{
|
||||
$this->connection->rows = [];
|
||||
$this->connection->lastInsertId = '11';
|
||||
|
||||
$usuario = $this->repository(Usuario::class)->findOrCreate(['correo' => 'a@x'], [
|
||||
'nombre' => 'Ana',
|
||||
'clave' => 'abc',
|
||||
]);
|
||||
|
||||
self::assertInstanceOf(Usuario::class, $usuario);
|
||||
self::assertSame('a@x', $usuario->correo);
|
||||
self::assertSame(11, $usuario->codigo);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_loads_a_belongs_to_relation_in_one_query(): void
|
||||
{
|
||||
$this->connection->rows = [$this->usuarioRow(7, 'Ana')];
|
||||
|
||||
$facturaList = $this->repository(Factura::class)->find(with: ['usuario']);
|
||||
|
||||
self::assertCount(1, $facturaList);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_marks_models_partial_when_selecting_columns(): void
|
||||
{
|
||||
$this->connection->rows = [['codigo' => '1', 'nombre' => 'Ana']];
|
||||
|
||||
$result = $this->repository(Usuario::class)->query()->select('codigo', 'nombre')->get();
|
||||
|
||||
self::assertTrue($result[0]->isPartial());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_an_unknown_relation(): void
|
||||
{
|
||||
$this->connection->rows = [];
|
||||
|
||||
$this->expectException(\Camelia\Orm\Exception\MetadataException::class);
|
||||
|
||||
$this->repository(Usuario::class)->find(with: ['inexistente']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_binds_params_in_criteria_order(): void
|
||||
{
|
||||
$this->connection->rows = [];
|
||||
|
||||
$this->repository(Usuario::class)->find(['activo' => true, 'codigo' => Cond::in([1, 2])]);
|
||||
|
||||
self::assertSame([true, 1, 2], $this->connection->selected[0]['params']);
|
||||
self::assertSame(
|
||||
'SELECT `codigo`, `nombre`, `correo`, `password_hash`, `activo`, `apodo`, `clave`'
|
||||
. ' FROM `usuarios` WHERE `activo` = ? AND `codigo` IN (?, ?)',
|
||||
$this->connection->selected[0]['sql'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 usuarioRow(int $codigo, string $nombre): array
|
||||
{
|
||||
return [
|
||||
'codigo' => (string) $codigo,
|
||||
'nombre' => $nombre,
|
||||
'correo' => 'a@x',
|
||||
'password_hash' => '',
|
||||
'activo' => '1',
|
||||
'apodo' => null,
|
||||
'clave' => '',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user