Skip to content

Commit 7de1568

Browse files
committed
Added Symfony Logger assertion (dontSeeDeprecations)
1 parent 94e411b commit 7de1568

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 2 additions & 0 deletions
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;
Lines changed: 60 additions & 0 deletions
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 the 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy