web-base/test/DatabaseEntity.test.php

193 lines
5.9 KiB
PHP
Raw Normal View History

2022-06-17 23:29:24 +02:00
<?php
namespace {
2022-11-18 18:06:46 +01:00
use Core\API\Parameter\Parameter;
use Core\Driver\SQL\Query\CreateTable;
use Core\Driver\SQL\SQL;
use Core\Objects\Context;
2022-11-20 17:13:53 +01:00
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
use Core\Objects\DatabaseEntity\Controller\DatabaseEntityHandler;
2022-11-18 18:06:46 +01:00
use Core\Objects\DatabaseEntity\User;
2022-06-20 19:52:31 +02:00
2022-06-17 23:29:24 +02:00
class DatabaseEntityTest extends \PHPUnit\Framework\TestCase {
2022-06-20 19:52:31 +02:00
static User $USER;
static SQL $SQL;
static Context $CONTEXT;
static DatabaseEntityHandler $HANDLER;
static DatabaseEntityHandler $HANDLER_RECURSIVE;
2022-06-17 23:29:24 +02:00
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
2023-01-15 00:32:17 +01:00
self::$CONTEXT = Context::instance();
2022-06-20 19:52:31 +02:00
if (!self::$CONTEXT->initSQL()) {
throw new Exception("Could not establish database connection");
}
self::$SQL = self::$CONTEXT->getSQL();
2022-06-17 23:29:24 +02:00
self::$HANDLER = TestEntity::getHandler(self::$SQL);
self::$HANDLER_RECURSIVE = TestEntityRecursive::getHandler(self::$SQL);
2022-06-17 23:36:04 +02:00
self::$HANDLER->getLogger()->unitTestMode();
self::$HANDLER_RECURSIVE->getLogger()->unitTestMode();
2022-06-17 23:29:24 +02:00
}
public function testCreateTable() {
2022-11-20 17:13:53 +01:00
$query = self::$HANDLER->getTableQuery(self::$CONTEXT->getSQL());
$this->assertInstanceOf(CreateTable::class, $query);
$this->assertTrue($query->execute());
2022-06-17 23:29:24 +02:00
}
public function testInsertEntity() {
$entity = new TestEntity();
$entity->a = 1;
$entity->b = "test";
$entity->c = true;
$entity->d = 1.23;
$entity->e = new DateTime();
$entity->f = null;
// insert
$this->assertTrue($entity->save(self::$SQL));
$entityId = $entity->getId();
$this->assertNotNull($entityId);
// fetch
$entity2 = TestEntity::find(self::$SQL, $entityId);
$this->assertNotNull($entity2);
$this->assertEquals($entity2->a, $entity->a);
$this->assertEquals($entity2->b, $entity->b);
$this->assertEquals($entity2->c, $entity->c);
$this->assertEquals($entity2->d, $entity->d);
$this->assertNotNull($entity2->e);
$this->assertEquals(
2022-11-18 18:06:46 +01:00
$entity2->e->format(Parameter::DATE_TIME_FORMAT),
$entity->e->format(Parameter::DATE_TIME_FORMAT)
2022-06-17 23:29:24 +02:00
);
$this->assertNull($entity2->f);
// update
$entity2->a = 100;
$this->assertTrue($entity2->save(self::$SQL));
$this->assertEquals($entity2->getId(), $entityId);
// re-fetch
$this->assertEquals($entity2->a, TestEntity::find(self::$SQL, $entityId)->a);
// check table contents
$allEntities = TestEntity::findAll(self::$SQL);
$this->assertIsArray($allEntities);
$this->assertCount(1, $allEntities);
2022-06-20 19:52:31 +02:00
$this->assertTrue(array_key_exists($entityId, $allEntities));
$this->assertEquals($entityId, $allEntities[$entityId]->getId());
2022-06-17 23:29:24 +02:00
// delete
$this->assertTrue($entity->delete(self::$SQL));
$this->assertNull($entity->getId());
// check table contents
$allEntities = TestEntity::findAll(self::$SQL);
$this->assertIsArray($allEntities);
$this->assertCount(0, $allEntities);
}
public function testInsertFail() {
$entity = new TestEntity();
$this->assertFalse($entity->save(self::$SQL));
2022-06-17 23:36:04 +02:00
$this->assertTrue(startsWith(
self::$HANDLER->getLogger()->getLastMessage(),
"Cannot insert entity: property 'a' was not initialized yet."
));
$this->assertEquals("error", self::$HANDLER->getLogger()->getLastLevel());
2022-06-17 23:29:24 +02:00
}
public function testDropTable() {
$this->assertTrue(self::$SQL->drop(self::$HANDLER->getTableName())->execute());
$this->assertTrue(self::$SQL->drop(self::$HANDLER_RECURSIVE->getTableName())->execute());
2022-06-17 23:29:24 +02:00
}
public function testTableNames() {
$sql = self::$SQL;
$this->assertEquals("TestEntity", TestEntity::getHandler($sql, null, true)->getTableName());
$this->assertEquals("TestEntityInherit", TestEntityInherit::getHandler($sql, null, true)->getTableName());
$this->assertEquals("TestEntityInherit", OverrideNameSpace\TestEntityInherit::getHandler($sql, null, true)->getTableName());
}
public function testCreateQueries() {
$queries = [];
$entities = [
TestEntity::class,
TestEntityInherit::class,
OverrideNameSpace\TestEntityInherit::class,
TestEntityRecursive::class,
];
\Core\Configuration\CreateDatabase::createEntityQueries(self::$SQL, $entities, $queries);
$this->assertCount(3, $queries);
$tables = [];
foreach ($queries as $query) {
$this->assertInstanceOf(CreateTable::class, $query);
$tables[] = $query->getTableName();
}
$this->assertEquals(["TestEntity", "TestEntityInherit", "TestEntityRecursive"], $tables);
}
public function testRecursive() {
$query = self::$HANDLER_RECURSIVE->getTableQuery(self::$CONTEXT->getSQL());
$this->assertInstanceOf(CreateTable::class, $query);
$this->assertTrue($query->execute());
// ID: 1
$entityA = new TestEntityRecursive();
$entityA->recursive = null;
// ID: 2
$entityB = new TestEntityRecursive();
$entityB->recursive = $entityA;
// ID: 3
$entityC = new TestEntityRecursive();
$entityC->recursive = $entityB;
$this->assertTrue($entityA->save(self::$SQL));
$this->assertTrue($entityB->save(self::$SQL));
$this->assertTrue($entityC->save(self::$SQL));
$fetchedEntity = TestEntityRecursive::find(self::$SQL, 3, true, true);
$this->assertInstanceOf(TestEntityRecursive::class, $fetchedEntity);
$this->assertEquals(3, $fetchedEntity->getId());
$this->assertEquals(2, $fetchedEntity->recursive->getId());
$this->assertEquals(1, $fetchedEntity->recursive->recursive->getId());
$this->assertNull($fetchedEntity->recursive->recursive->recursive);
}
2022-06-17 23:29:24 +02:00
}
2022-11-18 18:06:46 +01:00
class TestEntity extends DatabaseEntity {
2022-06-17 23:29:24 +02:00
public int $a;
public string $b;
public bool $c;
public float $d;
public \DateTime $e;
public ?int $f;
}
class TestEntityInherit extends DatabaseEntity {
public TestEntity $rel;
}
class TestEntityRecursive extends DatabaseEntity {
public ?TestEntityRecursive $recursive;
}
}
namespace OverrideNameSpace {
class TestEntityInherit extends \TestEntityInherit {
public int $new;
}
}