Compare commits

..

No commits in common. '7783b245df93a8c1bfc38247719acb626e7a8d03' and '3a5a4c7a5c673044b8704186d9f83d7760efdc46' have entirely different histories.

@ -14,7 +14,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^13",
"phpunit/phpunit": "^12.4",
"phpstan/phpstan": "^2.1",
"smeghead/php-class-diagram": "^1.6",
"ext-pdo_sqlite": "*",

886
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -2,7 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
cacheDirectory="tests/.phpunit.cache"
resolveDependencies="true"
executionOrder="depends,defects"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"

@ -15,7 +15,6 @@ use Kartierung\Repository\ResolutionStrategy\ResolutionStrategyFactory;
use ReflectionClass;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionUnionType;
/**
@ -55,25 +54,23 @@ readonly class RepositoryAnalyzer
$methods = [];
foreach ($refMethods as $refMethod) {
$query = $this->queryString($refMethod);
list($returnType, $returnsNullable) = $this->returnType($refMethod);
$returnType = $this->returnType($refMethod);
$listType = $this->listType($refMethod);
$params = $this->parameters($refMethod);
$name = $refMethod->name;
$resolutionStrategy = $this->methodValidator->forRepositoryMethod(
repName: $refClass->name,
methodName: $name,
query: $query,
returnType: $returnType,
returnsNullable: $returnsNullable,
listType: $listType,
params: $params
$refClass->name,
$name,
$query,
$returnType,
$listType,
$params
);
$methods[$refMethod->name] = new RepositoryMethod(
query: $query,
returnType: $returnType,
returnsNullable: $returnsNullable,
returnListType: $listType,
name: $name,
parameters: $params,
@ -104,9 +101,9 @@ readonly class RepositoryAnalyzer
/**
* @param ReflectionMethod $refMethod
* @return array{class-string<object>|string, bool}
* @return class-string<object>|string
*/
private function returnType(ReflectionMethod $refMethod): array
private function returnType(ReflectionMethod $refMethod): string
{
$returnType = $refMethod->getReturnType();
@ -119,11 +116,8 @@ readonly class RepositoryAnalyzer
);
}
/** @var ReflectionNamedType $returnType */
/** @var class-string<object>|string $typeStr */
$typeStr = $returnType->getName();
return [$typeStr, $returnType->allowsNull()];
/** @var class-string<object>|string */
return $returnType->__toString();
}
/**

@ -6,7 +6,6 @@ namespace Kartierung\Analyze\Result;
use Kartierung\Repository\ResolutionStrategy\ResolutionStrategy;
use Kartierung\Sql\LazyQuery;
use phpDocumentor\Reflection\Types\Boolean;
readonly class RepositoryMethod
{
@ -17,13 +16,17 @@ readonly class RepositoryMethod
public function __construct(
public ?string $query,
public string $returnType,
public bool $returnsNullable,
public ?string $returnListType,
public string $name,
public array $parameters,
public ResolutionStrategy $resolutionStrategy
) {}
public function returnsNullable(): bool
{
return str_starts_with($this->returnType, '?');
}
/**
* @param array<string, mixed> $parameters
*/

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Kartierung\Repository;
use Kartierung\Analyze\RepositoryAnalyzer;
use Kartierung\Analyze\Result\RepositoryMethod;
use Kartierung\Analyze\Result\RepositoryResult;
use Kartierung\Repository\ResolutionStrategy\ResolutionStrategyFactory;
use PDO;
@ -27,10 +26,10 @@ readonly class Repository
$repoResult = (new RepositoryAnalyzer($resolutionStrategyFactory))->analyze($clazz);
/**
* @var R $proxy
* @var R of object
* @phpstan-ignore varTag.nativeType
*/
$proxy = new class($repoResult, $this->pdo) {
return new class($repoResult, $this->pdo) {
public function __construct(
private readonly RepositoryResult $repoResult,
private readonly PDO $pdo
@ -44,30 +43,17 @@ readonly class Repository
public function __call(string $methodName, array $arguments): mixed
{
$repoMethod = $this->repoResult->methods[$methodName];
$queryArgs = $this->callArgsToQueryArgs($repoMethod, $arguments);
$query = $repoMethod->createQuery($queryArgs);
return $query->execute($this->pdo);
$argCount = 0;
$callParams = [];
foreach ($repoMethod->parameters as $parameter) {
$callParams[$parameter->name] = $arguments[$argCount++];
}
/**
* @param RepositoryMethod $method
* @param list<mixed> $arguments
* @return array<string, mixed>
*/
private function callArgsToQueryArgs(RepositoryMethod $method, array $arguments): array
{
$queryArgs = [];
foreach ($method->parameters as $i => $parameter) {
$queryArgs[$parameter->name] = $arguments[$i];
}
$query = $repoMethod->createQuery($callParams);
return $queryArgs;
return $query->execute($this->pdo);
}
};
return $proxy;
}
}

@ -4,51 +4,17 @@ declare(strict_types=1);
namespace Kartierung\Repository\ResolutionStrategy;
use Kartierung\Analyze\EntityAnalyzer;
use Kartierung\Analyze\Result\EntityField;
use Kartierung\Analyze\Result\RepositoryMethod;
use Kartierung\Sql\DataToEntityMapper;
use Kartierung\Sql\LazyQuery;
use PDO;
use PDOStatement;
readonly class FindById implements ResolutionStrategy
class FindById implements ResolutionStrategy
{
public function createQuery(RepositoryMethod $method, array $parameters): LazyQuery
{
return new LazyQuery(
static function (PDO $pdo) use ($method, $parameters) {
/** @var class-string<object> $type */
$type = $method->returnType;
$entityResult = (new EntityAnalyzer($type))->analyze();
$mapper = new DataToEntityMapper($entityResult);
function (PDO $pdo) {
$fields = implode(
', ',
array_map(
static fn(EntityField $e) => $e->columnName, $entityResult->fields
)
);
$idField = $entityResult->idField->name;
$sql = "
SELECT $fields
FROM {$entityResult->tableName}
WHERE $idField = ?
";
/** @var PDOStatement $stmt */
$stmt = $pdo->prepare($sql);
$stmt->execute([array_first($parameters)]);
/** @var array<string, mixed>|false $row */
$row = $stmt->fetch(mode: PDO::FETCH_ASSOC);
if($row) {
return $mapper->toEntity($row);
}
return null;
}
);
}

@ -17,12 +17,12 @@ class ResolutionStrategyFactory
string $methodName,
?string $query,
string $returnType,
bool $returnsNullable,
?string $listType,
array $params
): ResolutionStrategy
{
$oneParam = count($params) === 1;
$returnsNullable = str_starts_with($returnType, '?');
return match (true) {
$query !== null => new UserDefinedQuery(),
@ -40,7 +40,7 @@ class ResolutionStrategyFactory
$methodName === 'findById' && $oneParam && $returnsNullable => new FindById(),
$methodName === 'findById' => throw new InvalidRepository(
"fnyById functions expect exactly one parameter and nullable return type. " .
"fnyById functions expect exactly one parameter and nullable return type." .
"Invalid function found in $repName"
),

@ -130,7 +130,7 @@ class RepositoryAnalyzerTest extends TestCase
// Then
$method = $result->methods['findById'];
$this->assertEquals('Kartierung\FullEntity', $method->returnType);
$this->assertTrue($method->returnsNullable);
$this->assertEquals('?Kartierung\FullEntity', $method->returnType);
$this->assertTrue($method->returnsNullable());
}
}

@ -8,13 +8,17 @@ use Kartierung\Analyze\Result\RepositoryMethod;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
class FindAllTest extends SqliteBaseCase
class FindAllTest extends TestCase
{
private \PDO $pdo;
protected function setUp(): void
{
parent::setUp();
$tmpfile = tempnam(sys_get_temp_dir(), 'kartierung.sqlite');
$this->pdo = new \PDO("sqlite:/$tmpfile");
$this->getPdo()->exec(
$this->pdo->exec(
"
CREATE TABLE SimpleEntity (
stringcol VARCHAR(250),
@ -31,19 +35,20 @@ class FindAllTest extends SqliteBaseCase
public function testSelectsData(): void
{
// Given
$method = new RepositoryMethod(
$query = (new FindAll())->createQuery(
method: new RepositoryMethod(
query: '',
returnType: '',
returnsNullable: true,
returnListType: SimpleEntity::class,
name: '',
parameters: [],
resolutionStrategy: new FindAll()
),
parameters: []
);
$query = $method->createQuery([]);
// When
$result = $query->execute($this->getPdo());
$result = $query->execute($this->pdo);
// Then
$this->assertNotEmpty($result);

@ -1,101 +0,0 @@
<?php
declare(strict_types=1);
namespace Kartierung\Repository\ResolutionStrategy;
use Kartierung\Analyze\Result\RepositoryMethod;
use PHPUnit\Framework\Attributes\Group;
class FindByIdTest extends SqliteBaseCase
{
protected function setUp(): void
{
parent::setUp();
$this->getPdo()->exec(
"
CREATE TABLE SimpleEntity (
stringcol VARCHAR(250),
intcol INT
);
INSERT INTO SimpleEntity(stringcol, intcol)
VALUES ('dings', 1),
('bumms', 2),
('foobar', 3);
"
);
}
#[Group('integration')]
public function testSelectsFirstEntry(): void
{
// Given
$method = new RepositoryMethod(
query: '',
returnType: SimpleEntity::class,
returnsNullable: true,
returnListType: '',
name: '',
parameters: [],
resolutionStrategy: new FindById()
);
$query = $method->createQuery([1]);
// When
$result = $query->execute($this->getPdo());
// Then
$this->assertEquals(
new SimpleEntity(1, 'dings'),
$result
);
}
#[Group('integration')]
public function testSelectsThirdEntry(): void
{
// Given
$method = new RepositoryMethod(
query: '',
returnType: SimpleEntity::class,
returnsNullable: true,
returnListType: '',
name: '',
parameters: [],
resolutionStrategy: new FindById()
);
$query = $method->createQuery([3]);
// When
$result = $query->execute($this->getPdo());
// Then
$this->assertEquals(
new SimpleEntity(3, 'foobar'),
$result
);
}
#[Group('integration')]
public function testSelectsNothingMatching(): void
{
// Given
$method = new RepositoryMethod(
query: '',
returnType: SimpleEntity::class,
returnsNullable: true,
returnListType: '',
name: '',
parameters: [],
resolutionStrategy: new FindById()
);
$query = $method->createQuery([4]);
// When
$result = $query->execute($this->getPdo());
// Then
$this->assertNull($result);
}
}

@ -13,11 +13,11 @@ class ResolutionStrategyFactoryTest extends TestCase
public static function validFunctionsProvider(): array
{
return [
['R', 'save', null, '', false, null, [1], SaveEntity::class],
['R', 'delete', null, 'int', false, null, [1], DeleteEntity::class],
['R', 'findById', null, 'int', true, null, [1], FindById::class],
['R', 'findAll', null, 'array', false, FullEntity::class, [], FindAll::class],
['R', 'something', 'SELECT', 'int', false, null, [], UserDefinedQuery::class]
['R', 'save', null, '', null, [1], SaveEntity::class],
['R', 'delete', null, 'int', null, [1], DeleteEntity::class],
['R', 'findById', null, '?int', null, [1], FindById::class],
['R', 'findAll', null, 'array', FullEntity::class, [], FindAll::class],
['R', 'something', 'SELECT', 'int', null, [], UserDefinedQuery::class]
];
}
@ -27,7 +27,6 @@ class ResolutionStrategyFactoryTest extends TestCase
string $name,
?string $query,
string $returnType,
bool $returnsNullable,
?string $listType,
array $params,
string $strategyClass
@ -35,13 +34,12 @@ class ResolutionStrategyFactoryTest extends TestCase
{
// When
$function = (new ResolutionStrategyFactory())->forRepositoryMethod(
repName: $repName,
methodName: $name,
query: $query,
returnType: $returnType,
returnsNullable: $returnsNullable,
listType: $listType,
params: $params
$repName,
$name,
$query,
$returnType,
$listType,
$params
);
// Then
@ -71,13 +69,12 @@ class ResolutionStrategyFactoryTest extends TestCase
{
// When
$function = (new ResolutionStrategyFactory())->forRepositoryMethod(
repName: $repName,
methodName: $name,
query: $query,
returnType: $returnType,
returnsNullable: false,
listType: $listType,
params: $params
$repName,
$name,
$query,
$returnType,
$listType,
$params
);
// Then

@ -13,4 +13,12 @@ class SimpleEntity
public int $intcol,
public string $stringcol
) {}
public function withStringCol(string $value): self
{
$new = new self();
$new->stringcol = $value;
return $new;
}
}

@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
namespace Kartierung\Repository\ResolutionStrategy;
use PDO;
use PHPUnit\Framework\TestCase;
abstract class SqliteBaseCase extends TestCase
{
private PDO $pdo;
protected function setUp(): void
{
parent::setUp();
$tmpfile = tempnam(sys_get_temp_dir(), 'kartierung.sqlite');
$this->pdo = new \PDO("sqlite:/$tmpfile");
}
protected function getPdo(): PDO
{
return $this->pdo;
}
}
Loading…
Cancel
Save