InsightOpsHandlerTest.php 2.5 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\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. unset($this->handler);
  30. }
  31. public function testWriteContent()
  32. {
  33. $this->createHandler();
  34. $this->handler->handle($this->getRecord(Level::Critical, 'Critical write test'));
  35. fseek($this->resource, 0);
  36. $content = fread($this->resource, 1024);
  37. $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);
  38. }
  39. public function testWriteBatchContent()
  40. {
  41. $this->createHandler();
  42. $this->handler->handleBatch($this->getMultipleRecords());
  43. fseek($this->resource, 0);
  44. $content = fread($this->resource, 1024);
  45. $this->assertMatchesRegularExpression('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
  46. }
  47. private function createHandler()
  48. {
  49. $useSSL = \extension_loaded('openssl');
  50. $args = ['testToken', 'us', $useSSL, Level::Debug, true];
  51. $this->resource = fopen('php://memory', 'a');
  52. $this->handler = $this->getMockBuilder(InsightOpsHandler::class)
  53. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  54. ->setConstructorArgs($args)
  55. ->getMock();
  56. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  57. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  58. $this->handler->expects($this->any())
  59. ->method('fsockopen')
  60. ->willReturn($this->resource);
  61. $this->handler->expects($this->any())
  62. ->method('streamSetTimeout')
  63. ->willReturn(true);
  64. $this->handler->expects($this->any())
  65. ->method('closeSocket');
  66. }
  67. }