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.
71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit;
|
|
|
|
use Camelia\Orm\Orm;
|
|
use Camelia\Orm\Repository;
|
|
use Camelia\Orm\Tests\Fixture\Usuario;
|
|
use Camelia\Orm\Tests\Support\FakeConnection;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class OrmTest extends TestCase
|
|
{
|
|
#[Test]
|
|
public function it_returns_a_repository_for_a_model(): void
|
|
{
|
|
$orm = Orm::fromConnection(new FakeConnection());
|
|
|
|
self::assertInstanceOf(Repository::class, $orm->repository(Usuario::class));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_reuses_the_same_repository_instance(): void
|
|
{
|
|
$orm = Orm::fromConnection(new FakeConnection());
|
|
|
|
self::assertSame($orm->repository(Usuario::class), $orm->repository(Usuario::class));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_commits_a_successful_transaction(): void
|
|
{
|
|
$connection = new FakeConnection();
|
|
$orm = Orm::fromConnection($connection);
|
|
|
|
$result = $orm->transaction(static fn(): string => 'ok');
|
|
|
|
self::assertSame('ok', $result);
|
|
self::assertSame(1, $connection->commitCount);
|
|
self::assertSame(0, $connection->rollBackCount);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_rolls_back_and_rethrows(): void
|
|
{
|
|
$connection = new FakeConnection();
|
|
$orm = Orm::fromConnection($connection);
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
try {
|
|
$orm->transaction(static function (): void {
|
|
throw new \RuntimeException('boom');
|
|
});
|
|
} finally {
|
|
self::assertSame(1, $connection->rollBackCount);
|
|
self::assertSame(0, $connection->commitCount);
|
|
}
|
|
}
|
|
|
|
#[Test]
|
|
public function it_exposes_the_connection(): void
|
|
{
|
|
$connection = new FakeConnection();
|
|
|
|
self::assertSame($connection, Orm::fromConnection($connection)->connection());
|
|
}
|
|
}
|