FlowdockHandlerTest.php 2.6 KB

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