ErrorTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Nekonomokochan\Tests\Logger;
  3. use Nekonomokochan\PhpJsonLogger\InvalidArgumentException;
  4. use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * Class ErrorTest
  8. *
  9. * @package Nekonomokochan\Tests\Logger
  10. * @see \Nekonomokochan\PhpJsonLogger\Logger::error
  11. */
  12. class ErrorTest extends TestCase
  13. {
  14. /**
  15. * @var string
  16. */
  17. private $outputFileBaseName;
  18. /**
  19. * @var string
  20. */
  21. private $outputFileName;
  22. /**
  23. * Delete the log file used last time to test the contents of the log file
  24. */
  25. public function setUp()
  26. {
  27. parent::setUp();
  28. $this->outputFileBaseName = '/tmp/error-log-test.log';
  29. $this->outputFileName = '/tmp/error-log-test-' . date('Y-m-d') . '.log';
  30. if (file_exists($this->outputFileName)) {
  31. unlink($this->outputFileName);
  32. }
  33. }
  34. /**
  35. * @test
  36. */
  37. public function outputErrorLog()
  38. {
  39. $exception = new \Exception('TestException', 500);
  40. $context = [
  41. 'name' => 'keitakn',
  42. 'email' => 'dummy@email.com',
  43. ];
  44. $loggerBuilder = new LoggerBuilder();
  45. $loggerBuilder->setFileName($this->outputFileBaseName);
  46. $logger = $loggerBuilder->build();
  47. $logger->error($exception, $context);
  48. $resultJson = file_get_contents($this->outputFileName);
  49. $resultArray = json_decode($resultJson, true);
  50. echo "\n ---- Output Log Begin ---- \n";
  51. echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  52. echo "\n ---- Output Log End ---- \n";
  53. $expectedLog = [
  54. 'log_level' => 'ERROR',
  55. 'message' => 'Exception',
  56. 'channel' => 'PhpJsonLogger',
  57. 'trace_id' => $logger->getTraceId(),
  58. 'file' => __FILE__,
  59. 'line' => 54,
  60. 'context' => $context,
  61. 'remote_ip_address' => '127.0.0.1',
  62. 'server_ip_address' => '127.0.0.1',
  63. 'user_agent' => 'unknown',
  64. 'datetime' => $resultArray['datetime'],
  65. 'timezone' => date_default_timezone_get(),
  66. 'process_time' => $resultArray['process_time'],
  67. 'errors' => [
  68. 'message' => 'TestException',
  69. 'code' => 500,
  70. 'file' => __FILE__,
  71. 'line' => 45,
  72. 'trace' => $resultArray['errors']['trace'],
  73. ],
  74. ];
  75. $this->assertSame('PhpJsonLogger', $logger->getChannel());
  76. $this->assertSame($expectedLog, $resultArray);
  77. }
  78. /**
  79. * @test
  80. * @expectedException InvalidArgumentException
  81. * @expectedExceptionMessage Please give the exception class to the Nekonomokochan\PhpJsonLogger\Logger::error
  82. */
  83. public function invalidArgumentException()
  84. {
  85. $message = '';
  86. $context = [
  87. 'name' => 'keitakn',
  88. 'email' => 'dummy@email.com',
  89. ];
  90. $loggerBuilder = new LoggerBuilder();
  91. $loggerBuilder->setFileName($this->outputFileBaseName);
  92. $logger = $loggerBuilder->build();
  93. $logger->error($message, $context);
  94. }
  95. }