config->get('error')->fuzeworks_error_reporting = false; Logger::$logs = array(); } /** * @coversNothing */ public function testGetLogger() { $this->assertInstanceOf('FuzeWorks\Logger', new Logger); Factory::getInstance()->config->get('error')->php_error_reporting = true; $this->assertInstanceOf('FuzeWorks\Logger', new Logger); } /** * @covers ::errorHandler */ public function testErrorHandler() { Logger::errorHandler(E_ERROR, 'Example error', __FILE__, 1); $this->assertCount(1, Logger::$logs); $log = Logger::$logs[0]; $this->assertEquals('ERROR', $log['type']); $this->assertEquals('Example error', $log['message']); $this->assertEquals(__FILE__, $log['logFile']); $this->assertEquals(1, $log['logLine']); } /** * @depends testErrorHandler * @covers ::errorHandler * @covers ::getType */ public function testErrorHandlerTypes() { $types = array( E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_PARSE => 'ERROR', E_NOTICE => 'WARNING', E_CORE_ERROR => 'ERROR', E_CORE_WARNING => 'WARNING', E_COMPILE_ERROR => 'ERROR', E_COMPILE_WARNING => 'WARNING', E_USER_ERROR => 'ERROR', E_USER_WARNING => 'WARNING', E_USER_NOTICE => 'WARNING', E_USER_DEPRECATED => 'WARNING', E_STRICT => 'ERROR', E_RECOVERABLE_ERROR => 'ERROR', E_DEPRECATED => 'WARNING', 0 => 'Unknown error: 0' ); foreach ($types as $errorType => $output) { // Clear the log entries Logger::$logs = array(); // Log the error Logger::errorHandler($errorType, 'Log message'); // Fetch the error $log = Logger::$logs[0]; // Check the type $this->assertEquals($output, $log['type']); } } /** * @covers ::exceptionHandler */ public function testExceptionHandler() { // Create the exception $exception = new LoggerException("FAILURE"); // Prepare to intercept Events::addListener(function($event){ $event->setCancelled(true); $this->assertEquals('FAILURE', $event->log['message']); }, 'haltExecutionEvent'); // Log the exception Logger::exceptionHandler($exception); } /** * @covers ::log */ public function testLog() { // Log the message Logger::log('Log message', 'core_loggerTest', __FILE__, 1); // Fetch the message $log = Logger::$logs[0]; // Assert data $this->assertEquals('INFO', $log['type']); $this->assertEquals('Log message', $log['message']); $this->assertEquals(__FILE__, $log['logFile']); $this->assertEquals(1, $log['logLine']); $this->assertEquals('core_loggerTest', $log['context']); } /** * @depends testLog * @covers ::newLevel * @covers ::stopLevel * @covers ::logError * @covers ::logWarning * @covers ::logDebug * @covers ::mark */ public function testLogTypes() { $types = array( 'newLevel' => 'LEVEL_START', 'stopLevel' => 'LEVEL_STOP', 'logError' => 'ERROR', 'logWarning' => 'WARNING', 'logDebug' => 'DEBUG', 'mark' => 'BMARK' ); foreach ($types as $method => $returnValue) { // Clear the log entries Logger::$logs = array(); // Log the entry Logger::{$method}('Log message', 'core_loggerTest', __FILE__, 1); // Fetch the entry $log = Logger::$logs[0]; // Assert the entry $this->assertEquals($returnValue, $log['type']); } } /** * @covers ::enable * @covers ::disable * @covers ::isEnabled */ public function testEnableDisable() { // First enable Logger::enable(); $this->assertTrue(Logger::isEnabled()); // Then disable Logger::disable(); $this->assertFalse(Logger::isEnabled()); } public function tearDown(): void { parent::tearDown(); Logger::disable(); Logger::$logs = array(); } }