Bez popisu

LoggerDataCollectorTest.php 4.7KB

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