3dec3bd7f87a00878a9167ca2508c72f5b98a1dd
[yaffs-website] / vendor / symfony / http-kernel / Tests / DataCollector / LoggerDataCollectorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\DataCollector;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\Exception\SilencedErrorContext;
16 use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
17
18 class LoggerDataCollectorTest extends TestCase
19 {
20     public function testCollectWithUnexpectedFormat()
21     {
22         $logger = $this
23             ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
24             ->setMethods(array('countErrors', 'getLogs', 'clear'))
25             ->getMock();
26         $logger->expects($this->once())->method('countErrors')->will($this->returnValue('foo'));
27         $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue(array()));
28
29         $c = new LoggerDataCollector($logger, __DIR__.'/');
30         $c->lateCollect();
31         $compilerLogs = $c->getCompilerLogs()->getValue('message');
32
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']);
37
38         $this->assertSame(array(
39             array('message' => 'Some custom logging message'),
40             array('message' => 'With ending :'),
41         ), $compilerLogs['Unknown Compiler Pass']);
42     }
43
44     /**
45      * @dataProvider getCollectTestData
46      */
47     public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount, $expectedPriorities = null)
48     {
49         $logger = $this
50             ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
51             ->setMethods(array('countErrors', 'getLogs', 'clear'))
52             ->getMock();
53         $logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
54         $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
55
56         $c = new LoggerDataCollector($logger);
57         $c->lateCollect();
58
59         $this->assertEquals('logger', $c->getName());
60         $this->assertEquals($nb, $c->countErrors());
61
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"]);
66             }
67
68             return $v;
69         }, $c->getLogs()->getValue(true));
70         $this->assertEquals($expectedLogs, $logs);
71         $this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
72         $this->assertEquals($expectedScreamCount, $c->countScreams());
73
74         if (isset($expectedPriorities)) {
75             $this->assertSame($expectedPriorities, $c->getPriorities()->getValue(true));
76         }
77     }
78
79     public function testReset()
80     {
81         $logger = $this
82             ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
83             ->setMethods(array('countErrors', 'getLogs', 'clear'))
84             ->getMock();
85         $logger->expects($this->once())->method('clear');
86
87         $c = new LoggerDataCollector($logger);
88         $c->reset();
89     }
90
91     public function getCollectTestData()
92     {
93         yield 'simple log' => array(
94             1,
95             array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
96             array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
97             0,
98             0,
99         );
100
101         yield 'log with a context' => array(
102             1,
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')),
105             0,
106             0,
107         );
108
109         if (!class_exists(SilencedErrorContext::class)) {
110             return;
111         }
112
113         yield 'logs with some deprecations' => array(
114             1,
115             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'),
119             ),
120             array(
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),
124             ),
125             2,
126             0,
127             array(100 => array('count' => 3, 'name' => 'DEBUG')),
128         );
129
130         yield 'logs with some silent errors' => array(
131             1,
132             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'),
135             ),
136             array(
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),
139             ),
140             0,
141             1,
142         );
143     }
144 }