You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.4 KiB

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