Logger Unit tests

This commit is contained in:
Roman 2022-06-17 23:36:04 +02:00
parent 9ecbb2775d
commit b549af3166
2 changed files with 36 additions and 1 deletions

@ -20,9 +20,17 @@ class Logger {
private ?SQL $sql; private ?SQL $sql;
private string $module; private string $module;
// unit tests
private bool $unitTestMode;
private ?string $lastMessage;
private ?string $lastLevel;
public function __construct(string $module = "Unknown", ?SQL $sql = null) { public function __construct(string $module = "Unknown", ?SQL $sql = null) {
$this->module = $module; $this->module = $module;
$this->sql = $sql; $this->sql = $sql;
$this->unitTestMode = false;
$this->lastMessage = null;
$this->lastLevel = null;
} }
protected function getStackTrace(int $pop = 2): string { protected function getStackTrace(int $pop = 2): string {
@ -42,6 +50,12 @@ class Logger {
$message .= "\n" . $this->getStackTrace(); $message .= "\n" . $this->getStackTrace();
} }
if ($this->unitTestMode) {
$this->lastMessage = $message;
$this->lastLevel = $severity;
return;
}
if ($this->sql !== null && $this->sql->isConnected()) { if ($this->sql !== null && $this->sql->isConnected()) {
$success = $this->sql->insert("SystemLog", ["module", "message", "severity"]) $success = $this->sql->insert("SystemLog", ["module", "message", "severity"])
->addRow($this->module, $message, $severity) ->addRow($this->module, $message, $severity)
@ -91,4 +105,20 @@ class Logger {
return self::$INSTANCE; return self::$INSTANCE;
} }
/**
* Calling this method will prevent the logger from persisting log messages (writing to database/file),
* and allow to access the last logged message via #getLastMessage() and #getLastLevel()
*/
public function unitTestMode() {
$this->unitTestMode = true;
}
public function getLastMessage(): ?string {
return $this->lastMessage;
}
public function getLastLevel(): ?string {
return $this->lastLevel;
}
} }

@ -1,6 +1,5 @@
<?php <?php
// TODO: disable logging for tests
class DatabaseEntityTest extends \PHPUnit\Framework\TestCase { class DatabaseEntityTest extends \PHPUnit\Framework\TestCase {
static \Objects\User $USER; static \Objects\User $USER;
@ -12,6 +11,7 @@ class DatabaseEntityTest extends \PHPUnit\Framework\TestCase {
self::$USER = new Objects\User(new \Configuration\Configuration()); self::$USER = new Objects\User(new \Configuration\Configuration());
self::$SQL = self::$USER->getSQL(); self::$SQL = self::$USER->getSQL();
self::$HANDLER = TestEntity::getHandler(self::$SQL); self::$HANDLER = TestEntity::getHandler(self::$SQL);
self::$HANDLER->getLogger()->unitTestMode();
} }
public function testCreateTable() { public function testCreateTable() {
@ -75,6 +75,11 @@ class DatabaseEntityTest extends \PHPUnit\Framework\TestCase {
public function testInsertFail() { public function testInsertFail() {
$entity = new TestEntity(); $entity = new TestEntity();
$this->assertFalse($entity->save(self::$SQL)); $this->assertFalse($entity->save(self::$SQL));
$this->assertTrue(startsWith(
self::$HANDLER->getLogger()->getLastMessage(),
"Cannot insert entity: property 'a' was not initialized yet."
));
$this->assertEquals("error", self::$HANDLER->getLogger()->getLastLevel());
} }
public function testDropTable() { public function testDropTable() {