Commit b9a0ff26 authored by crell's avatar crell Committed by Stefan Bürk
Browse files

[TASK] Add tests for log `#[Channel(...)]` attribute

v11 introduced support for using a Channel attribute to define
what log channel should be injected into a class, either via
the constructor or the LoggingAwareInterface. However, the
initial patch lacked any tests.

This patch adds basic tests to verify that the attribute works,
and indirectly document it.

Resolves: #97695
Related: #95079
Related: #95044
Releases: main, 11.5
Change-Id: I7cc97fc839ccd4015ac20b1bce787d4cbbc40a26
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74750

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
parent cda3d9f5
......@@ -290,7 +290,8 @@
"TYPO3\\CMS\\Workspaces\\Tests\\": "typo3/sysext/workspaces/Tests/",
"TYPO3\\CMS\\Recycler\\Tests\\": "typo3/sysext/recycler/Tests/",
"TYPO3\\CMS\\T3editor\\Tests\\": "typo3/sysext/t3editor/Tests/",
"TYPO3\\CMS\\Tstemplate\\Tests\\": "typo3/sysext/tstemplate/Tests/"
"TYPO3\\CMS\\Tstemplate\\Tests\\": "typo3/sysext/tstemplate/Tests/",
"TYPO3Tests\\TestLogger\\": "typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_logger/Classes/"
},
"classmap": [
"typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/",
......
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3Tests\TestLogger;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Log\Channel;
class ConstructorAttributeChannelTester
{
public function __construct(#[Channel('beep')] protected LoggerInterface $logger)
{
}
public function run(): void
{
$this->logger->debug('beep beep');
}
}
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3Tests\TestLogger;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Log\Channel;
#[Channel(name: 'beep')]
class LoggerAwareClassAttributeChannelTester implements LoggerAwareInterface
{
use LoggerAwareTrait;
public function run(): void
{
$this->logger->debug('beep beep');
}
}
services:
_defaults:
autowire: true
autoconfigure: true
public: false
TYPO3Tests\TestLogger\ConstructorAttributeChannelTester:
public: true
TYPO3Tests\TestLogger\LoggerAwareClassAttributeChannelTester:
public: true
<?php
declare(strict_types=1);
$EM_CONF[$_EXTKEY] = [
'title' => 'Logger Test',
'description' => 'Logger Test',
'category' => 'example',
'version' => '12.0.0',
'state' => 'beta',
'author' => 'Larry Garfield',
'author_email' => 'larry.garfield@typo3.org',
'author_company' => 'TYPO3 GmbH',
'constraints' => [
'depends' => [
'typo3' => '12.0.0',
],
'conflicts' => [],
'suggests' => [],
],
];
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\Functional\Fixtures\Log;
use TYPO3\CMS\Core\Log\LogRecord;
use TYPO3\CMS\Core\Log\Writer\WriterInterface;
class DummyWriter implements WriterInterface
{
/** @var LogRecord[] */
public static array $logs = [];
public function writeLog(LogRecord $record): WriterInterface
{
self::$logs[] = $record;
return $this;
}
}
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\Functional\Log;
use Psr\Log\LogLevel;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Log\LogRecord;
use TYPO3\CMS\Core\Tests\Functional\Fixtures\Log\DummyWriter;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3Tests\TestLogger\ConstructorAttributeChannelTester;
use TYPO3Tests\TestLogger\LoggerAwareClassAttributeChannelTester;
class LoggerAwareChannelTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = [
'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_logger',
];
protected array $configurationToUseInTestInstance = [
'LOG' => [
'beep' => [
'writerConfiguration' => [
LogLevel::DEBUG => [
DummyWriter::class => [],
],
],
],
],
];
protected function setUp(): void
{
parent::setUp();
$this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
$this->setUpBackendUser(1);
Bootstrap::initializeLanguageObject();
DummyWriter::$logs = [];
}
protected function tearDown(): void
{
DummyWriter::$logs = [];
parent::tearDown();
}
/**
* @test
*/
public function classLevelChannelAttributeIsRead(): void
{
$container = $this->getContainer();
$subject = $container->get(LoggerAwareClassAttributeChannelTester::class);
$subject->run();
self::assertInstanceOf(LogRecord::class, DummyWriter::$logs[0]);
self::assertSame('beep beep', DummyWriter::$logs[0]->getMessage());
}
/**
* @test
*/
public function constructorChannelAttributeIsRead(): void
{
$container = $this->getContainer();
$subject = $container->get(ConstructorAttributeChannelTester::class);
$subject->run();
self::assertInstanceOf(LogRecord::class, DummyWriter::$logs[0]);
self::assertSame('beep beep', DummyWriter::$logs[0]->getMessage());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment