LoggerDataCollectorTest.php 6.5 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(array('countErrors', 'getLogs', 'clear'))
  21. ->getMock();
  22. $logger->expects($this->once())->method('countErrors')->will($this->returnValue('foo'));
  23. $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue(array()));
  24. $c = new LoggerDataCollector($logger, __DIR__.'/');
  25. $c->lateCollect();
  26. $compilerLogs = $c->getCompilerLogs()->getValue('message');
  27. $this->assertSame(array(
  28. array('message' => 'Removed service "Psr\Container\ContainerInterface"; reason: private alias.'),
  29. array('message' => 'Removed service "Symfony\Component\DependencyInjection\ContainerInterface"; reason: private alias.'),
  30. ), $compilerLogs['Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass']);
  31. $this->assertSame(array(
  32. array('message' => 'Some custom logging message'),
  33. array('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(array('countErrors', 'getLogs', 'clear'))
  44. ->getMock();
  45. $logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
  46. $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($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"]) ? array($e["\0*\0message"], $e["\0*\0severity"]) : array($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(array('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' => array(
  78. 1,
  79. array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
  80. array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
  81. 0,
  82. 0,
  83. );
  84. yield 'log with a context' => array(
  85. 1,
  86. array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
  87. array(array('message' => 'foo', 'context' => array('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' => array(
  95. 1,
  96. array(
  97. array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  98. array('message' => 'foo', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  99. array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  100. ),
  101. array(
  102. array('message' => 'foo3', 'context' => array('exception' => array('warning', E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  103. array('message' => 'foo', 'context' => array('exception' => array('deprecated', E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
  104. array('message' => 'foo2', 'context' => array('exception' => array('deprecated', E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
  105. ),
  106. 2,
  107. 0,
  108. array(100 => array('count' => 3, 'name' => 'DEBUG')),
  109. );
  110. yield 'logs with some silent errors' => array(
  111. 1,
  112. array(
  113. array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  114. array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  115. ),
  116. array(
  117. array('message' => 'foo3', 'context' => array('exception' => array('warning', E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  118. array('message' => 'foo3', 'context' => array('exception' => array(E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true),
  119. ),
  120. 0,
  121. 1,
  122. );
  123. }
  124. }