SlackNotificationTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Nekonomokochan\Tests\Logger;
  3. use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
  4. use Nekonomokochan\PhpJsonLogger\SlackHandlerBuilder;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * Class SlackNotificationTest
  8. *
  9. * @package Nekonomokochan\Tests\Logger
  10. */
  11. class SlackNotificationTest 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/slack-log-test.log';
  28. $this->outputFileName = '/tmp/slack-log-test-' . date('Y-m-d') . '.log';
  29. if (file_exists($this->outputFileName)) {
  30. unlink($this->outputFileName);
  31. }
  32. }
  33. /**
  34. * @test
  35. */
  36. public function notificationToSlack()
  37. {
  38. $exception = new \Exception('TestException', 500);
  39. $context = [
  40. 'name' => 'keitakn',
  41. 'email' => 'dummy@email.com',
  42. ];
  43. $slackToken = getenv('PHP_JSON_LOGGER_SLACK_TOKEN', true) ?: getenv('PHP_JSON_LOGGER_SLACK_TOKEN');
  44. $slackChannel = getenv('PHP_JSON_LOGGER_SLACK_CHANNEL', true) ?: getenv('PHP_JSON_LOGGER_SLACK_CHANNEL');
  45. $slackHandlerBuilder = new SlackHandlerBuilder($slackToken, $slackChannel);
  46. $slackHandlerBuilder->setLevel(LoggerBuilder::CRITICAL);
  47. $_SERVER['REQUEST_URI'] = '/tests/notifications';
  48. $_SERVER['REMOTE_ADDR'] = '192.168.10.10';
  49. $_SERVER['REQUEST_METHOD'] = 'POST';
  50. $_SERVER['SERVER_NAME'] = 'cat-moko.localhost';
  51. $_SERVER['HTTP_REFERER'] = 'https://github.com/nekonomokochan/php-json-logger/issues/50';
  52. $_SERVER['SERVER_ADDR'] = '10.0.0.11';
  53. $_SERVER['HTTP_USER_AGENT'] = 'Chrome';
  54. $loggerBuilder = new LoggerBuilder();
  55. $loggerBuilder->setFileName($this->outputFileBaseName);
  56. $loggerBuilder->setSlackHandler($slackHandlerBuilder->build());
  57. $logger = $loggerBuilder->build();
  58. $logger->critical($exception, $context);
  59. unset($_SERVER['REQUEST_URI']);
  60. unset($_SERVER['REMOTE_ADDR']);
  61. unset($_SERVER['REQUEST_METHOD']);
  62. unset($_SERVER['SERVER_NAME']);
  63. unset($_SERVER['HTTP_REFERER']);
  64. unset($_SERVER['SERVER_ADDR']);
  65. unset($_SERVER['HTTP_USER_AGENT']);
  66. $resultJson = file_get_contents($this->outputFileName);
  67. $resultArray = json_decode($resultJson, true);
  68. echo "\n ---- Output Log Begin ---- \n";
  69. echo json_encode($resultArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  70. echo "\n ---- Output Log End ---- \n";
  71. $expectedLog = [
  72. 'log_level' => 'CRITICAL',
  73. 'message' => 'Exception',
  74. 'channel' => 'PhpJsonLogger',
  75. 'trace_id' => $logger->getTraceId(),
  76. 'file' => __FILE__,
  77. 'line' => 68,
  78. 'context' => $context,
  79. 'remote_ip_address' => '192.168.10.10',
  80. 'server_ip_address' => '10.0.0.11',
  81. 'user_agent' => 'Chrome',
  82. 'datetime' => $resultArray['datetime'],
  83. 'timezone' => date_default_timezone_get(),
  84. 'process_time' => $resultArray['process_time'],
  85. 'errors' => [
  86. 'message' => 'TestException',
  87. 'code' => 500,
  88. 'file' => __FILE__,
  89. 'line' => 44,
  90. 'trace' => $resultArray['errors']['trace'],
  91. ],
  92. ];
  93. $this->assertSame('PhpJsonLogger', $logger->getChannel());
  94. $this->assertSame($expectedLog, $resultArray);
  95. }
  96. }