InsightOpsHandlerTest.php 2.5 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\Test\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @author Robert Kaufmann III <rok3@rok3.me>
  15. * @author Gabriel Machado <gabriel.ms1@hotmail.com>
  16. */
  17. class InsightOpsHandlerTest extends TestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $resource;
  23. /**
  24. * @var LogEntriesHandler
  25. */
  26. private $handler;
  27. public function tearDown(): void
  28. {
  29. parent::tearDown();
  30. unset($this->resource);
  31. }
  32. public function testWriteContent()
  33. {
  34. $this->createHandler();
  35. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  36. fseek($this->resource, 0);
  37. $content = fread($this->resource, 1024);
  38. $this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
  39. }
  40. public function testWriteBatchContent()
  41. {
  42. $this->createHandler();
  43. $this->handler->handleBatch($this->getMultipleRecords());
  44. fseek($this->resource, 0);
  45. $content = fread($this->resource, 1024);
  46. $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
  47. }
  48. private function createHandler()
  49. {
  50. $useSSL = extension_loaded('openssl');
  51. $args = array('testToken', 'us', $useSSL, Logger::DEBUG, true);
  52. $this->resource = fopen('php://memory', 'a');
  53. $this->handler = $this->getMockBuilder(InsightOpsHandler::class)
  54. ->onlyMethods(array('fsockopen', 'streamSetTimeout', 'closeSocket'))
  55. ->setConstructorArgs($args)
  56. ->getMock();
  57. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  58. $reflectionProperty->setAccessible(true);
  59. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  60. $this->handler->expects($this->any())
  61. ->method('fsockopen')
  62. ->will($this->returnValue($this->resource));
  63. $this->handler->expects($this->any())
  64. ->method('streamSetTimeout')
  65. ->will($this->returnValue(true));
  66. $this->handler->expects($this->any())
  67. ->method('closeSocket')
  68. ->will($this->returnValue(true));
  69. }
  70. }