4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\DataCollector;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\Exception\SilencedErrorContext;
16 use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
18 class LoggerDataCollectorTest extends TestCase
20 public function testCollectWithUnexpectedFormat()
23 ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
24 ->setMethods(array('countErrors', 'getLogs', 'clear'))
26 $logger->expects($this->once())->method('countErrors')->will($this->returnValue('foo'));
27 $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue(array()));
29 $c = new LoggerDataCollector($logger, __DIR__.'/');
31 $compilerLogs = $c->getCompilerLogs()->getValue('message');
33 $this->assertSame(array(
34 array('message' => 'Removed service "Psr\Container\ContainerInterface"; reason: private alias.'),
35 array('message' => 'Removed service "Symfony\Component\DependencyInjection\ContainerInterface"; reason: private alias.'),
36 ), $compilerLogs['Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass']);
38 $this->assertSame(array(
39 array('message' => 'Some custom logging message'),
40 array('message' => 'With ending :'),
41 ), $compilerLogs['Unknown Compiler Pass']);
45 * @dataProvider getCollectTestData
47 public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount, $expectedPriorities = null)
50 ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
51 ->setMethods(array('countErrors', 'getLogs', 'clear'))
53 $logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
54 $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
56 $c = new LoggerDataCollector($logger);
59 $this->assertEquals('logger', $c->getName());
60 $this->assertEquals($nb, $c->countErrors());
62 $logs = array_map(function ($v) {
63 if (isset($v['context']['exception'])) {
64 $e = &$v['context']['exception'];
65 $e = isset($e["\0*\0message"]) ? array($e["\0*\0message"], $e["\0*\0severity"]) : array($e["\0Symfony\Component\Debug\Exception\SilencedErrorContext\0severity"]);
69 }, $c->getLogs()->getValue(true));
70 $this->assertEquals($expectedLogs, $logs);
71 $this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
72 $this->assertEquals($expectedScreamCount, $c->countScreams());
74 if (isset($expectedPriorities)) {
75 $this->assertSame($expectedPriorities, $c->getPriorities()->getValue(true));
79 public function testReset()
82 ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
83 ->setMethods(array('countErrors', 'getLogs', 'clear'))
85 $logger->expects($this->once())->method('clear');
87 $c = new LoggerDataCollector($logger);
91 public function getCollectTestData()
93 yield 'simple log' => array(
95 array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
96 array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
101 yield 'log with a context' => array(
103 array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
104 array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
109 if (!class_exists(SilencedErrorContext::class)) {
113 yield 'logs with some deprecations' => array(
116 array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
117 array('message' => 'foo', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
118 array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
121 array('message' => 'foo3', 'context' => array('exception' => array('warning', E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
122 array('message' => 'foo', 'context' => array('exception' => array('deprecated', E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
123 array('message' => 'foo2', 'context' => array('exception' => array('deprecated', E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
127 array(100 => array('count' => 3, 'name' => 'DEBUG')),
130 yield 'logs with some silent errors' => array(
133 array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
134 array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
137 array('message' => 'foo3', 'context' => array('exception' => array('warning', E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
138 array('message' => 'foo3', 'context' => array('exception' => array(E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true),