FlowdockHandlerTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 tearDown(): void
  35. {
  36. parent::tearDown();
  37. unset($this->res);
  38. }
  39. public function testWriteHeader()
  40. {
  41. $this->createHandler();
  42. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  43. fseek($this->res, 0);
  44. $content = fread($this->res, 1024);
  45. $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);
  46. return $content;
  47. }
  48. /**
  49. * @depends testWriteHeader
  50. */
  51. public function testWriteContent($content)
  52. {
  53. $this->assertRegexp('/"source":"test_source"/', $content);
  54. $this->assertRegexp('/"from_address":"source@test\.com"/', $content);
  55. }
  56. private function createHandler($token = 'myToken')
  57. {
  58. $constructorArgs = [$token, Logger::DEBUG];
  59. $this->res = fopen('php://memory', 'a');
  60. $this->handler = $this->getMockBuilder('Monolog\Handler\FlowdockHandler')
  61. ->setConstructorArgs($constructorArgs)
  62. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  63. ->getMock();
  64. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  65. $reflectionProperty->setAccessible(true);
  66. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  67. $this->handler->expects($this->any())
  68. ->method('fsockopen')
  69. ->will($this->returnValue($this->res));
  70. $this->handler->expects($this->any())
  71. ->method('streamSetTimeout')
  72. ->will($this->returnValue(true));
  73. $this->handler->expects($this->any())
  74. ->method('closeSocket')
  75. ->will($this->returnValue(true));
  76. $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com'));
  77. }
  78. }