InsightOpsHandlerTest.php 2.6 KB

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