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.
178 lines
5.1 KiB
PHP
178 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit;
|
|
|
|
use Camelia\Orm\Exception\PartialModelException;
|
|
use Camelia\Orm\Exception\ValidationException;
|
|
use Camelia\Orm\Persistence\AuditListener;
|
|
use Camelia\Orm\Persistence\Persister;
|
|
use Camelia\Orm\Query\MySqlDialect;
|
|
use Camelia\Orm\Query\QueryBuilder;
|
|
use Camelia\Orm\Tests\Fixture\Usuario;
|
|
use Camelia\Orm\Tests\Support\FakeConnection;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class PersisterTest extends TestCase
|
|
{
|
|
private FakeConnection $connection;
|
|
|
|
private Persister $persister;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->connection = new FakeConnection();
|
|
$this->persister = new Persister($this->connection, new QueryBuilder(new MySqlDialect()));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_inserts_and_assigns_the_generated_key(): void
|
|
{
|
|
$this->connection->lastInsertId = '42';
|
|
$usuario = new Usuario()->hydrate(['nombre' => 'Ana', 'clave' => 'abc']);
|
|
|
|
$key = $this->persister->insert($usuario);
|
|
|
|
self::assertSame(42, $key);
|
|
self::assertSame(42, $usuario->codigo);
|
|
self::assertStringStartsWith('INSERT INTO `usuarios`', $this->connection->lastSql());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_only_writes_writable_columns(): void
|
|
{
|
|
$this->connection->lastInsertId = '1';
|
|
$usuario = new Usuario()->hydrate(['nombre' => 'Ana', 'clave' => 'abc']);
|
|
|
|
$this->persister->insert($usuario);
|
|
|
|
$sql = $this->connection->lastSql();
|
|
|
|
self::assertStringNotContainsString('`codigo`', $sql);
|
|
self::assertStringContainsString('`password_hash`', $sql);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_saves_as_update_when_the_key_is_present(): void
|
|
{
|
|
$usuario = new Usuario()->hydrate(['nombre' => 'Ana', 'clave' => 'abc']);
|
|
$usuario->codigo = 9;
|
|
|
|
$this->persister->save($usuario);
|
|
|
|
self::assertStringStartsWith('UPDATE `usuarios`', $this->connection->lastSql());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_saves_as_insert_when_the_key_is_absent(): void
|
|
{
|
|
$this->connection->lastInsertId = '3';
|
|
$usuario = new Usuario()->hydrate(['nombre' => 'Ana', 'clave' => 'abc']);
|
|
|
|
$this->persister->save($usuario);
|
|
|
|
self::assertStringStartsWith('INSERT INTO `usuarios`', $this->connection->lastSql());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refuses_an_invalid_model(): void
|
|
{
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$this->persister->insert(new Usuario()->hydrate(['nombre' => 'Ana']));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refuses_to_write_a_partial_model(): void
|
|
{
|
|
$usuario = new Usuario()->hydrate(['nombre' => 'Ana', 'clave' => 'abc']);
|
|
$usuario->codigo = 1;
|
|
$usuario->markPartial();
|
|
|
|
$this->expectException(PartialModelException::class);
|
|
|
|
$this->persister->update($usuario);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_patches_only_the_given_columns(): void
|
|
{
|
|
$this->persister->patch(new Usuario(), 5, ['nombre' => 'Bea']);
|
|
|
|
self::assertSame(
|
|
'UPDATE `usuarios` SET `nombre` = ? WHERE `codigo` = ?',
|
|
$this->connection->lastSql(),
|
|
);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_removes_by_primary_key(): void
|
|
{
|
|
$removed = $this->persister->remove(new Usuario(), 5);
|
|
|
|
self::assertTrue($removed);
|
|
self::assertSame('DELETE FROM `usuarios` WHERE `codigo` = ?', $this->connection->lastSql());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_false_when_nothing_was_removed(): void
|
|
{
|
|
$this->connection->affected = 0;
|
|
|
|
self::assertFalse($this->persister->remove(new Usuario(), 5));
|
|
}
|
|
|
|
#[Test]
|
|
public function it_notifies_the_audit_listener(): void
|
|
{
|
|
$listener = new class() implements AuditListener {
|
|
/** @var list<array{action: string, pk: int|string}> */
|
|
public array $calls = [];
|
|
|
|
public function onWrite(
|
|
string $table,
|
|
int|string $pk,
|
|
string $action,
|
|
?array $before,
|
|
array $after,
|
|
): void {
|
|
$this->calls[] = ['action' => $action, 'pk' => $pk];
|
|
}
|
|
};
|
|
|
|
$this->connection->lastInsertId = '7';
|
|
$persister = new Persister(
|
|
$this->connection,
|
|
new QueryBuilder(new MySqlDialect()),
|
|
$listener,
|
|
);
|
|
|
|
$persister->insert(new Usuario()->hydrate(['nombre' => 'Ana', 'clave' => 'abc']));
|
|
|
|
self::assertSame([['action' => 'insert', 'pk' => 7]], $listener->calls);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_invokes_the_before_insert_hook(): void
|
|
{
|
|
$model = new \Camelia\Orm\Tests\Fixture\ConHooks();
|
|
|
|
$this->persister->insert($model);
|
|
|
|
self::assertSame('insert', $model->creadoPorHook);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_invokes_the_before_update_hook(): void
|
|
{
|
|
$model = new \Camelia\Orm\Tests\Fixture\ConHooks();
|
|
$model->codigo = 3;
|
|
|
|
$this->persister->update($model);
|
|
|
|
self::assertSame('update', $model->creadoPorHook);
|
|
}
|
|
}
|