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\TestCase;
  12. use Monolog\Logger;
  13. use Monolog\Formatter\LineFormatter;
  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->createHandler();
  30. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
  31. fseek($this->res, 0);
  32. $content = fread($this->res, 1024);
  33. $this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] test.CRITICAL: Critical write test/', $content);
  34. }
  35. public function testWriteBatchContent()
  36. {
  37. $records = array(
  38. $this->getRecord(),
  39. $this->getRecord(),
  40. $this->getRecord()
  41. );
  42. $this->createHandler();
  43. $this->handler->handleBatch($records);
  44. fseek($this->res, 0);
  45. $content = fread($this->res, 1024);
  46. $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] .* \[\] \[\]\n){3}/', $content);
  47. }
  48. private function createHandler($logToken = 'testToken', $useSSL = true)
  49. {
  50. $args = array($logToken, Logger::DEBUG, true, $useSSL = 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. }