LogmaticHandlerTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Level;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. /**
  14. * @author Julien Breux <julien.breux@gmail.com>
  15. */
  16. class LogmaticHandlerTest extends \Monolog\Test\MonologTestCase
  17. {
  18. /**
  19. * @var resource
  20. */
  21. private $res;
  22. private LogmaticHandler&MockObject $handler;
  23. public function tearDown(): void
  24. {
  25. parent::tearDown();
  26. unset($this->res);
  27. unset($this->handler);
  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->setValue($this->handler, 'localhost:1234');
  61. $this->handler->expects($this->any())
  62. ->method('fsockopen')
  63. ->willReturn($this->res);
  64. $this->handler->expects($this->any())
  65. ->method('streamSetTimeout')
  66. ->willReturn(true);
  67. $this->handler->expects($this->any())
  68. ->method('closeSocket');
  69. }
  70. }