LogEntriesHandlerTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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()
  49. {
  50. $useSSL = extension_loaded('openssl');
  51. $args = array('testToken', $useSSL, Logger::DEBUG, true);
  52. $this->res = fopen('php://memory', 'a');
  53. $this->handler = $this->getMock(
  54. '\Monolog\Handler\LogEntriesHandler',
  55. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  56. $args
  57. );
  58. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  59. $reflectionProperty->setAccessible(true);
  60. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  61. $this->handler->expects($this->any())
  62. ->method('fsockopen')
  63. ->will($this->returnValue($this->res));
  64. $this->handler->expects($this->any())
  65. ->method('streamSetTimeout')
  66. ->will($this->returnValue(true));
  67. $this->handler->expects($this->any())
  68. ->method('closeSocket')
  69. ->will($this->returnValue(true));
  70. }
  71. }