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.
162 lines
4.7 KiB
PHP
162 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit\Query;
|
|
|
|
use Camelia\Orm\Exception\CriteriaException;
|
|
use Camelia\Orm\Metadata\ClassMetadata;
|
|
use Camelia\Orm\Metadata\MetadataFactory;
|
|
use Camelia\Orm\Query\MySqlDialect;
|
|
use Camelia\Orm\Query\QueryBuilder;
|
|
use Camelia\Orm\Tests\Fixture\Factura;
|
|
use Camelia\Orm\Tests\Fixture\Usuario;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class QueryBuilderTest extends TestCase
|
|
{
|
|
private QueryBuilder $builder;
|
|
|
|
private ClassMetadata $usuarios;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->builder = new QueryBuilder(new MySqlDialect());
|
|
$this->usuarios = new MetadataFactory()->forClass(Usuario::class);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_selects_every_non_relation_column(): void
|
|
{
|
|
$compiled = $this->builder->select($this->usuarios, []);
|
|
|
|
self::assertSame(
|
|
'SELECT `codigo`, `nombre`, `correo`, `password_hash`, `activo`, `apodo`, `clave`'
|
|
. ' FROM `usuarios`',
|
|
$compiled['sql'],
|
|
);
|
|
self::assertSame([], $compiled['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_excludes_relations_from_the_select_list(): void
|
|
{
|
|
$metadata = new MetadataFactory()->forClass(Factura::class);
|
|
$compiled = $this->builder->select($metadata, []);
|
|
|
|
self::assertStringNotContainsString('`usuario`', $compiled['sql']);
|
|
self::assertStringNotContainsString('`detalles`', $compiled['sql']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_adds_where_order_and_limit(): void
|
|
{
|
|
$compiled = $this->builder->select(
|
|
$this->usuarios,
|
|
['activo' => true],
|
|
[['nombre', 'ASC']],
|
|
10,
|
|
20,
|
|
);
|
|
|
|
self::assertSame(
|
|
'SELECT `codigo`, `nombre`, `correo`, `password_hash`, `activo`, `apodo`, `clave`'
|
|
. ' FROM `usuarios` WHERE `activo` = ?'
|
|
. ' ORDER BY `usuarios`.`nombre` ASC LIMIT 10 OFFSET 20',
|
|
$compiled['sql'],
|
|
);
|
|
self::assertSame([true], $compiled['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_rejects_an_invalid_order_direction(): void
|
|
{
|
|
$this->expectException(CriteriaException::class);
|
|
|
|
/** @phpstan-ignore-next-line intentionally invalid direction to prove runtime validation */
|
|
$this->builder->select($this->usuarios, [], [['nombre', 'SIDEWAYS']]);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_restricts_selected_columns_when_asked(): void
|
|
{
|
|
$compiled = $this->builder->select(
|
|
$this->usuarios,
|
|
[],
|
|
[],
|
|
null,
|
|
null,
|
|
['codigo', 'nombre'],
|
|
);
|
|
|
|
self::assertStringContainsString('SELECT `codigo`, `nombre` FROM', $compiled['sql']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_count(): void
|
|
{
|
|
$compiled = $this->builder->count($this->usuarios, ['activo' => 1]);
|
|
|
|
self::assertSame(
|
|
'SELECT COUNT(*) AS aggregate FROM `usuarios` WHERE `activo` = ?',
|
|
$compiled['sql'],
|
|
);
|
|
self::assertSame([1], $compiled['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_aggregates(): void
|
|
{
|
|
$compiled = $this->builder->aggregate($this->usuarios, 'codigo', 'MAX', []);
|
|
|
|
self::assertSame('SELECT MAX(`codigo`) AS aggregate FROM `usuarios`', $compiled['sql']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_insert(): void
|
|
{
|
|
$compiled = $this->builder->insert($this->usuarios, ['nombre' => 'Ana', 'activo' => true]);
|
|
|
|
self::assertSame(
|
|
'INSERT INTO `usuarios` (`nombre`, `activo`) VALUES (?, ?)',
|
|
$compiled['sql'],
|
|
);
|
|
self::assertSame(['Ana', true], $compiled['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_update(): void
|
|
{
|
|
$compiled = $this->builder->update($this->usuarios, ['nombre' => 'Ana'], 7);
|
|
|
|
self::assertSame('UPDATE `usuarios` SET `nombre` = ? WHERE `codigo` = ?', $compiled['sql']);
|
|
self::assertSame(['Ana', 7], $compiled['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_compiles_delete(): void
|
|
{
|
|
$compiled = $this->builder->delete($this->usuarios, 7);
|
|
|
|
self::assertSame('DELETE FROM `usuarios` WHERE `codigo` = ?', $compiled['sql']);
|
|
self::assertSame([7], $compiled['params']);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refuses_an_empty_insert(): void
|
|
{
|
|
$this->expectException(CriteriaException::class);
|
|
|
|
$this->builder->insert($this->usuarios, []);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_refuses_an_empty_update(): void
|
|
{
|
|
$this->expectException(CriteriaException::class);
|
|
|
|
$this->builder->update($this->usuarios, [], 1);
|
|
}
|
|
}
|