TestHandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /**
  12. * Used for testing purposes.
  13. *
  14. * It records all records and gives you access to them for verification.
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. *
  18. * @method bool hasEmergency($record)
  19. * @method bool hasAlert($record)
  20. * @method bool hasCritical($record)
  21. * @method bool hasError($record)
  22. * @method bool hasWarning($record)
  23. * @method bool hasNotice($record)
  24. * @method bool hasInfo($record)
  25. * @method bool hasDebug($record)
  26. *
  27. * @method bool hasEmergencyRecords()
  28. * @method bool hasAlertRecords()
  29. * @method bool hasCriticalRecords()
  30. * @method bool hasErrorRecords()
  31. * @method bool hasWarningRecords()
  32. * @method bool hasNoticeRecords()
  33. * @method bool hasInfoRecords()
  34. * @method bool hasDebugRecords()
  35. *
  36. * @method bool hasEmergencyThatContains($message)
  37. * @method bool hasAlertThatContains($message)
  38. * @method bool hasCriticalThatContains($message)
  39. * @method bool hasErrorThatContains($message)
  40. * @method bool hasWarningThatContains($message)
  41. * @method bool hasNoticeThatContains($message)
  42. * @method bool hasInfoThatContains($message)
  43. * @method bool hasDebugThatContains($message)
  44. *
  45. * @method bool hasEmergencyThatMatches($message)
  46. * @method bool hasAlertThatMatches($message)
  47. * @method bool hasCriticalThatMatches($message)
  48. * @method bool hasErrorThatMatches($message)
  49. * @method bool hasWarningThatMatches($message)
  50. * @method bool hasNoticeThatMatches($message)
  51. * @method bool hasInfoThatMatches($message)
  52. * @method bool hasDebugThatMatches($message)
  53. *
  54. * @method bool hasEmergencyThatPasses($message)
  55. * @method bool hasAlertThatPasses($message)
  56. * @method bool hasCriticalThatPasses($message)
  57. * @method bool hasErrorThatPasses($message)
  58. * @method bool hasWarningThatPasses($message)
  59. * @method bool hasNoticeThatPasses($message)
  60. * @method bool hasInfoThatPasses($message)
  61. * @method bool hasDebugThatPasses($message)
  62. */
  63. class TestHandler extends AbstractProcessingHandler
  64. {
  65. protected $records = array();
  66. protected $recordsByLevel = array();
  67. public function getRecords()
  68. {
  69. return $this->records;
  70. }
  71. public function clear()
  72. {
  73. $this->records = array();
  74. $this->recordsByLevel = array();
  75. }
  76. public function hasRecords($level)
  77. {
  78. return isset($this->recordsByLevel[$level]);
  79. }
  80. public function hasRecord($record, $level)
  81. {
  82. if (is_array($record)) {
  83. $record = $record['message'];
  84. }
  85. return $this->hasRecordThatPasses(function ($rec) use ($record) {
  86. return $rec['message'] === $record;
  87. }, $level);
  88. }
  89. public function hasRecordThatContains($message, $level)
  90. {
  91. return $this->hasRecordThatPasses(function ($rec) use ($message) {
  92. return strpos($rec['message'], $message) !== false;
  93. }, $level);
  94. }
  95. public function hasRecordThatMatches($regex, $level)
  96. {
  97. return $this->hasRecordThatPasses(function ($rec) use ($regex) {
  98. return preg_match($regex, $rec['message']) > 0;
  99. }, $level);
  100. }
  101. public function hasRecordThatPasses($predicate, $level)
  102. {
  103. if (!is_callable($predicate)) {
  104. throw new \InvalidArgumentException("Expected a callable for hasRecordThatSucceeds");
  105. }
  106. if (!isset($this->recordsByLevel[$level])) {
  107. return false;
  108. }
  109. foreach ($this->recordsByLevel[$level] as $i => $rec) {
  110. if (call_user_func($predicate, $rec, $i)) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. protected function write(array $record)
  120. {
  121. $this->recordsByLevel[$record['level']][] = $record;
  122. $this->records[] = $record;
  123. }
  124. public function __call($method, $args)
  125. {
  126. if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
  127. $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3];
  128. $level = constant('Monolog\Logger::' . strtoupper($matches[2]));
  129. if (method_exists($this, $genericMethod)) {
  130. $args[] = $level;
  131. return call_user_func_array(array($this, $genericMethod), $args);
  132. }
  133. }
  134. throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
  135. }
  136. }