function LoggerChannelTest::testLog

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest::testLog()
  2. 8.9.x core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest::testLog()
  3. 10 core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest::testLog()

Tests LoggerChannel::log().

@dataProvider providerTestLog @covers ::log @covers ::setCurrentUser @covers ::setRequestStack

Parameters

callable $expected: An anonymous function to use with $this->callback() of the logger mock. The function should check the $context array for expected values.

bool $request: Whether to pass a request to the channel under test.

bool $account: Whether to pass an account to the channel under test.

File

core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php, line 38

Class

LoggerChannelTest
@coversDefaultClass \Drupal\Core\Logger\LoggerChannel @group Logger

Namespace

Drupal\Tests\Core\Logger

Code

public function testLog(callable $expected, bool $request = FALSE, bool $account = FALSE) : void {
    $channel = new LoggerChannel('test');
    $message = $this->randomMachineName();
    $logger = $this->createMock('Psr\\Log\\LoggerInterface');
    $logger->expects($this->once())
        ->method('log')
        ->with($this->anything(), $message, $this->callback($expected));
    $channel->addLogger($logger);
    if ($request) {
        $request_mock = $this->getMockBuilder(Request::class)
            ->onlyMethods([
            'getClientIp',
        ])
            ->getMock();
        $request_mock->expects($this->any())
            ->method('getClientIp')
            ->willReturn('127.0.0.1');
        $request_mock->headers = $this->createMock(HeaderBag::class);
        $requestStack = new RequestStack();
        $requestStack->push($request_mock);
        $channel->setRequestStack($requestStack);
    }
    if ($account) {
        $account_mock = $this->createMock(AccountInterface::class);
        $account_mock->expects($this->any())
            ->method('id')
            ->willReturn(1);
        $channel->setCurrentUser($account_mock);
    }
    $channel->log(rand(0, 7), $message);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.