TestHandler.php 6.4 KB

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