Updated Logger tests

This commit is contained in:
Florian Bouillon 2019-04-26 15:27:12 +02:00
parent 6f6bfa5e7e
commit 1f687b8cd9

View File

@ -3,76 +3,65 @@
namespace Tests; namespace Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use DeltaCMS\Cache\FileCache;
use DeltaCMS\Logger; use DeltaCMS\Logger;
use Psr\Log\InvalidArgumentException; use Psr\Log\InvalidArgumentException;
use Exception; use Exception;
use DeltaCMS\Exception\FileException;
final class LoggerTest extends TestCase final class LoggerTest extends TestCase
{ {
private $file;
private $logger;
public function __construct()
{
parent::__construct();
$file = "tests/logs.log";
if (file_exists($file)) {
unlink($file);
}
$this->file = $file;
$this->logger = new Logger($file);
}
public function testFileIsCreated()
{
unlink($this->file);
new Logger($this->file);
$this->assertFileExists($this->file);
}
public function testCanLog() public function testCanLog()
{ {
$file = "tests/logs.log"; $logger = $this->logger;
if (file_exists($file)) {
unlink($file);
}
$logger = new Logger($file);
$this->assertFileExists($file);
$logger->info("test");
$this->assertStringEqualsFile(
$file,
"[Info]: " . (new \DateTime())->format("Y-m-d H:i:s") . " test\n"
);
}
public function testCanLogExceptions()
{
$file = "tests/logs.log";
if (file_exists($file)) {
unlink($file);
}
$logger = new Logger($file);
$exception = new Exception("hello phpunit"); $exception = new Exception("hello phpunit");
$logger->alert("test", array( $logger->alert("test", array(
'exception' => $exception 'exception' => $exception
)); ));
$this->assertStringEqualsFile( $this->assertStringEqualsFile(
$file, $this->file,
"[Alert]: " . (new \DateTime())->format("Y-m-d H:i:s") . " test\n[Alert]: " . (new \DateTime())->format("Y-m-d H:i:s") . " 0 " . $exception->getFile() . "[Exception] 0 " . $exception->getMessage() . "\n" "[Alert]: " . (new \DateTime())->format("Y-m-d H:i:s") . " test\n[Alert]: " . (new \DateTime())->format("Y-m-d H:i:s") . " 0 " . $exception->getFile() . "[Exception] 0 " . $exception->getMessage() . "\n"
); );
} }
public function testLevelException()
public function testFileException()
{ {
$file = "tests/logs.log"; $file = "tests/this-folder-don't & can't exist/logs.log";
if (file_exists($file)) { $this->expectException(FileException::class);
unlink($file); new Logger($file);
}
$logger = new Logger($file);
try {
$logger->log('incorrect_level', "level incorrect");
} catch (InvalidArgumentException $e) {
$this->assertEquals($e->getMessage(), "Level not supported");
return;
}
$this->fail();
} }
public function testNothingException() public function testLevelException()
{ {
$file = "tests/logs.log"; $this->expectException(InvalidArgumentException::class);
if (file_exists($file)) { $this->logger->log('incorrect_level', "level incorrect");
unlink($file);
} }
$logger = new Logger($file);
try { public function testArgumentsException()
$logger->log("info"); {
} catch (InvalidArgumentException $e) { $this->expectException(InvalidArgumentException::class);
$this->assertEquals($e->getMessage(), "Message and Exception not set"); $this->logger->log("info");
return;
}
$this->fail();
} }
} }