2
0

FlowdockHandlerTest.php 2.6 KB

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