LoggerDataCollectorTest.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\DataCollector;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Debug\Exception\SilencedErrorContext;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
  17. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  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. $c = new LoggerDataCollector($logger, __DIR__.'/');
  29. $c->lateCollect();
  30. $compilerLogs = $c->getCompilerLogs()->getValue('message');
  31. $this->assertSame(array(
  32. array('message' => 'Removed service "Psr\Container\ContainerInterface"; reason: private alias.'),
  33. array('message' => 'Removed service "Symfony\Component\DependencyInjection\ContainerInterface"; reason: private alias.'),
  34. ), $compilerLogs['Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass']);
  35. $this->assertSame(array(
  36. array('message' => 'Some custom logging message'),
  37. array('message' => 'With ending :'),
  38. ), $compilerLogs['Unknown Compiler Pass']);
  39. }
  40. public function testWithMasterRequest()
  41. {
  42. $masterRequest = new Request();
  43. $stack = new RequestStack();
  44. $stack->push($masterRequest);
  45. $logger = $this
  46. ->getMockBuilder(DebugLoggerInterface::class)
  47. ->setMethods(array('countErrors', 'getLogs', 'clear'))
  48. ->getMock();
  49. $logger->expects($this->once())->method('countErrors')->with(null);
  50. $logger->expects($this->exactly(2))->method('getLogs')->with(null)->will($this->returnValue(array()));
  51. $c = new LoggerDataCollector($logger, __DIR__.'/', $stack);
  52. $c->collect($masterRequest, new Response());
  53. $c->lateCollect();
  54. }
  55. public function testWithSubRequest()
  56. {
  57. $masterRequest = new Request();
  58. $subRequest = new Request();
  59. $stack = new RequestStack();
  60. $stack->push($masterRequest);
  61. $stack->push($subRequest);
  62. $logger = $this
  63. ->getMockBuilder(DebugLoggerInterface::class)
  64. ->setMethods(array('countErrors', 'getLogs', 'clear'))
  65. ->getMock();
  66. $logger->expects($this->once())->method('countErrors')->with($subRequest);
  67. $logger->expects($this->exactly(2))->method('getLogs')->with($subRequest)->will($this->returnValue(array()));
  68. $c = new LoggerDataCollector($logger, __DIR__.'/', $stack);
  69. $c->collect($subRequest, new Response());
  70. $c->lateCollect();
  71. }
  72. /**
  73. * @dataProvider getCollectTestData
  74. */
  75. public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount, $expectedPriorities = null)
  76. {
  77. $logger = $this
  78. ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
  79. ->setMethods(array('countErrors', 'getLogs', 'clear'))
  80. ->getMock();
  81. $logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
  82. $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
  83. $c = new LoggerDataCollector($logger);
  84. $c->lateCollect();
  85. $this->assertEquals('logger', $c->getName());
  86. $this->assertEquals($nb, $c->countErrors());
  87. $logs = array_map(function ($v) {
  88. if (isset($v['context']['exception'])) {
  89. $e = &$v['context']['exception'];
  90. $e = isset($e["\0*\0message"]) ? array($e["\0*\0message"], $e["\0*\0severity"]) : array($e["\0Symfony\Component\Debug\Exception\SilencedErrorContext\0severity"]);
  91. }
  92. return $v;
  93. }, $c->getLogs()->getValue(true));
  94. $this->assertEquals($expectedLogs, $logs);
  95. $this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
  96. $this->assertEquals($expectedScreamCount, $c->countScreams());
  97. if (isset($expectedPriorities)) {
  98. $this->assertSame($expectedPriorities, $c->getPriorities()->getValue(true));
  99. }
  100. }
  101. public function testReset()
  102. {
  103. $logger = $this
  104. ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
  105. ->setMethods(array('countErrors', 'getLogs', 'clear'))
  106. ->getMock();
  107. $logger->expects($this->once())->method('clear');
  108. $c = new LoggerDataCollector($logger);
  109. $c->reset();
  110. }
  111. public function getCollectTestData()
  112. {
  113. yield 'simple log' => array(
  114. 1,
  115. array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
  116. array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
  117. 0,
  118. 0,
  119. );
  120. yield 'log with a context' => array(
  121. 1,
  122. array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
  123. array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
  124. 0,
  125. 0,
  126. );
  127. if (!class_exists(SilencedErrorContext::class)) {
  128. return;
  129. }
  130. yield 'logs with some deprecations' => 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' => 'foo', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  135. array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  136. ),
  137. array(
  138. array('message' => 'foo3', 'context' => array('exception' => array('warning', E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  139. array('message' => 'foo', 'context' => array('exception' => array('deprecated', E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
  140. array('message' => 'foo2', 'context' => array('exception' => array('deprecated', E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
  141. ),
  142. 2,
  143. 0,
  144. array(100 => array('count' => 3, 'name' => 'DEBUG')),
  145. );
  146. yield 'logs with some silent errors' => array(
  147. 1,
  148. array(
  149. array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  150. array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  151. ),
  152. array(
  153. array('message' => 'foo3', 'context' => array('exception' => array('warning', E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  154. array('message' => 'foo3', 'context' => array('exception' => array(E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true),
  155. ),
  156. 0,
  157. 1,
  158. );
  159. }
  160. }