Skip to content

Commit a1d115c

Browse files
committed
Add LoggerAssertionsTrait which provide shortcuts to assert a log has been written
1 parent 41da7e8 commit a1d115c

File tree

11 files changed

+169
-0
lines changed

11 files changed

+169
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.4
55
---
66

7+
* Add `LoggerAssertionsTrait`
78
* Add `AbstractController::renderBlock()` and `renderBlockView()`
89
* Add native return type to `Translator` and to `Application::reset()`
910
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable the integration by setting `framework.annotations` to `false`

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class KernelTestCase extends TestCase
2727
{
2828
use MailerAssertionsTrait;
2929
use NotificationAssertionsTrait;
30+
use LoggerAssertionsTrait;
3031

3132
protected static $class;
3233

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use Monolog\Handler\TestHandler;
15+
use Monolog\Logger;
16+
use Monolog\LogRecord;
17+
18+
trait LoggerAssertionsTrait
19+
{
20+
public static function assertLogExists(string $expectedLog, string $level = Logger::DEBUG): void
21+
{
22+
/** @var TestHandler $logger */
23+
$logger = self::getContainer()->get('monolog.handler.test');
24+
25+
self::assertTrue($logger->hasRecordThatPasses(
26+
function (array|LogRecord $record) use ($expectedLog) {
27+
return $record['message'] === $expectedLog;
28+
},
29+
$level,
30+
));
31+
}
32+
33+
public static function assertLogMatches(string $expectedRegex, string $level = Logger::DEBUG): void
34+
{
35+
/** @var TestHandler $logger */
36+
$logger = self::getContainer()->get('monolog.handler.test');
37+
38+
self::assertTrue($logger->hasRecordThatMatches($expectedRegex, $level));
39+
}
40+
41+
public static function assertLogContains(string $expectedLog, string $level = Logger::DEBUG): void
42+
{
43+
/** @var TestHandler $logger */
44+
$logger = self::getContainer()->get('monolog.handler.test');
45+
46+
self::assertTrue($logger->hasRecordThatContains($expectedLog, $level));
47+
}
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\HttpFoundation\Response;
16+
17+
final class LoggerController
18+
{
19+
public function index(LoggerInterface $logger)
20+
{
21+
$logger->debug('test1_' . __CLASS__);
22+
$logger->debug('test2_' . __CLASS__);
23+
$logger->debug('test3_' . __CLASS__);
24+
25+
return new Response();
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Logger;
4+
5+
use Monolog\Logger;
6+
7+
class TestLogger extends Logger
8+
{
9+
public array $logs = [];
10+
11+
public function __construct($handler)
12+
{
13+
parent::__construct(__CLASS__, [$handler]);
14+
}
15+
16+
public function log($level, $message, array $context = []): void
17+
{
18+
$this->logs[] = [
19+
'level' => $level,
20+
'message' => (string) $message,
21+
'context' => $context,
22+
];
23+
}
24+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ uid:
6868
send_notification:
6969
path: /send_notification
7070
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\NotificationController::indexAction }
71+
72+
log:
73+
path: /log
74+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\LoggerController::index }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
final class LoggerTest extends AbstractWebTestCase
15+
{
16+
public function testLoggerAssertion()
17+
{
18+
$client = $this->createClient(['test_case' => 'Logger', 'root_config' => 'config.yml', 'debug' => true]);
19+
$client->request('GET', '/log');
20+
21+
$this->assertLogExists('test1_Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\LoggerController');
22+
$this->assertLogMatches("/(test2_).*(LoggerController)/");
23+
$this->assertLogContains('test3');
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: services.yml }
4+
5+
framework:
6+
profiler: ~
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_loggertest_bundle:
2+
resource: '@TestBundle/Resources/config/routing.yml'

0 commit comments

Comments
 (0)