2
0

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. }
  29. public function testWriteContent()
  30. {
  31. $this->createHandler();
  32. $this->handler->handle($this->getRecord(Level::Critical, 'Critical write test'));
  33. fseek($this->res, 0);
  34. $content = fread($this->res, 1024);
  35. $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);
  36. }
  37. public function testWriteBatchContent()
  38. {
  39. $records = [
  40. $this->getRecord(),
  41. $this->getRecord(),
  42. $this->getRecord(),
  43. ];
  44. $this->createHandler();
  45. $this->handler->handleBatch($records);
  46. fseek($this->res, 0);
  47. $content = fread($this->res, 1024);
  48. $this->assertMatchesRegularExpression('/testToken {"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
  49. }
  50. private function createHandler()
  51. {
  52. $useSSL = \extension_loaded('openssl');
  53. $args = ['testToken', 'testHostname', 'testAppname', $useSSL, Level::Debug, true];
  54. $this->res = fopen('php://memory', 'a');
  55. $this->handler = $this->getMockBuilder('Monolog\Handler\LogmaticHandler')
  56. ->setConstructorArgs($args)
  57. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  58. ->getMock();
  59. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  60. $reflectionProperty->setAccessible(true);
  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. }