Tests / PHP 8.4 (push) Failing after 5m34s
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs only on 8.4. - composer.json: require php >=8.4. - mago.toml: php-version = "8.4", matching the minimum supported version. - phpstan.neon: phpVersion 80400. - Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the 8.4 formatter now emits. - Fix the type errors PHPStan 2 reports: class-string<static> variance, optional array offsets in fromEnvironment, list vs array for PdoConnection::$lru, ReflectionClass<Model> docblocks, Repository made covariant in T, and a redundant null comparison in validateRelationPath. - CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php, which failed with a TLS handshake timeout on the runner. BREAKING CHANGE: PHP 8.4 is now required.
140 lines
4.5 KiB
PHP
140 lines
4.5 KiB
PHP
<?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);
|
|
}
|
|
}
|