LogmaticHandlerTest.php 2.7 KB

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