TestHandler.php 5.6 KB

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