LogmaticHandlerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Handler;
  11. use Monolog\Test\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @author Julien Breux <julien.breux@gmail.com>
  15. */
  16. class LogmaticHandlerTest extends TestCase
  17. {
  18. /**
  19. * @var resource
  20. */
  21. private $res;
  22. /**
  23. * @var LogmaticHandler
  24. */
  25. private $handler;
  26. public function testWriteContent()
  27. {
  28. $this->initHandlerAndSocket();
  29. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  30. $content = $this->closeSocket();
  31. $this->assertRegexp('/testToken {"message":"Critical write test","context":\[\],"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname"}/', $content);
  32. }
  33. public function testWriteBatchContent()
  34. {
  35. $records = [
  36. $this->getRecord(),
  37. $this->getRecord(),
  38. $this->getRecord(),
  39. ];
  40. $this->initHandlerAndSocket();
  41. $this->handler->handleBatch($records);
  42. $content = $this->closeSocket();
  43. $this->assertRegexp('/testToken {"message":"test","context":\[\],"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname"}/', $content);
  44. }
  45. private function initHandlerAndSocket()
  46. {
  47. $tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
  48. file_put_contents($tmpFile, <<<'SCRIPT'
  49. <?php
  50. $sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  51. socket_bind($sock, '127.0.0.1', 51984);
  52. socket_listen($sock);
  53. while (true) {
  54. $res = socket_accept($sock);
  55. socket_set_option($res, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 500));
  56. while ($read = socket_read($res, 1024)) {
  57. echo $read;
  58. }
  59. socket_close($res);
  60. }
  61. SCRIPT
  62. );
  63. $this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
  64. $this->socket->start();
  65. $useSSL = extension_loaded('openssl');
  66. $this->handler = new LogmaticHandler('testToken', 'testHostname', 'testAppname', $useSSL, Logger::DEBUG, true);
  67. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  68. $reflectionProperty->setAccessible(true);
  69. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  70. }
  71. private function closeSocket()
  72. {
  73. $this->socket->stop();
  74. return $this->socket->getOutput();
  75. }
  76. public function tearDown()
  77. {
  78. if (isset($this->socket)) {
  79. $this->closeSocket();
  80. unset($this->socket);
  81. }
  82. }
  83. }