2
0

FlowdockHandlerTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Formatter\FlowdockFormatter;
  12. use Monolog\TestCase;
  13. use Monolog\Logger;
  14. /**
  15. * @author Dominik Liebler <liebler.dominik@gmail.com>
  16. * @see https://www.hipchat.com/docs/api
  17. */
  18. class FlowdockHandlerTest extends TestCase
  19. {
  20. /**
  21. * @var resource
  22. */
  23. private $res;
  24. /**
  25. * @var FlowdockHandler
  26. */
  27. private $handler;
  28. public function testWriteHeader()
  29. {
  30. $this->createHandler();
  31. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  32. fseek($this->res, 0);
  33. $content = fread($this->res, 1024);
  34. $this->assertRegexp('/POST \/v1\/messages\/team_inbox\/.* HTTP\/1.1\\r\\nHost: api.flowdock.com\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  35. return $content;
  36. }
  37. /**
  38. * @depends testWriteHeader
  39. */
  40. public function testWriteContent($content)
  41. {
  42. $this->assertRegexp('/"source":"test_source"/', $content);
  43. $this->assertRegexp('/"from_address":"source@test\.com"/', $content);
  44. }
  45. private function createHandler($token = 'myToken')
  46. {
  47. $constructorArgs = array($token, Logger::DEBUG);
  48. $this->res = fopen('php://memory', 'a');
  49. $this->handler = $this->getMock(
  50. '\Monolog\Handler\FlowdockHandler',
  51. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  52. $constructorArgs
  53. );
  54. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  55. $reflectionProperty->setAccessible(true);
  56. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  57. $this->handler->expects($this->any())
  58. ->method('fsockopen')
  59. ->will($this->returnValue($this->res));
  60. $this->handler->expects($this->any())
  61. ->method('streamSetTimeout')
  62. ->will($this->returnValue(true));
  63. $this->handler->expects($this->any())
  64. ->method('closeSocket')
  65. ->will($this->returnValue(true));
  66. $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com'));
  67. }
  68. }