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.

117 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Kartierung\Analyze;
use Kartierung\EntityWithoutId;
use Kartierung\EntityWithTwoIds;
use Kartierung\FullEntity;
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: 'stringField',
fqcn: $entity,
columnName: 'stringField',
isIdField: false,
writeAccess: FieldAccess::WITHER,
readAccess: FieldAccess::PUBLIC
),
$result->fields
);
$this->assertContainsEquals(
new EntityField(
name: 'intField',
fqcn: $entity,
columnName: 'int-field',
isIdField: false,
writeAccess: FieldAccess::PUBLIC,
readAccess: FieldAccess::GETSET
),
$result->fields
);
$this->assertContainsEquals(
new EntityField(
name: 'idField',
fqcn: $entity,
columnName: 'renamed-id-field',
isIdField: true,
writeAccess: FieldAccess::GETSET,
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',
fqcn: $entity,
columnName: 'renamed-id-field',
isIdField: true,
writeAccess: FieldAccess::GETSET,
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();
}
}