LoggerDataCollectorTest.php 6.0 KB

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