LoggerChannelTest.php in Zircon Profile 8.0
File
core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php
View source
<?php
namespace Drupal\Tests\Core\Logger;
use Drupal\Core\Logger\LoggerChannel;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class LoggerChannelTest extends UnitTestCase {
public function testLog(callable $expected, Request $request = NULL, AccountInterface $current_user = NULL) {
$channel = new LoggerChannel('test');
$message = $this
->randomMachineName();
$logger = $this
->getMock('Psr\\Log\\LoggerInterface');
$logger
->expects($this
->once())
->method('log')
->with($this
->anything(), $message, $this
->callback($expected));
$channel
->addLogger($logger);
if ($request) {
$requestStack = new RequestStack();
$requestStack
->push($request);
$channel
->setRequestStack($requestStack);
}
if ($current_user) {
$channel
->setCurrentUser($current_user);
}
$channel
->log(rand(0, 7), $message);
}
public function testSortLoggers() {
$channel = new LoggerChannel($this
->randomMachineName());
$index_order = '';
for ($i = 0; $i < 4; $i++) {
$logger = $this
->getMock('Psr\\Log\\LoggerInterface');
$logger
->expects($this
->once())
->method('log')
->will($this
->returnCallback(function () use ($i, &$index_order) {
$index_order .= $i;
}));
$channel
->addLogger($logger, $i);
}
$channel
->log(rand(0, 7), $this
->randomMachineName());
$this
->assertEquals($index_order, '3210');
}
public function providerTestLog() {
$account_mock = $this
->getMock('Drupal\\Core\\Session\\AccountInterface');
$account_mock
->expects($this
->exactly(2))
->method('id')
->will($this
->returnValue(1));
$request_mock = $this
->getMock('Symfony\\Component\\HttpFoundation\\Request');
$request_mock
->expects($this
->exactly(2))
->method('getClientIp')
->will($this
->returnValue('127.0.0.1'));
$request_mock->headers = $this
->getMock('Symfony\\Component\\HttpFoundation\\ParameterBag');
$cases[] = array(
function ($context) {
return $context['channel'] == 'test' && empty($context['uid']) && empty($context['ip']);
},
);
$cases[] = array(
function ($context) {
return $context['uid'] === 0 && empty($context['ip']);
},
NULL,
$account_mock,
);
$cases[] = array(
function ($context) {
return $context['ip'] === '127.0.0.1' && empty($context['uid']);
},
$request_mock,
);
$cases[] = array(
function ($context) {
return $context['ip'] === '127.0.0.1' && $context['uid'] === 1;
},
$request_mock,
$account_mock,
);
return $cases;
}
}