InsightOpsHandlerTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  14. * @author Robert Kaufmann III <rok3@rok3.me>
  15. * @author Gabriel Machado <gabriel.ms1@hotmail.com>
  16. */
  17. class InsightOpsHandlerTest extends TestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $resource;
  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->resource, 0);
  32. $content = fread($this->resource, 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. $this->createHandler();
  38. $this->handler->handleBatch($this->getMultipleRecords());
  39. fseek($this->resource, 0);
  40. $content = fread($this->resource, 1024);
  41. $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] .* \[\] \[\]\n){3}/', $content);
  42. }
  43. private function createHandler()
  44. {
  45. $useSSL = extension_loaded('openssl');
  46. $args = array('testToken', 'us', $useSSL, Logger::DEBUG, true);
  47. $this->resource = fopen('php://memory', 'a');
  48. $this->handler = $this->getMock(
  49. '\Monolog\Handler\InsightOpsHandler',
  50. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  51. $args
  52. );
  53. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  54. $reflectionProperty->setAccessible(true);
  55. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  56. $this->handler->expects($this->any())
  57. ->method('fsockopen')
  58. ->will($this->returnValue($this->resource));
  59. $this->handler->expects($this->any())
  60. ->method('streamSetTimeout')
  61. ->will($this->returnValue(true));
  62. $this->handler->expects($this->any())
  63. ->method('closeSocket')
  64. ->will($this->returnValue(true));
  65. }
  66. }