TestHandler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php declare(strict_types=1);
  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 = [];
  66. protected $recordsByLevel = [];
  67. public function getRecords()
  68. {
  69. return $this->records;
  70. }
  71. public function clear()
  72. {
  73. $this->records = [];
  74. $this->recordsByLevel = [];
  75. }
  76. public function hasRecords($level)
  77. {
  78. return isset($this->recordsByLevel[$level]);
  79. }
  80. /**
  81. * @param string|array $record Either a message string or an array containing message and optionally context keys that will be checked against all records
  82. * @param int $level Logger::LEVEL constant value
  83. */
  84. public function hasRecord($record, $level)
  85. {
  86. if (is_string($record)) {
  87. $record = array('message' => $record);
  88. }
  89. return $this->hasRecordThatPasses(function ($rec) use ($record) {
  90. if ($rec['message'] !== $record['message']) {
  91. return false;
  92. }
  93. if (isset($record['context']) && $rec['context'] !== $record['context']) {
  94. return false;
  95. }
  96. return true;
  97. }, $level);
  98. }
  99. public function hasRecordThatContains($message, $level)
  100. {
  101. return $this->hasRecordThatPasses(function ($rec) use ($message) {
  102. return strpos($rec['message'], $message) !== false;
  103. }, $level);
  104. }
  105. public function hasRecordThatMatches($regex, $level)
  106. {
  107. return $this->hasRecordThatPasses(function ($rec) use ($regex) {
  108. return preg_match($regex, $rec['message']) > 0;
  109. }, $level);
  110. }
  111. public function hasRecordThatPasses(callable $predicate, $level)
  112. {
  113. if (!isset($this->recordsByLevel[$level])) {
  114. return false;
  115. }
  116. foreach ($this->recordsByLevel[$level] as $i => $rec) {
  117. if (call_user_func($predicate, $rec, $i)) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. protected function write(array $record): void
  127. {
  128. $this->recordsByLevel[$record['level']][] = $record;
  129. $this->records[] = $record;
  130. }
  131. public function __call($method, $args)
  132. {
  133. if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
  134. $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3];
  135. $level = constant('Monolog\Logger::' . strtoupper($matches[2]));
  136. if (method_exists($this, $genericMethod)) {
  137. $args[] = $level;
  138. return call_user_func_array([$this, $genericMethod], $args);
  139. }
  140. }
  141. throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
  142. }
  143. }