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;
use PHPUnit\Framework\TestCase;
use DeltaCMS\Cache\FileCache;
use DeltaCMS\Logger;
use Psr\Log\InvalidArgumentException;
use Exception;
use DeltaCMS\Exception\FileException;
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()
{
$file = "tests/logs.log";
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);
$logger = $this->logger;
$exception = new Exception("hello phpunit");
$logger->alert("test", array(
'exception' => $exception
));
$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"
);
}
public function testLevelException()
public function testFileException()
{
$file = "tests/logs.log";
if (file_exists($file)) {
unlink($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();
$file = "tests/this-folder-don't & can't exist/logs.log";
$this->expectException(FileException::class);
new Logger($file);
}
public function testNothingException()
public function testLevelException()
{
$file = "tests/logs.log";
if (file_exists($file)) {
unlink($file);
$this->expectException(InvalidArgumentException::class);
$this->logger->log('incorrect_level', "level incorrect");
}
$logger = new Logger($file);
try {
$logger->log("info");
} catch (InvalidArgumentException $e) {
$this->assertEquals($e->getMessage(), "Message and Exception not set");
return;
}
$this->fail();
public function testArgumentsException()
{
$this->expectException(InvalidArgumentException::class);
$this->logger->log("info");
}
}