forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerAssertionsTrait.php
More file actions
64 lines (57 loc) · 1.87 KB
/
LoggerAssertionsTrait.php
File metadata and controls
64 lines (57 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
use Symfony\Component\VarDumper\Cloner\Data;
use function array_map;
use function count;
use function implode;
use function is_scalar;
use function is_string;
use function json_encode;
use function sprintf;
trait LoggerAssertionsTrait
{
/**
* Asserts that there are no deprecation messages in Symfony's log.
*
* ```php
* <?php
* $I->amOnPage('/home');
* $I->dontSeeDeprecations();
* ```
*
* @param string $message Optional custom failure message.
*/
public function dontSeeDeprecations(string $message = ''): void
{
$logs = $this->grabLoggerCollector(__FUNCTION__)->getProcessedLogs();
$foundDeprecations = [];
/** @var array<string, mixed> $log */
foreach ($logs as $log) {
if (!isset($log['type']) || $log['type'] !== 'deprecation') {
continue;
}
$msg = $log['message'];
if ($msg instanceof Data) {
$msg = $msg->getValue(true);
}
if (!is_string($msg) && !is_scalar($msg)) {
$msg = json_encode($msg, JSON_THROW_ON_ERROR);
}
$foundDeprecations[] = (string) $msg;
}
$count = count($foundDeprecations);
$errorMessage = $message ?: sprintf(
"Found %d deprecation message%s in the log:\n%s",
$count,
$count !== 1 ? 's' : '',
implode("\n", array_map(static fn(string $m): string => " - $m", $foundDeprecations)),
);
$this->assertEmpty($foundDeprecations, $errorMessage);
}
protected function grabLoggerCollector(string $function): LoggerDataCollector
{
return $this->grabCollector(DataCollectorName::LOGGER, $function);
}
}