LogEntriesHandlerTest.php 2.6 KB

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