2
0

LogmaticHandlerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 tearDown(): void
  27. {
  28. parent::tearDown();
  29. unset($this->res);
  30. }
  31. public function testWriteContent()
  32. {
  33. $this->createHandler();
  34. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  35. fseek($this->res, 0);
  36. $content = fread($this->res, 1024);
  37. $this->assertRegexp('/testToken {"message":"Critical write test","context":{},"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
  38. }
  39. public function testWriteBatchContent()
  40. {
  41. $records = [
  42. $this->getRecord(),
  43. $this->getRecord(),
  44. $this->getRecord(),
  45. ];
  46. $this->createHandler();
  47. $this->handler->handleBatch($records);
  48. fseek($this->res, 0);
  49. $content = fread($this->res, 1024);
  50. $this->assertRegexp('/testToken {"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
  51. }
  52. private function createHandler()
  53. {
  54. $useSSL = extension_loaded('openssl');
  55. $args = ['testToken', 'testHostname', 'testAppname', $useSSL, Logger::DEBUG, true];
  56. $this->res = fopen('php://memory', 'a');
  57. $this->handler = $this->getMockBuilder('Monolog\Handler\LogmaticHandler')
  58. ->setConstructorArgs($args)
  59. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  60. ->getMock();
  61. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  62. $reflectionProperty->setAccessible(true);
  63. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  64. $this->handler->expects($this->any())
  65. ->method('fsockopen')
  66. ->will($this->returnValue($this->res));
  67. $this->handler->expects($this->any())
  68. ->method('streamSetTimeout')
  69. ->will($this->returnValue(true));
  70. $this->handler->expects($this->any())
  71. ->method('closeSocket')
  72. ->will($this->returnValue(true));
  73. }
  74. }