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;
17 use Symfony\Component\VarDumper\Cloner\Data;
19 class LoggerDataCollectorTest extends TestCase
24 * @dataProvider getCollectTestData
26 public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount, $expectedPriorities = null)
28 $logger = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')->getMock();
29 $logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
30 $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
32 // disable cloning the context, to ease fixtures creation.
33 $c = $this->getMockBuilder(LoggerDataCollector::class)
34 ->setMethods(array('cloneVar'))
35 ->setConstructorArgs(array($logger))
37 $c->expects($this->any())->method('cloneVar')->willReturn(self::$data);
40 $this->assertEquals('logger', $c->getName());
41 $this->assertEquals($nb, $c->countErrors());
42 $this->assertEquals($expectedLogs, $c->getLogs());
43 $this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
44 $this->assertEquals($expectedScreamCount, $c->countScreams());
46 if (isset($expectedPriorities)) {
47 $this->assertSame($expectedPriorities, $c->getPriorities());
51 public function getCollectTestData()
53 if (null === self::$data) {
54 self::$data = new Data(array());
57 yield 'simple log' => array(
59 array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
60 array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
65 yield 'log with a context' => array(
67 array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
68 array(array('message' => 'foo', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG')),
73 if (!class_exists(SilencedErrorContext::class)) {
77 yield 'logs with some deprecations' => array(
80 array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
81 array('message' => 'foo', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
82 array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
85 array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG'),
86 array('message' => 'foo', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
87 array('message' => 'foo2', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
91 array(100 => array('count' => 3, 'name' => 'DEBUG')),
94 yield 'logs with some silent errors' => array(
97 array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
98 array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
101 array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG'),
102 array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true),