View source
<?php
namespace Drupal\Tests\purge\Unit\Logger;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\purge\Logger\LoggerChannelPart;
use Drupal\Tests\UnitTestCase;
class LoggerChannelPartTest extends UnitTestCase {
protected $loggerChannelPurge;
protected function setUp() : void {
$this->loggerChannelPurge = $this
->createMock('\\Psr\\Log\\LoggerInterface');
}
private function helperForSeverityMethods($id, array $grants, $output, $severity) : void {
$occurrence = is_null($output) ? $this
->never() : $this
->once();
$this->loggerChannelPurge
->expects($occurrence)
->method('log')
->with($this
->stringContains($severity), $this
->stringContains('@purge_channel_part: @replaceme'), $this
->callback(function ($subject) use ($id, $output) {
return $subject['@purge_channel_part'] === $id && $subject['@replaceme'] === $output;
}));
$part = new LoggerChannelPart($this->loggerChannelPurge, $id, $grants);
$part
->{$severity}('@replaceme', [
'@replaceme' => $output,
]);
}
public function testInstance() : void {
$part = new LoggerChannelPart($this->loggerChannelPurge, 'id', []);
$this
->assertInstanceOf('\\Psr\\Log\\LoggerInterface', $part);
}
public function testGetGrants(array $grants) : void {
$part = new LoggerChannelPart($this->loggerChannelPurge, 'id', $grants);
$this
->assertEquals(count($grants), count($part
->getGrants()));
$this
->assertEquals($grants, $part
->getGrants());
foreach ($part
->getGrants() as $k => $v) {
$this
->assertTrue(is_int($k));
$this
->assertTrue(is_int($v));
}
}
public function providerTestGetGrants() : array {
return [
[
[],
],
[
[
RfcLogLevel::EMERGENCY,
],
],
[
[
RfcLogLevel::ALERT,
],
],
[
[
RfcLogLevel::CRITICAL,
],
],
[
[
RfcLogLevel::ERROR,
],
],
[
[
RfcLogLevel::WARNING,
],
],
[
[
RfcLogLevel::NOTICE,
],
],
[
[
RfcLogLevel::INFO,
],
],
[
[
RfcLogLevel::INFO,
RfcLogLevel::DEBUG,
],
],
[
[
RfcLogLevel::DEBUG,
],
],
];
}
public function testEmergency($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'emergency');
}
public function providerTestEmergency() : array {
return [
[
'good',
[
RfcLogLevel::EMERGENCY,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testAlert($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'alert');
}
public function providerTestAlert() : array {
return [
[
'good',
[
RfcLogLevel::ALERT,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testCritical($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'critical');
}
public function providerTestCritical() : array {
return [
[
'good',
[
RfcLogLevel::CRITICAL,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testError($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'error');
}
public function providerTestError() : array {
return [
[
'good',
[
RfcLogLevel::ERROR,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testWarning($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'warning');
}
public function providerTestWarning() : array {
return [
[
'good',
[
RfcLogLevel::WARNING,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testNotice($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'notice');
}
public function providerTestNotice() : array {
return [
[
'good',
[
RfcLogLevel::NOTICE,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testInfo($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'info');
}
public function providerTestInfo() : array {
return [
[
'good',
[
RfcLogLevel::INFO,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testDebug($id, array $grants, $output) : void {
$this
->helperForSeverityMethods($id, $grants, $output, 'debug');
}
public function providerTestDebug() : array {
return [
[
'good',
[
RfcLogLevel::DEBUG,
],
'bazinga!',
],
[
'bad',
[
-1,
],
NULL,
],
];
}
public function testLog($id, $level, $message, $output) : void {
$this->loggerChannelPurge
->expects($this
->once())
->method('log')
->with($this
->stringContains($level), $this
->stringContains('@purge_channel_part: ' . $message), $this
->callback(function ($subject) use ($id, $output) {
return $subject['@purge_channel_part'] === $id && $subject['@replaceme'] === $output;
}));
$part = new LoggerChannelPart($this->loggerChannelPurge, $id);
$part
->log($level, $message, [
'@replaceme' => $output,
]);
}
public function providerTestLog() : array {
return [
[
'id1',
'level1',
'message @placeholder',
[
'@placeholder' => 'foo',
],
],
[
'id2',
'level2',
'message @placeholder',
[
'@placeholder' => 'bar',
],
],
[
'id3',
'level3',
'message @placeholder',
[
'@placeholder' => 'baz',
],
],
];
}
}