LogmaticHandlerTest.php 2.8 KB

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