NoticeTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Nekonomokochan\Tests\Logger;
  3. use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Class NoticeTest
  7. *
  8. * @package Nekonomokochan\Tests\Logger
  9. * @see \Nekonomokochan\PhpJsonLogger\Logger::notice
  10. */
  11. class NoticeTest extends TestCase
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $outputFileBaseName;
  17. /**
  18. * @var string
  19. */
  20. private $outputFileName;
  21. /**
  22. * Delete the log file used last time to test the contents of the log file
  23. */
  24. public function setUp()
  25. {
  26. parent::setUp();
  27. $this->outputFileBaseName = '/tmp/notice-log-test.log';
  28. $this->outputFileName = '/tmp/notice-log-test-' . date('Y-m-d') . '.log';
  29. if (file_exists($this->outputFileName)) {
  30. unlink($this->outputFileName);
  31. }
  32. }
  33. /**
  34. * @test
  35. * @throws \Exception
  36. */
  37. public function outputNoticeLog()
  38. {
  39. $context = [
  40. 'title' => 'Test',
  41. ];
  42. $loggerBuilder = new LoggerBuilder();
  43. $loggerBuilder->setFileName($this->outputFileBaseName);
  44. $loggerBuilder->setLogLevel(LoggerBuilder::DEBUG);
  45. $logger = $loggerBuilder->build();
  46. $logger->notice('🐶', $context);
  47. $resultJson = file_get_contents($this->outputFileName);
  48. $resultArray = json_decode($resultJson, true);
  49. echo "\n ---- Output Log Begin ---- \n";
  50. echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  51. echo "\n ---- Output Log End ---- \n";
  52. $expectedLog = [
  53. 'log_level' => 'NOTICE',
  54. 'message' => '🐶',
  55. 'channel' => 'PhpJsonLogger',
  56. 'trace_id' => $logger->getTraceId(),
  57. 'file' => __FILE__,
  58. 'line' => 53,
  59. 'context' => $context,
  60. 'remote_ip_address' => '127.0.0.1',
  61. 'server_ip_address' => '127.0.0.1',
  62. 'user_agent' => 'unknown',
  63. 'datetime' => $resultArray['datetime'],
  64. 'timezone' => date_default_timezone_get(),
  65. 'process_time' => $resultArray['process_time'],
  66. ];
  67. $this->assertSame('PhpJsonLogger', $logger->getChannel());
  68. $this->assertSame($expectedLog, $resultArray);
  69. }
  70. }