|
| 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