class JsonLogTest in JSONlog 3.x
Same name and namespace in other branches
- 8.2 tests/src/Unit/JsonLogTest.php \Drupal\Tests\jsonlog\Unit\JsonLogTest
- 8 tests/src/Unit/JsonLogTest.php \Drupal\Tests\jsonlog\Unit\JsonLogTest
Unit tests for JsonLog class
@group JsonLog
Class JsonLogTest @package Drupal\Tests\jsonlog\Unit
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\jsonlog\Unit\JsonLogTest
Expanded class hierarchy of JsonLogTest
File
- tests/
src/ Unit/ JsonLogTest.php, line 22
Namespace
Drupal\Tests\jsonlog\UnitView source
class JsonLogTest extends UnitTestCase {
const DEFAULT_THRESHOLD = 4;
const DEFAULT_LOG_DIR = '/var/log';
const DEFAULT_TIME_FORMAT = 'Ymd';
const DEFAULT_SITE_ID = 'jsonlog_test';
/**
* @var mixed - mock of \Symfony\Component\HttpFoundation\RequestStack
*/
private $requestStackMock;
/**
* @var \Drupal\jsonlog\Logger\JsonLog
*/
private $jsonLogger;
protected function setUp() {
parent::setUp();
$config_stub['jsonlog.settings'] = [
'jsonlog_severity_threshold' => self::DEFAULT_THRESHOLD,
'jsonlog_truncate' => 64,
'jsonlog_siteid' => self::DEFAULT_SITE_ID,
'jsonlog_canonical' => '',
'jsonlog_file_time' => self::DEFAULT_TIME_FORMAT,
'jsonlog_dir' => self::DEFAULT_LOG_DIR,
'jsonlog_tags' => 'test',
];
/** @var ConfigFactoryInterface $configFactoryMock */
$configFactoryMock = $this
->getConfigFactoryStub($config_stub);
/** @var LogMessageParserInterface $messageParserMock */
$messageParserMock = $this
->getEmptyMessageParserMock();
/** @var ModuleHandlerInterface $moduleHandlerMock */
$moduleHandlerMock = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
/** @var RequestStack requestStackMock */
$this->requestStackMock = $this
->createMock('Symfony\\Component\\HttpFoundation\\RequestStack');
$this->jsonLogger = new JsonLog($configFactoryMock, $messageParserMock, $moduleHandlerMock, $this->requestStackMock);
}
/**
* test to see if a log file is well prepared
*/
public function testCanPrepareLogFile() {
$request_mock = $this
->createMock('Symfony\\Component\\HttpFoundation\\Request');
$request_mock
->expects($this
->exactly(1))
->method('getRealMethod')
->willReturn('POST');
/** @var Request $request_mock */
$this
->setupContainerCurrentRequest($request_mock);
$test_context = [
'user' => NULL,
'uid' => 12345,
'ip' => '127.0.0.9',
'request_uri' => 'admin/help',
'channel' => '500',
'link' => '',
'referer' => '',
];
$level = self::DEFAULT_THRESHOLD - 1;
$log_entry = $this->jsonLogger
->prepareLog($level, 'test', $test_context);
$this
->assertFalse($log_entry === FALSE, 'Log entry constructed');
$this
->assertEquals(JsonLogData::class, get_class($log_entry));
$this
->assertEquals('500', $log_entry
->getData()['subtype'], 'Correct subtype logged');
$this
->assertEquals('127.0.0.9', $log_entry
->getData()['client_ip'], 'Correct client_ip logged');
$this
->assertEquals('admin/help', $log_entry
->getData()['request_uri'], 'Correct request_uri logged');
$this
->assertEquals('POST', $log_entry
->getData()['method'], 'Correct method logged');
$this
->assertEquals(12345, $log_entry
->getData()['uid'], 'Uid logged.');
}
/**
* test to see if a log file is not prepared when the log-level is too high
*/
public function testCannotPrepareLogFileIfThresholdExceeded() {
$level = self::DEFAULT_THRESHOLD + 1;
$log_entry = $this->jsonLogger
->prepareLog($level, 'test', []);
$this
->assertTrue($log_entry === FALSE, 'Log entry not built');
}
/**
* test correct filename can be constructed
*/
public function testGetCorrectFilename() {
$time = date(self::DEFAULT_TIME_FORMAT);
$filename = $this->jsonLogger
->getFileName(self::DEFAULT_TIME_FORMAT);
$this
->assertEquals(self::DEFAULT_LOG_DIR . '/' . self::DEFAULT_SITE_ID . '.' . $time . '.json.log', $filename, 'Correct filename constructed.');
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject
*/
private function getEmptyMessageParserMock() {
$messageParserMock = $this
->createMock('Drupal\\Core\\Logger\\LogMessageParserInterface');
$messageParserMock
->expects($this
->atMost(1))
->method('parseMessagePlaceholders')
->withAnyParameters()
->willReturn([]);
return $messageParserMock;
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request
*/
private function setupContainerCurrentRequest(Request $request) {
$this->requestStackMock
->expects($this
->any())
->method('getCurrentRequest')
->willReturn($request);
$containerMock = $this
->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$containerMock
->expects($this
->any())
->method('get')
->with('request_stack')
->willReturn($this->requestStackMock);
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $containerMock */
\Drupal::setContainer($containerMock);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JsonLogTest:: |
private | property | ||
JsonLogTest:: |
private | property | ||
JsonLogTest:: |
constant | |||
JsonLogTest:: |
constant | |||
JsonLogTest:: |
constant | |||
JsonLogTest:: |
constant | |||
JsonLogTest:: |
private | function | ||
JsonLogTest:: |
protected | function |
Overrides UnitTestCase:: |
|
JsonLogTest:: |
private | function | ||
JsonLogTest:: |
public | function | test to see if a log file is not prepared when the log-level is too high | |
JsonLogTest:: |
public | function | test to see if a log file is well prepared | |
JsonLogTest:: |
public | function | test correct filename can be constructed | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |