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,166 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Unit;
|
||||
|
||||
use Camelia\Orm\Tests\Fixture\Factura;
|
||||
use Camelia\Orm\Tests\Fixture\Usuario;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ModelTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_initializes_neutral_values(): void
|
||||
{
|
||||
$usuario = new Usuario();
|
||||
|
||||
self::assertNull($usuario->codigo);
|
||||
self::assertSame('', $usuario->nombre);
|
||||
self::assertTrue($usuario->activo);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_preserves_declared_defaults(): void
|
||||
{
|
||||
$factura = new Factura();
|
||||
|
||||
self::assertSame(0.0, $factura->total);
|
||||
self::assertSame([], $factura->detalles);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_hydrates_by_property_name(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['nombre' => 'Ana', 'activo' => false]);
|
||||
|
||||
self::assertSame('Ana', $usuario->nombre);
|
||||
self::assertFalse($usuario->activo);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_hydrates_by_column_name(): void
|
||||
{
|
||||
$factura = (new Factura())->hydrate(['importe_total' => '12.5']);
|
||||
|
||||
self::assertSame(12.5, $factura->total);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_prefers_the_property_when_both_forms_are_present(): void
|
||||
{
|
||||
$factura = (new Factura())->hydrate(['total' => 1.0, 'importe_total' => 2.0]);
|
||||
|
||||
self::assertSame(1.0, $factura->total);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_skips_guarded_properties(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['passwordHash' => 'secreto']);
|
||||
|
||||
self::assertSame('', $usuario->passwordHash);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_guarded_properties_in_strict_mode(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
(new Usuario())->hydrateStrict(['passwordHash' => 'secreto']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_ignores_unknown_keys(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['inexistente' => 1, 'nombre' => 'Ana']);
|
||||
|
||||
self::assertSame('Ana', $usuario->nombre);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_never_hydrates_the_primary_key(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['codigo' => 99]);
|
||||
|
||||
self::assertNull($usuario->codigo);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_casts_and_refuses_uncastable_values(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
(new Usuario())->hydrate(['activo' => 'no-es-bool']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_serializes_writable_columns_by_column_name(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['nombre' => 'Ana']);
|
||||
$array = $usuario->toArray();
|
||||
|
||||
self::assertSame('Ana', $array['nombre']);
|
||||
self::assertArrayHasKey('password_hash', $array);
|
||||
self::assertArrayNotHasKey('codigo', $array);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_serializes_json(): void
|
||||
{
|
||||
$json = (new Usuario())->hydrate(['nombre' => 'Ana'])->toJson();
|
||||
$decoded = json_decode($json, true);
|
||||
|
||||
self::assertIsArray($decoded);
|
||||
self::assertSame('Ana', $decoded['nombre']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_validates_required_and_max_length(): void
|
||||
{
|
||||
$usuario = new Usuario();
|
||||
$errors = $usuario->validate();
|
||||
|
||||
self::assertArrayHasKey('clave', $errors);
|
||||
self::assertStringContainsString('requerido', $errors['clave'][0]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_reports_a_too_long_value(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['clave' => 'supercalifragilistico']);
|
||||
|
||||
self::assertStringContainsString('10', $usuario->validate()['clave'][0]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_is_valid_when_no_rule_fails(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['clave' => 'abc']);
|
||||
|
||||
self::assertTrue($usuario->isValid());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_replicates_without_the_primary_key(): void
|
||||
{
|
||||
$usuario = (new Usuario())->hydrate(['nombre' => 'Ana']);
|
||||
$usuario->codigo = 5;
|
||||
|
||||
$copy = $usuario->replicate();
|
||||
|
||||
self::assertNull($copy->codigo);
|
||||
self::assertSame('Ana', $copy->nombre);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_tracks_partial_state(): void
|
||||
{
|
||||
$usuario = new Usuario();
|
||||
|
||||
self::assertFalse($usuario->isPartial());
|
||||
$usuario->markPartial();
|
||||
self::assertTrue($usuario->isPartial());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user