diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 61b17f6..67017f8 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -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 { - public function testCanLog() + private $file; + private $logger; + + public function __construct() { + parent::__construct(); $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" - ); + $this->file = $file; + $this->logger = new Logger($file); } - public function testCanLogExceptions() + public function testFileIsCreated() { - $file = "tests/logs.log"; - if (file_exists($file)) { - unlink($file); - } - $logger = new Logger($file); + unlink($this->file); + new Logger($this->file); + $this->assertFileExists($this->file); + } + + public function testCanLog() + { + $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); - } - $logger = new Logger($file); - try { - $logger->log("info"); - } catch (InvalidArgumentException $e) { - $this->assertEquals($e->getMessage(), "Message and Exception not set"); - return; - } - $this->fail(); + $this->expectException(InvalidArgumentException::class); + $this->logger->log('incorrect_level', "level incorrect"); + } + + public function testArgumentsException() + { + $this->expectException(InvalidArgumentException::class); + $this->logger->log("info"); } }