TestHandler.php 6.3 KB

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