LogEntriesHandlerTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  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. /**
  14. * @author Robert Kaufmann III <rok3@rok3.me>
  15. */
  16. class LogEntriesHandlerTest extends TestCase
  17. {
  18. /**
  19. * @var resource
  20. */
  21. private $res;
  22. /**
  23. * @var LogEntriesHandler
  24. */
  25. private $handler;
  26. public function testWriteContent()
  27. {
  28. $this->createHandler();
  29. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  30. fseek($this->res, 0);
  31. $content = fread($this->res, 1024);
  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 = array(
  37. $this->getRecord(),
  38. $this->getRecord(),
  39. $this->getRecord(),
  40. );
  41. $this->createHandler();
  42. $this->handler->handleBatch($records);
  43. fseek($this->res, 0);
  44. $content = fread($this->res, 1024);
  45. $this->assertRegexp('/(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 = array('testToken', $useSSL, Logger::DEBUG, true);
  51. $this->res = fopen('php://memory', 'a');
  52. $this->handler = $this->getMock(
  53. '\Monolog\Handler\LogEntriesHandler',
  54. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  55. $args
  56. );
  57. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  58. $reflectionProperty->setAccessible(true);
  59. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  60. $this->handler->expects($this->any())
  61. ->method('fsockopen')
  62. ->will($this->returnValue($this->res));
  63. $this->handler->expects($this->any())
  64. ->method('streamSetTimeout')
  65. ->will($this->returnValue(true));
  66. $this->handler->expects($this->any())
  67. ->method('closeSocket')
  68. ->will($this->returnValue(true));
  69. }
  70. }