설명 없음

FlowdockHandlerTest.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 setUp()
  29. {
  30. if (!extension_loaded('openssl')) {
  31. $this->markTestSkipped('This test requires openssl to run');
  32. }
  33. }
  34. public function testWriteHeader()
  35. {
  36. $this->createHandler();
  37. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  38. fseek($this->res, 0);
  39. $content = fread($this->res, 1024);
  40. $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);
  41. return $content;
  42. }
  43. /**
  44. * @depends testWriteHeader
  45. */
  46. public function testWriteContent($content)
  47. {
  48. $this->assertRegexp('/"source":"test_source"/', $content);
  49. $this->assertRegexp('/"from_address":"source@test\.com"/', $content);
  50. }
  51. private function createHandler($token = 'myToken')
  52. {
  53. $constructorArgs = array($token, Logger::DEBUG);
  54. $this->res = fopen('php://memory', 'a');
  55. $this->handler = $this->getMock(
  56. '\Monolog\Handler\FlowdockHandler',
  57. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  58. $constructorArgs
  59. );
  60. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  61. $reflectionProperty->setAccessible(true);
  62. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  63. $this->handler->expects($this->any())
  64. ->method('fsockopen')
  65. ->will($this->returnValue($this->res));
  66. $this->handler->expects($this->any())
  67. ->method('streamSetTimeout')
  68. ->will($this->returnValue(true));
  69. $this->handler->expects($this->any())
  70. ->method('closeSocket')
  71. ->will($this->returnValue(true));
  72. $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com'));
  73. }
  74. }