class LoggerChannelTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest
  3. 11.x core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest

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

Hierarchy

Expanded class hierarchy of LoggerChannelTest

File

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

Namespace

Drupal\Tests\Core\Logger
View source
class LoggerChannelTest extends UnitTestCase {
    
    /**
     * Tests LoggerChannel::log().
     *
     * @param callable $expected
     *   An anonymous function to use with $this->callback() of the logger mock.
     *   The function should check the $context array for expected values.
     * @param bool $request
     *   Whether to pass a request to the channel under test.
     * @param bool $account
     *   Whether to pass an account to the channel under test.
     *
     * @dataProvider providerTestLog
     * @covers ::log
     * @covers ::setCurrentUser
     * @covers ::setRequestStack
     */
    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);
    }
    
    /**
     * Tests LoggerChannel::log() recursion protection.
     *
     * @covers ::log
     */
    public function testLogRecursionProtection() : void {
        $channel = new LoggerChannel('test');
        $logger = $this->createMock('Psr\\Log\\LoggerInterface');
        $logger->expects($this->exactly(LoggerChannel::MAX_CALL_DEPTH))
            ->method('log');
        $channel->addLogger($logger);
        $channel->addLogger(new NaughtyRecursiveLogger($channel));
        $channel->log(rand(0, 7), $this->randomMachineName());
    }
    
    /**
     * Tests LoggerChannel::addLoggers().
     *
     * @covers ::addLogger
     * @covers ::sortLoggers
     */
    public function testSortLoggers() : void {
        $channel = new LoggerChannel($this->randomMachineName());
        $index_order = '';
        for ($i = 0; $i < 4; $i++) {
            $logger = $this->createMock('Psr\\Log\\LoggerInterface');
            $logger->expects($this->once())
                ->method('log')
                ->willReturnCallback(function () use ($i, &$index_order) {
                // Append the $i to the index order, so that we know the order that
                // loggers got called with.
                $index_order .= $i;
            });
            $channel->addLogger($logger, $i);
        }
        $channel->log(rand(0, 7), $this->randomMachineName());
        // Ensure that the logger added in the end fired first.
        $this->assertEquals('3210', $index_order);
    }
    
    /**
     * Tests that $context['ip'] is a string even when the request's IP is NULL.
     */
    public function testNullIp() : void {
        // Create a logger that will fail if $context['ip'] is not an empty string.
        $logger = $this->createMock(LoggerInterface::class);
        $expected = function ($context) {
            return $context['channel'] == 'test' && $context['ip'] === '';
        };
        $logger->expects($this->once())
            ->method('log')
            ->with($this->anything(), 'Test message', $this->callback($expected));
        // Set up a request stack that has a request that will return NULL when
        // ::getClientIp() is called.
        $requestStack = new RequestStack();
        $request_mock = $this->getMockBuilder(Request::class)
            ->onlyMethods([
            'getClientIp',
        ])
            ->getMock();
        $request_mock->expects($this->any())
            ->method('getClientIp')
            ->willReturn(NULL);
        $requestStack->push($request_mock);
        // Set up the logger channel for testing.
        $channel = new LoggerChannel('test');
        $channel->addLogger($logger);
        $channel->setRequestStack($requestStack);
        // Perform the test.
        $channel->log(rand(0, 7), 'Test message');
    }
    
    /**
     * Data provider for self::testLog().
     */
    public static function providerTestLog() : \Generator {
        // No request or account.
        (yield [
            function ($context) {
                return $context['channel'] == 'test' && empty($context['uid']) && $context['ip'] === '';
            },
            FALSE,
            FALSE,
        ]);
        // With account but not request. Since the request is not available the
        // current user should not be used.
        (yield [
            function ($context) {
                return $context['uid'] === 0 && $context['ip'] === '';
            },
            FALSE,
            TRUE,
        ]);
        // With request but not account.
        (yield [
            function ($context) {
                return $context['ip'] === '127.0.0.1' && empty($context['uid']);
            },
            TRUE,
            FALSE,
        ]);
        // Both request and account.
        (yield [
            function ($context) {
                return $context['ip'] === '127.0.0.1' && $context['uid'] === 1;
            },
            TRUE,
            TRUE,
        ]);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
LoggerChannelTest::providerTestLog public static function Data provider for self::testLog().
LoggerChannelTest::testLog public function Tests LoggerChannel::log().
LoggerChannelTest::testLogRecursionProtection public function Tests LoggerChannel::log() recursion protection.
LoggerChannelTest::testNullIp public function Tests that $context[&#039;ip&#039;] is a string even when the request&#039;s IP is NULL.
LoggerChannelTest::testSortLoggers public function Tests LoggerChannel::addLoggers().
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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