LoggerDataCollectorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\HttpKernel\DataCollector\LoggerDataCollector;
  14. class LoggerDataCollectorTest extends TestCase
  15. {
  16. public function testCollectWithUnexpectedFormat()
  17. {
  18. $logger = $this
  19. ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
  20. ->setMethods(['countErrors', 'getLogs', 'clear'])
  21. ->getMock();
  22. $logger->expects($this->once())->method('countErrors')->willReturn('foo');
  23. $logger->expects($this->exactly(2))->method('getLogs')->willReturn([]);
  24. $c = new LoggerDataCollector($logger, __DIR__.'/');
  25. $c->lateCollect();
  26. $compilerLogs = $c->getCompilerLogs()->getValue('message');
  27. $this->assertSame([
  28. ['message' => 'Removed service "Psr\Container\ContainerInterface"; reason: private alias.'],
  29. ['message' => 'Removed service "Symfony\Component\DependencyInjection\ContainerInterface"; reason: private alias.'],
  30. ], $compilerLogs['Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass']);
  31. $this->assertSame([
  32. ['message' => 'Some custom logging message'],
  33. ['message' => 'With ending :'],
  34. ], $compilerLogs['Unknown Compiler Pass']);
  35. }
  36. /**
  37. * @dataProvider getCollectTestData
  38. */
  39. public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount, $expectedPriorities = null)
  40. {
  41. $logger = $this
  42. ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
  43. ->setMethods(['countErrors', 'getLogs', 'clear'])
  44. ->getMock();
  45. $logger->expects($this->once())->method('countErrors')->willReturn($nb);
  46. $logger->expects($this->exactly(2))->method('getLogs')->willReturn($logs);
  47. $c = new LoggerDataCollector($logger);
  48. $c->lateCollect();
  49. $this->assertEquals('logger', $c->getName());
  50. $this->assertEquals($nb, $c->countErrors());
  51. $logs = array_map(function ($v) {
  52. if (isset($v['context']['exception'])) {
  53. $e = &$v['context']['exception'];
  54. $e = isset($e["\0*\0message"]) ? [$e["\0*\0message"], $e["\0*\0severity"]] : [$e["\0Symfony\Component\Debug\Exception\SilencedErrorContext\0severity"]];
  55. }
  56. return $v;
  57. }, $c->getLogs()->getValue(true));
  58. $this->assertEquals($expectedLogs, $logs);
  59. $this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
  60. $this->assertEquals($expectedScreamCount, $c->countScreams());
  61. if (isset($expectedPriorities)) {
  62. $this->assertSame($expectedPriorities, $c->getPriorities()->getValue(true));
  63. }
  64. }
  65. public function testReset()
  66. {
  67. $logger = $this
  68. ->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')
  69. ->setMethods(['countErrors', 'getLogs', 'clear'])
  70. ->getMock();
  71. $logger->expects($this->once())->method('clear');
  72. $c = new LoggerDataCollector($logger);
  73. $c->reset();
  74. }
  75. public function getCollectTestData()
  76. {
  77. yield 'simple log' => [
  78. 1,
  79. [['message' => 'foo', 'context' => [], 'priority' => 100, 'priorityName' => 'DEBUG']],
  80. [['message' => 'foo', 'context' => [], 'priority' => 100, 'priorityName' => 'DEBUG']],
  81. 0,
  82. 0,
  83. ];
  84. yield 'log with a context' => [
  85. 1,
  86. [['message' => 'foo', 'context' => ['foo' => 'bar'], 'priority' => 100, 'priorityName' => 'DEBUG']],
  87. [['message' => 'foo', 'context' => ['foo' => 'bar'], 'priority' => 100, 'priorityName' => 'DEBUG']],
  88. 0,
  89. 0,
  90. ];
  91. if (!class_exists(SilencedErrorContext::class)) {
  92. return;
  93. }
  94. yield 'logs with some deprecations' => [
  95. 1,
  96. [
  97. ['message' => 'foo3', 'context' => ['exception' => new \ErrorException('warning', 0, E_USER_WARNING)], 'priority' => 100, 'priorityName' => 'DEBUG'],
  98. ['message' => 'foo', 'context' => ['exception' => new \ErrorException('deprecated', 0, E_DEPRECATED)], 'priority' => 100, 'priorityName' => 'DEBUG'],
  99. ['message' => 'foo2', 'context' => ['exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)], 'priority' => 100, 'priorityName' => 'DEBUG'],
  100. ],
  101. [
  102. ['message' => 'foo3', 'context' => ['exception' => ['warning', E_USER_WARNING]], 'priority' => 100, 'priorityName' => 'DEBUG'],
  103. ['message' => 'foo', 'context' => ['exception' => ['deprecated', E_DEPRECATED]], 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false],
  104. ['message' => 'foo2', 'context' => ['exception' => ['deprecated', E_USER_DEPRECATED]], 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false],
  105. ],
  106. 2,
  107. 0,
  108. [100 => ['count' => 3, 'name' => 'DEBUG']],
  109. ];
  110. yield 'logs with some silent errors' => [
  111. 1,
  112. [
  113. ['message' => 'foo3', 'context' => ['exception' => new \ErrorException('warning', 0, E_USER_WARNING)], 'priority' => 100, 'priorityName' => 'DEBUG'],
  114. ['message' => 'foo3', 'context' => ['exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)], 'priority' => 100, 'priorityName' => 'DEBUG'],
  115. ],
  116. [
  117. ['message' => 'foo3', 'context' => ['exception' => ['warning', E_USER_WARNING]], 'priority' => 100, 'priorityName' => 'DEBUG'],
  118. ['message' => 'foo3', 'context' => ['exception' => [E_USER_WARNING]], 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true],
  119. ],
  120. 0,
  121. 1,
  122. ];
  123. }
  124. }