WhatFailureGroupHandlerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Handler;
  11. use Monolog\TestCase;
  12. use Monolog\Logger;
  13. class WhatFailureGroupHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\WhatFailureGroupHandler::__construct
  17. * @expectedException InvalidArgumentException
  18. */
  19. public function testConstructorOnlyTakesHandler()
  20. {
  21. new WhatFailureGroupHandler(array(new TestHandler(), "foo"));
  22. }
  23. /**
  24. * @covers Monolog\Handler\WhatFailureGroupHandler::__construct
  25. * @covers Monolog\Handler\WhatFailureGroupHandler::handle
  26. */
  27. public function testHandle()
  28. {
  29. $testHandlers = array(new TestHandler(), new TestHandler());
  30. $handler = new WhatFailureGroupHandler($testHandlers);
  31. $handler->handle($this->getRecord(Logger::DEBUG));
  32. $handler->handle($this->getRecord(Logger::INFO));
  33. foreach ($testHandlers as $test) {
  34. $this->assertTrue($test->hasDebugRecords());
  35. $this->assertTrue($test->hasInfoRecords());
  36. $this->assertTrue(count($test->getRecords()) === 2);
  37. }
  38. }
  39. /**
  40. * @covers Monolog\Handler\WhatFailureGroupHandler::handleBatch
  41. */
  42. public function testHandleBatch()
  43. {
  44. $testHandlers = array(new TestHandler(), new TestHandler());
  45. $handler = new WhatFailureGroupHandler($testHandlers);
  46. $handler->handleBatch(array($this->getRecord(Logger::DEBUG), $this->getRecord(Logger::INFO)));
  47. foreach ($testHandlers as $test) {
  48. $this->assertTrue($test->hasDebugRecords());
  49. $this->assertTrue($test->hasInfoRecords());
  50. $this->assertTrue(count($test->getRecords()) === 2);
  51. }
  52. }
  53. /**
  54. * @covers Monolog\Handler\WhatFailureGroupHandler::isHandling
  55. */
  56. public function testIsHandling()
  57. {
  58. $testHandlers = array(new TestHandler(Logger::ERROR), new TestHandler(Logger::WARNING));
  59. $handler = new WhatFailureGroupHandler($testHandlers);
  60. $this->assertTrue($handler->isHandling($this->getRecord(Logger::ERROR)));
  61. $this->assertTrue($handler->isHandling($this->getRecord(Logger::WARNING)));
  62. $this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG)));
  63. }
  64. /**
  65. * @covers Monolog\Handler\WhatFailureGroupHandler::handle
  66. */
  67. public function testHandleUsesProcessors()
  68. {
  69. $test = new TestHandler();
  70. $handler = new WhatFailureGroupHandler(array($test));
  71. $handler->pushProcessor(function ($record) {
  72. $record['extra']['foo'] = true;
  73. return $record;
  74. });
  75. $handler->handle($this->getRecord(Logger::WARNING));
  76. $this->assertTrue($test->hasWarningRecords());
  77. $records = $test->getRecords();
  78. $this->assertTrue($records[0]['extra']['foo']);
  79. }
  80. /**
  81. * @covers Monolog\Handler\WhatFailureGroupHandler::handleBatch
  82. */
  83. public function testHandleBatchUsesProcessors()
  84. {
  85. $testHandlers = array(new TestHandler(), new TestHandler());
  86. $handler = new WhatFailureGroupHandler($testHandlers);
  87. $handler->pushProcessor(function ($record) {
  88. $record['extra']['foo'] = true;
  89. return $record;
  90. });
  91. $handler->handleBatch(array($this->getRecord(Logger::DEBUG), $this->getRecord(Logger::INFO)));
  92. foreach ($testHandlers as $test) {
  93. $this->assertTrue($test->hasDebugRecords());
  94. $this->assertTrue($test->hasInfoRecords());
  95. $this->assertTrue(count($test->getRecords()) === 2);
  96. $records = $test->getRecords();
  97. $this->assertTrue($records[0]['extra']['foo']);
  98. $this->assertTrue($records[1]['extra']['foo']);
  99. }
  100. }
  101. /**
  102. * @covers Monolog\Handler\WhatFailureGroupHandler::handle
  103. */
  104. public function testHandleException()
  105. {
  106. $test = new TestHandler();
  107. $exception = new ExceptionTestHandler();
  108. $handler = new WhatFailureGroupHandler(array($exception, $test, $exception));
  109. $handler->pushProcessor(function ($record) {
  110. $record['extra']['foo'] = true;
  111. return $record;
  112. });
  113. $handler->handle($this->getRecord(Logger::WARNING));
  114. $this->assertTrue($test->hasWarningRecords());
  115. $records = $test->getRecords();
  116. $this->assertTrue($records[0]['extra']['foo']);
  117. }
  118. }
  119. class ExceptionTestHandler extends TestHandler
  120. {
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function handle(array $record)
  125. {
  126. parent::handle($record);
  127. throw new \Exception("ExceptionTestHandler::handle");
  128. }
  129. }