InsightOpsHandlerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Robert Kaufmann III <rok3@rok3.me>
  15. * @author Gabriel Machado <gabriel.ms1@hotmail.com>
  16. */
  17. class InsightOpsHandlerTest extends \Monolog\Test\MonologTestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $resource;
  23. private InsightOpsHandler&MockObject $handler;
  24. public function tearDown(): void
  25. {
  26. parent::tearDown();
  27. unset($this->resource);
  28. unset($this->handler);
  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->setValue($this->handler, 'localhost:1234');
  57. $this->handler->expects($this->any())
  58. ->method('fsockopen')
  59. ->willReturn($this->resource);
  60. $this->handler->expects($this->any())
  61. ->method('streamSetTimeout')
  62. ->willReturn(true);
  63. $this->handler->expects($this->any())
  64. ->method('closeSocket');
  65. }
  66. }