TestCase.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Test;
  11. use Monolog\Logger;
  12. use Monolog\DateTimeImmutable;
  13. use Monolog\Formatter\FormatterInterface;
  14. /**
  15. * Lets you easily generate log records and a dummy formatter for testing purposes
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. *
  19. * @phpstan-import-type Record from \Monolog\Logger
  20. * @phpstan-import-type Level from \Monolog\Logger
  21. */
  22. class TestCase extends \PHPUnit\Framework\TestCase
  23. {
  24. public function tearDown(): void
  25. {
  26. parent::tearDown();
  27. if (isset($this->handler)) {
  28. unset($this->handler);
  29. }
  30. }
  31. /**
  32. * @param mixed[] $context
  33. *
  34. * @return array Record
  35. *
  36. * @phpstan-param Level $level
  37. * @phpstan-return Record
  38. */
  39. protected function getRecord(int $level = Logger::WARNING, string $message = 'test', array $context = []): array
  40. {
  41. return [
  42. 'message' => (string) $message,
  43. 'context' => $context,
  44. 'level' => $level,
  45. 'level_name' => Logger::getLevelName($level),
  46. 'channel' => 'test',
  47. 'datetime' => new DateTimeImmutable(true),
  48. 'extra' => [],
  49. ];
  50. }
  51. /**
  52. * @phpstan-return Record[]
  53. */
  54. protected function getMultipleRecords(): array
  55. {
  56. return [
  57. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  58. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  59. $this->getRecord(Logger::INFO, 'information'),
  60. $this->getRecord(Logger::WARNING, 'warning'),
  61. $this->getRecord(Logger::ERROR, 'error'),
  62. ];
  63. }
  64. protected function getIdentityFormatter(): FormatterInterface
  65. {
  66. $formatter = $this->createMock(FormatterInterface::class);
  67. $formatter->expects($this->any())
  68. ->method('format')
  69. ->will($this->returnCallback(function ($record) {
  70. return $record['message'];
  71. }));
  72. return $formatter;
  73. }
  74. }