2
0

FlowdockHandlerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php declare(strict_types=1);
  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\Test\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(): void
  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 = [$token, Logger::DEBUG];
  54. $this->res = fopen('php://memory', 'a');
  55. $this->handler = $this->getMockBuilder('Monolog\Handler\FlowdockHandler')
  56. ->setConstructorArgs($constructorArgs)
  57. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  58. ->getMock();
  59. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  60. $reflectionProperty->setAccessible(true);
  61. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  62. $this->handler->expects($this->any())
  63. ->method('fsockopen')
  64. ->will($this->returnValue($this->res));
  65. $this->handler->expects($this->any())
  66. ->method('streamSetTimeout')
  67. ->will($this->returnValue(true));
  68. $this->handler->expects($this->any())
  69. ->method('closeSocket')
  70. ->will($this->returnValue(true));
  71. $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com'));
  72. }
  73. }