mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Refactor Log module tests
This commit is contained in:
parent
7118d89070
commit
59f1e32259
@ -5,7 +5,7 @@
|
||||
*
|
||||
* Extends ChromeLoggerHandler, exposing some inner workings
|
||||
*/
|
||||
class MockChromeHandler extends ChromeLoggerHandler
|
||||
class MockChromeHandler extends \CodeIgniter\Log\Handlers\ChromeLoggerHandler
|
||||
{
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
@ -1,11 +1,12 @@
|
||||
<?php namespace Tests\Support\Log\Handlers;
|
||||
|
||||
|
||||
/**
|
||||
* Class MockHandler
|
||||
*
|
||||
* Extends FileHandler, exposing some inner workings
|
||||
*/
|
||||
class MockFileHandler extends FileHandler
|
||||
class MockFileHandler extends \CodeIgniter\Log\Handlers\FileHandler
|
||||
{
|
||||
/**
|
||||
* Where would the log be written?
|
||||
|
@ -3,9 +3,7 @@
|
||||
use CodeIgniter\Test\FeatureTestCase;
|
||||
|
||||
/**
|
||||
* This relies on a database connection
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @group DatabaseLive
|
||||
*/
|
||||
class HomeTest extends FeatureTestCase
|
||||
{
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php namespace CodeIgniter\Log\Handlers;
|
||||
|
||||
use Tests\Support\Config\MockLogger as LoggerConfig;
|
||||
use Tests\Support\Log\Handlers\MockFileHandler as MockFileHandler;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use org\bovigo\vfs\vfsStreamDirectory;
|
||||
use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
|
||||
@ -18,9 +19,9 @@ class FileHandlerTest extends \CIUnitTestCase
|
||||
public function testHandle()
|
||||
{
|
||||
$config = new LoggerConfig();
|
||||
$config->handlers['CodeIgniter\Log\Handlers\TestHandler']['handles'] = ['critical'];
|
||||
$config->handlers['Tests\Support\Log\Handlers\TestHandler']['handles'] = ['critical'];
|
||||
|
||||
$logger = new FileHandler($config->handlers['CodeIgniter\Log\Handlers\TestHandler']);
|
||||
$logger = new FileHandler($config->handlers['Tests\Support\Log\Handlers\TestHandler']);
|
||||
$logger->setDateFormat("Y-m-d H:i:s:u");
|
||||
$this->assertTrue($logger->handle("warning", "This is a test log"));
|
||||
}
|
||||
@ -31,9 +32,8 @@ class FileHandlerTest extends \CIUnitTestCase
|
||||
{
|
||||
$config = new LoggerConfig();
|
||||
$config->path = $this->start . 'charlie/';
|
||||
$config->handlers['CodeIgniter\Log\Handlers\TestHandler']['handles'] = ['critical'];
|
||||
$logger = new TestHandler($config->handlers['CodeIgniter\Log\Handlers\TestHandler']);
|
||||
// $logger = new FileHandler($config->handlers['CodeIgniter\Log\Handlers\TestHandler']);
|
||||
$config->handlers['Tests\Support\Log\Handlers\TestHandler']['handles'] = ['critical'];
|
||||
$logger = new TestHandler($config->handlers['Tests\Support\Log\Handlers\TestHandler']);
|
||||
$logger->setDateFormat("Y-m-d H:i:s:u");
|
||||
$this->assertTrue($logger->handle("warning", "This is a test log"));
|
||||
}
|
||||
|
@ -218,6 +218,29 @@ class LoggerTest extends \CIUnitTestCase
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testLogInterpolatesExceptions()
|
||||
{
|
||||
$config = new LoggerConfig();
|
||||
$logger = new Logger($config);
|
||||
|
||||
$expected = 'ERROR - ' . date('Y-m-d') . ' --> [ERROR] These are not the droids you are looking for';
|
||||
|
||||
try
|
||||
{
|
||||
throw new Exception('These are not the droids you are looking for');
|
||||
} catch (\Exception $e)
|
||||
{
|
||||
$logger->log('error', '[ERROR] {exception}', ['exception' => $e]);
|
||||
}
|
||||
|
||||
$logs = TestHandler::getLogs();
|
||||
|
||||
$this->assertCount(1, $logs);
|
||||
$this->assertTrue(strpos($logs[0], $expected) === 0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testEmergencyLogsCorrectly()
|
||||
{
|
||||
$config = new LoggerConfig();
|
||||
@ -376,7 +399,7 @@ class LoggerTest extends \CIUnitTestCase
|
||||
$config = new LoggerConfig();
|
||||
$logger = new Logger($config);
|
||||
|
||||
$expected = '[CodeIgniter\Log\Handlers\TestHandler]';
|
||||
$expected = '[Tests\Support\Log\Handlers\TestHandler]';
|
||||
$logger->log(5, $config);
|
||||
|
||||
$logs = TestHandler::getLogs();
|
||||
@ -390,7 +413,7 @@ class LoggerTest extends \CIUnitTestCase
|
||||
public function testFilenameCleaning()
|
||||
{
|
||||
$config = new LoggerConfig();
|
||||
$logger = new \CodeIgniter\Log\TestLogger($config);
|
||||
$logger = new \Tests\Support\Log\TestLogger($config);
|
||||
|
||||
$ohoh = APPPATH . 'LoggerTest';
|
||||
$expected = 'APPPATH/LoggerTest';
|
||||
|
@ -105,7 +105,7 @@ file name and line number. You must still provide the exception placeholder in
|
||||
{
|
||||
... Something throws error here
|
||||
}
|
||||
catch (\Exception #e)
|
||||
catch (\Exception $e)
|
||||
{
|
||||
log_message('error', '[ERROR] {exception}', ['exception' => $e]);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user