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.
120 lines
2.7 KiB
PHP
120 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Support;
|
|
|
|
use Camelia\Orm\Connection\Connection;
|
|
|
|
/**
|
|
* Records every statement instead of touching a database.
|
|
*/
|
|
final class FakeConnection implements Connection
|
|
{
|
|
/** @var list<array{sql: string, params: list<mixed>}> */
|
|
public array $executed = [];
|
|
|
|
/** @var list<array{sql: string, params: list<mixed>}> */
|
|
public array $selected = [];
|
|
|
|
/** @var list<array<string, mixed>> rows returned by select() */
|
|
public array $rows = [];
|
|
|
|
/** @var list<list<array<string, mixed>>> sequential responses; when set, consumed before $rows */
|
|
public array $rowBatches = [];
|
|
|
|
/** @var list<mixed> values returned in order by scalar() */
|
|
public array $scalars = [];
|
|
|
|
public string $lastInsertId = '1';
|
|
|
|
public int $affected = 1;
|
|
|
|
public int $beginCount = 0;
|
|
|
|
public int $commitCount = 0;
|
|
|
|
public int $rollBackCount = 0;
|
|
|
|
private bool $inTransaction = false;
|
|
|
|
public function execute(string $sql, array $params = []): int
|
|
{
|
|
$this->executed[] = ['sql' => $sql, 'params' => $params];
|
|
|
|
return $this->affected;
|
|
}
|
|
|
|
public function select(string $sql, array $params = []): array
|
|
{
|
|
$this->selected[] = ['sql' => $sql, 'params' => $params];
|
|
|
|
if ($this->rowBatches !== []) {
|
|
return array_shift($this->rowBatches);
|
|
}
|
|
|
|
return $this->rows;
|
|
}
|
|
|
|
public function scalar(string $sql, array $params = []): mixed
|
|
{
|
|
$this->selected[] = ['sql' => $sql, 'params' => $params];
|
|
|
|
return $this->scalars === [] ? null : array_shift($this->scalars);
|
|
}
|
|
|
|
public function lastInsertId(): string
|
|
{
|
|
return $this->lastInsertId;
|
|
}
|
|
|
|
public function begin(): void
|
|
{
|
|
$this->beginCount++;
|
|
$this->inTransaction = true;
|
|
}
|
|
|
|
public function commit(): void
|
|
{
|
|
$this->commitCount++;
|
|
$this->inTransaction = false;
|
|
}
|
|
|
|
public function rollBack(): void
|
|
{
|
|
$this->rollBackCount++;
|
|
$this->inTransaction = false;
|
|
}
|
|
|
|
public function inTransaction(): bool
|
|
{
|
|
return $this->inTransaction;
|
|
}
|
|
|
|
public function driver(): string
|
|
{
|
|
return 'fake';
|
|
}
|
|
|
|
public function lastSql(): string
|
|
{
|
|
if ($this->executed === []) {
|
|
return '';
|
|
}
|
|
|
|
return $this->executed[count($this->executed) - 1]['sql'];
|
|
}
|
|
|
|
/** @return list<mixed> */
|
|
public function lastParams(): array
|
|
{
|
|
$entries = $this->selected === [] ? $this->executed : $this->selected;
|
|
|
|
if ($entries === []) {
|
|
return [];
|
|
}
|
|
|
|
return $entries[count($entries) - 1]['params'];
|
|
}
|
|
}
|