Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b796544

Browse files
committedFeb 17, 2025·
Added Symfony Logger assertion (dontSeeDeprecations)
1 parent 94e411b commit b796544

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
 

‎src/Codeception/Module/Symfony.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Codeception\Module\Symfony\EventsAssertionsTrait;
1818
use Codeception\Module\Symfony\FormAssertionsTrait;
1919
use Codeception\Module\Symfony\HttpClientAssertionsTrait;
20+
use Codeception\Module\Symfony\LoggerAssertionsTrait;
2021
use Codeception\Module\Symfony\MailerAssertionsTrait;
2122
use Codeception\Module\Symfony\MimeAssertionsTrait;
2223
use Codeception\Module\Symfony\ParameterAssertionsTrait;
@@ -142,6 +143,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
142143
use EventsAssertionsTrait;
143144
use FormAssertionsTrait;
144145
use HttpClientAssertionsTrait;
146+
use LoggerAssertionsTrait;
145147
use MailerAssertionsTrait;
146148
use MimeAssertionsTrait;
147149
use ParameterAssertionsTrait;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Symfony;
6+
7+
use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
8+
use Symfony\Component\VarDumper\Cloner\Data;
9+
use function sprintf;
10+
11+
trait LoggerAssertionsTrait
12+
{
13+
/**
14+
* Asserts that there are no deprecation messages in Symfony's log.
15+
*
16+
* ```php
17+
* <?php
18+
* $I->amOnPage('/home');
19+
* $I->dontSeeDeprecations();
20+
* ```
21+
*
22+
* @param string $message Optional custom failure message.
23+
*/
24+
public function dontSeeDeprecations(string $message = ''): void
25+
{
26+
$loggerCollector = $this->grabLoggerCollector(__FUNCTION__);
27+
$logs = $loggerCollector->getProcessedLogs();
28+
29+
$foundDeprecations = [];
30+
31+
foreach ($logs as $log) {
32+
if (isset($log['type']) && $log['type'] === 'deprecation') {
33+
$msg = $log['message'];
34+
if ($msg instanceof Data) {
35+
$msg = $msg->getValue(true);
36+
}
37+
if (!is_string($msg)) {
38+
$msg = (string)$msg;
39+
}
40+
$foundDeprecations[] = $msg;
41+
}
42+
}
43+
44+
$errorMessage = $message ?: sprintf(
45+
"Found %d deprecation message%s in the log:\n%s",
46+
count($foundDeprecations),
47+
count($foundDeprecations) > 1 ? 's' : '',
48+
implode("\n", array_map(static function ($msg) {
49+
return " - " . $msg;
50+
}, $foundDeprecations))
51+
);
52+
53+
$this->assertEmpty($foundDeprecations, $errorMessage);
54+
}
55+
56+
protected function grabLoggerCollector(string $function): LoggerDataCollector
57+
{
58+
return $this->grabCollector('logger', $function);
59+
}
60+
}

0 commit comments

Comments
 (0)
Please sign in to comment.