LogmaticHandlerTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. use Monolog\Util\LocalSocket;
  14. /**
  15. * @author Julien Breux <julien.breux@gmail.com>
  16. */
  17. class LogmaticHandlerTest extends TestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $res;
  23. /**
  24. * @var LogmaticHandler
  25. */
  26. private $handler;
  27. public function testWriteContent()
  28. {
  29. $this->initHandlerAndSocket();
  30. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  31. $content = $this->socket->getOutput();
  32. $this->assertRegexp('/testToken {"message":"Critical write test","context":\[\],"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname"}/', $content);
  33. }
  34. public function testWriteBatchContent()
  35. {
  36. $records = [
  37. $this->getRecord(),
  38. $this->getRecord(),
  39. $this->getRecord(),
  40. ];
  41. $this->initHandlerAndSocket();
  42. $this->handler->handleBatch($records);
  43. $content = $this->socket->getOutput();
  44. $this->assertRegexp('/testToken {"message":"test","context":\[\],"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname"}/', $content);
  45. }
  46. private function initHandlerAndSocket()
  47. {
  48. $this->socket = LocalSocket::initSocket();
  49. $useSSL = extension_loaded('openssl');
  50. $this->handler = new LogmaticHandler('testToken', 'testHostname', 'testAppname', $useSSL, Logger::DEBUG, true);
  51. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  52. $reflectionProperty->setAccessible(true);
  53. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  54. }
  55. public function tearDown()
  56. {
  57. unset($this->socket, $this->handler);
  58. }
  59. }