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.

185 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Kartierung\Analyze;
use Kartierung\Analyze\Exception\InvalidEntity;
use Kartierung\Analyze\Result\EntityField;
use Kartierung\Analyze\Result\EntityResult;
use Kartierung\Analyze\Result\FieldAccess;
use Kartierung\Analyze\Result\Type\ArrayType;
use Kartierung\Analyze\Result\Type\PrimitiveType;
use Kartierung\FullEntity;
use Kartierung\PropertyPropagatedEntity;
use PHPUnit\Framework\TestCase;
class EntityAnalyzerTest extends TestCase
{
public function testTableNameIsAnalyzed(): void
{
// Given
$entity = FullEntity::class;
// When
$result = (new EntityAnalyzer(FullEntity::class))->analyze();
// Then
$this->assertEquals('simple-table', $result->tableName);
$this->assertEquals(FullEntity::class, $result->classFqcn);
}
public function testFieldsAreAnalyzed(): void
{
// Given
$entity = FullEntity::class;
// When
$result = (new EntityAnalyzer($entity))->analyze();
// Then
$this->assertContainsEquals(
new EntityField(
name: 'intField',
type: PrimitiveType::INTEGER,
columnName: 'int-field',
isIdField: false,
readAccess: FieldAccess::GETTER
),
$result->fields
);
$this->assertContainsEquals(
new EntityField(
name: 'idField',
type: PrimitiveType::INTEGER,
columnName: 'renamed-id-field',
isIdField: true,
readAccess: FieldAccess::PUBLIC
),
$result->fields
);
}
public function testIdFieldIsFound(): void
{
// Given
$entity = FullEntity::class;
// When
$result = (new EntityAnalyzer($entity))->analyze();
// Then
$this->assertEquals(
new EntityField(
name: 'idField',
type: PrimitiveType::INTEGER,
columnName: 'renamed-id-field',
isIdField: true,
readAccess: FieldAccess::PUBLIC
),
$result->idField
);
}
public function testNoIdIsInvalid(): void
{
// Given
$entity = EntityWithoutId::class;
// Then
$this->expectException(InvalidEntity::class);
// When
(new EntityAnalyzer($entity))->analyze();
}
public function testMultipleIdsIsInvalid(): void
{
// Given
$entity = EntityWithTwoIds::class;
// Then
$this->expectException(InvalidEntity::class);
// When
(new EntityAnalyzer($entity))->analyze();
}
public function testTableAttributeIsOptional(): void
{
// Given
$entity = EntityWithoutTableAttribute::class;
// When
$result = (new EntityAnalyzer($entity))->analyze();
// Then
$this->assertEquals("EntityWithoutTableAttribute", $result->tableName);
}
public function testArrayOfChildEntities(): void
{
// Given
$entity = EntityWithListOfFullEntities::class;
// When
$result = (new EntityAnalyzer($entity))->analyze();
// Then
$this->assertEquals(
new EntityField(
name: 'id',
type: PrimitiveType::INTEGER,
columnName: 'id',
isIdField: true,
readAccess: FieldAccess::PUBLIC
),
$result->fields[0]
);
$this->assertEquals(
new EntityField(
name: 'children',
type: new ArrayType(
new EntityResult(
classFqcn: IdOnlyEntity::class,
tableName: 'IdOnlyEntity',
fields: [
new EntityField(
name: 'id',
type: PrimitiveType::INTEGER,
columnName: 'id',
isIdField: true,
readAccess: FieldAccess::PUBLIC
)
],
idField: new EntityField(
name: 'id',
type: PrimitiveType::INTEGER,
columnName: 'id',
isIdField: true,
readAccess: FieldAccess::PUBLIC
)
)
),
columnName: 'children',
isIdField: false,
readAccess: FieldAccess::PUBLIC
),
$result->fields[1]
);
}
public function testInfiniteRecursionIsDetected(): void
{
// Given
$entity = EntityWithInfiniteRecursion::class;
// Then
$this->expectException(InvalidEntity::class);
$this->expectExceptionMessageMatches('/.*recursive.*/');
// When
(new EntityAnalyzer($entity))->analyze();
}
}