You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.8 KiB
PHP
137 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Analyze;
|
|
|
|
use Kartierung\Analyze\InvalidRepository;
|
|
use Kartierung\Analyze\RepositoryAnalyzer;
|
|
use Kartierung\Analyze\RepositoryMethodParameter;
|
|
use Kartierung\Analyze\Validate\ResolutionStrategyFactory;
|
|
use Kartierung\FullEntity;
|
|
use Kartierung\InvalidListResultOfRepository;
|
|
use Kartierung\Repository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class RepositoryAnalyzerTest extends TestCase
|
|
{
|
|
public function testQueryAttributeIsRead(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$this->assertEquals('SELECT something', $result->methods['fetchWithQuery']->query);
|
|
}
|
|
|
|
public function testEntityReturnTypeIsAnalyzed(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$this->assertEquals(FullEntity::class, $result->methods['fetchWithQuery']->returnType);
|
|
}
|
|
|
|
public function testArrayReturnTypeIsRead(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$this->assertEquals('array', $result->methods['fetchArray']->returnType);
|
|
}
|
|
|
|
public function testVoidReturnTypeIsRead(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$this->assertEquals('void', $result->methods['insert']->returnType);
|
|
}
|
|
|
|
public function testListResultOfIsRead(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$this->assertEquals(FullEntity::class, $result->methods['fetchArray']->returnListType);
|
|
}
|
|
|
|
public function testInvalidListResultOfIsReported(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// Then
|
|
$this->expectException(InvalidRepository::class);
|
|
|
|
// When
|
|
$analyzer->analyze(InvalidListResultOfRepository::class);
|
|
}
|
|
|
|
public function testUnionReturnTypeIsReported(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// Then
|
|
$this->expectException(InvalidRepository::class);
|
|
|
|
// When
|
|
$analyzer->analyze(InvalidRepository::class);
|
|
}
|
|
|
|
public function testParametersAreAnalyzed(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$method = $result->methods['doSomethingWithParameters'];
|
|
/** @var array<RepositoryMethodParameter> $parameters */
|
|
$parameters = $method->parameters;
|
|
|
|
$this->assertCount(2, $parameters);
|
|
$this->assertEquals('stringParam', $parameters[0]->name);
|
|
$this->assertEquals('string', $parameters[0]->type);
|
|
|
|
$this->assertEquals('intParam', $parameters[1]->name);
|
|
$this->assertEquals('int', $parameters[1]->type);
|
|
}
|
|
|
|
public function testNullableReturnType(): void
|
|
{
|
|
// Given
|
|
$analyzer = new RepositoryAnalyzer(new ResolutionStrategyFactory());
|
|
|
|
// When
|
|
$result = $analyzer->analyze(Repository::class);
|
|
|
|
// Then
|
|
$method = $result->methods['findById'];
|
|
$this->assertEquals('?Kartierung\FullEntity', $method->returnType);
|
|
$this->assertTrue($method->returnsNullable());
|
|
}
|
|
}
|