LoggerDataCollectorTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. use Symfony\Component\VarDumper\Cloner\Data;
  15. class LoggerDataCollectorTest extends TestCase
  16. {
  17. private static $data;
  18. /**
  19. * @dataProvider getCollectTestData
  20. */
  21. public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount, $expectedPriorities = null)
  22. {
  23. $logger = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\DebugLoggerInterface')->getMock();
  24. $logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
  25. $logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
  26. // disable cloning the context, to ease fixtures creation.
  27. $c = $this->getMockBuilder(LoggerDataCollector::class)
  28. ->setMethods(array('cloneVar'))
  29. ->setConstructorArgs(array($logger))
  30. ->getMock();
  31. $c->expects($this->any())->method('cloneVar')->willReturn(self::$data);
  32. $c->lateCollect();
  33. $this->assertEquals('logger', $c->getName());
  34. $this->assertEquals($nb, $c->countErrors());
  35. $this->assertEquals($expectedLogs, $c->getLogs());
  36. $this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
  37. $this->assertEquals($expectedScreamCount, $c->countScreams());
  38. if (isset($expectedPriorities)) {
  39. $this->assertSame($expectedPriorities, $c->getPriorities());
  40. }
  41. }
  42. public function getCollectTestData()
  43. {
  44. if (null === self::$data) {
  45. self::$data = new Data(array());
  46. }
  47. yield 'simple log' => array(
  48. 1,
  49. array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
  50. array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
  51. 0,
  52. 0,
  53. );
  54. yield 'log with a context' => array(
  55. 1,
  56. array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
  57. array(array('message' => 'foo', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG')),
  58. 0,
  59. 0,
  60. );
  61. if (!class_exists(SilencedErrorContext::class)) {
  62. return;
  63. }
  64. yield 'logs with some deprecations' => array(
  65. 1,
  66. array(
  67. array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  68. array('message' => 'foo', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  69. array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  70. ),
  71. array(
  72. array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG'),
  73. array('message' => 'foo', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
  74. array('message' => 'foo2', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
  75. ),
  76. 2,
  77. 0,
  78. array(100 => array('count' => 3, 'name' => 'DEBUG')),
  79. );
  80. yield 'logs with some silent errors' => array(
  81. 1,
  82. array(
  83. array('message' => 'foo3', 'context' => array('exception' => new \ErrorException('warning', 0, E_USER_WARNING)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  84. array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
  85. ),
  86. array(
  87. array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG'),
  88. array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true),
  89. ),
  90. 0,
  91. 1,
  92. );
  93. }
  94. }