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,139 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Unit\Metadata;
|
||||
|
||||
use Camelia\Orm\Exception\MetadataException;
|
||||
use Camelia\Orm\Metadata\MetadataFactory;
|
||||
use Camelia\Orm\Tests\Fixture\Articulo;
|
||||
use Camelia\Orm\Tests\Fixture\Factura;
|
||||
use Camelia\Orm\Tests\Fixture\Invalid\ColumnaDuplicada;
|
||||
use Camelia\Orm\Tests\Fixture\Invalid\DosPrimaryKeys;
|
||||
use Camelia\Orm\Tests\Fixture\Invalid\RelacionNoNulable;
|
||||
use Camelia\Orm\Tests\Fixture\Invalid\SinPrimaryKey;
|
||||
use Camelia\Orm\Tests\Fixture\Invalid\SinTableName;
|
||||
use Camelia\Orm\Tests\Fixture\Invalid\SinTipo;
|
||||
use Camelia\Orm\Tests\Fixture\Usuario;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MetadataFactoryTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_reads_table_and_primary_key(): void
|
||||
{
|
||||
$metadata = (new MetadataFactory())->forClass(Usuario::class);
|
||||
|
||||
self::assertSame('usuarios', $metadata->table());
|
||||
self::assertSame('codigo', $metadata->primaryKey());
|
||||
self::assertSame('codigo', $metadata->primaryKeyColumn());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_converts_property_names_to_columns(): void
|
||||
{
|
||||
$metadata = (new MetadataFactory())->forClass(Usuario::class);
|
||||
|
||||
self::assertSame('password_hash', $metadata->column('passwordHash'));
|
||||
self::assertSame('passwordHash', $metadata->property('password_hash'));
|
||||
self::assertNull($metadata->property('inexistente'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_honours_an_explicit_column_name(): void
|
||||
{
|
||||
$metadata = (new MetadataFactory())->forClass(Factura::class);
|
||||
|
||||
self::assertSame('importe_total', $metadata->column('total'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_excludes_relations_from_writable_columns(): void
|
||||
{
|
||||
$metadata = (new MetadataFactory())->forClass(Factura::class);
|
||||
|
||||
self::assertSame(['usuarioCodigo', 'total'], $metadata->writable());
|
||||
self::assertCount(2, $metadata->relations());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_resolves_belongs_to_metadata(): void
|
||||
{
|
||||
$metadata = (new MetadataFactory())->forClass(Factura::class);
|
||||
$relation = $metadata->properties()['usuario']->relation;
|
||||
|
||||
self::assertNotNull($relation);
|
||||
self::assertSame('belongsTo', $relation->kind);
|
||||
self::assertSame(Usuario::class, $relation->relatedClass);
|
||||
self::assertSame('usuarioCodigo', $relation->foreignKey);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_resolves_belongs_to_many_pivot_metadata(): void
|
||||
{
|
||||
$metadata = (new MetadataFactory())->forClass(Articulo::class);
|
||||
$relation = $metadata->properties()['etiquetas']->relation;
|
||||
|
||||
self::assertNotNull($relation);
|
||||
self::assertSame('belongsToMany', $relation->kind);
|
||||
self::assertSame('articulo_etiqueta', $relation->through);
|
||||
self::assertSame('articulo_codigo', $relation->foreignPivotKey);
|
||||
self::assertSame('etiqueta_codigo', $relation->relatedPivotKey);
|
||||
self::assertTrue($relation->isToMany());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_a_model_without_table_name(): void
|
||||
{
|
||||
$this->expectException(MetadataException::class);
|
||||
$this->expectExceptionMessage('[M2]');
|
||||
|
||||
(new MetadataFactory())->forClass(SinTableName::class);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_a_model_without_primary_key(): void
|
||||
{
|
||||
$this->expectException(MetadataException::class);
|
||||
$this->expectExceptionMessage('[M3]');
|
||||
|
||||
(new MetadataFactory())->forClass(SinPrimaryKey::class);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_a_model_with_two_primary_keys(): void
|
||||
{
|
||||
$this->expectException(MetadataException::class);
|
||||
$this->expectExceptionMessage('[M3]');
|
||||
|
||||
(new MetadataFactory())->forClass(DosPrimaryKeys::class);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_an_untyped_property(): void
|
||||
{
|
||||
$this->expectException(MetadataException::class);
|
||||
$this->expectExceptionMessage('[M4]');
|
||||
|
||||
(new MetadataFactory())->forClass(SinTipo::class);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_duplicate_columns(): void
|
||||
{
|
||||
$this->expectException(MetadataException::class);
|
||||
$this->expectExceptionMessage('[M7]');
|
||||
|
||||
(new MetadataFactory())->forClass(ColumnaDuplicada::class);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_rejects_a_non_nullable_relation(): void
|
||||
{
|
||||
$this->expectException(MetadataException::class);
|
||||
$this->expectExceptionMessage('[M5]');
|
||||
|
||||
(new MetadataFactory())->forClass(RelacionNoNulable::class);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user