LogEntriesHandlerTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. use Monolog\Util\LocalSocket;
  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. /**
  24. * @var LogEntriesHandler
  25. */
  26. private $handler;
  27. public function testWriteContent()
  28. {
  29. $this->initHandlerAndSocket();
  30. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  31. $content = $this->socket->getOutput();
  32. $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);
  33. }
  34. public function testWriteBatchContent()
  35. {
  36. $records = [
  37. $this->getRecord(),
  38. $this->getRecord(),
  39. $this->getRecord(),
  40. ];
  41. $this->initHandlerAndSocket();
  42. $this->handler->handleBatch($records);
  43. $content = $this->socket->getOutput();
  44. $this->assertRegexp('/(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 initHandlerAndSocket()
  47. {
  48. $this->socket = LocalSocket::initSocket();
  49. $useSSL = extension_loaded('openssl');
  50. $this->handler = new LogEntriesHandler('testToken', $useSSL, Logger::DEBUG, true);
  51. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  52. $reflectionProperty->setAccessible(true);
  53. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  54. }
  55. public function tearDown()
  56. {
  57. unset($this->socket, $this->handler);
  58. }
  59. }