FlowdockHandlerTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. use Monolog\Util\LocalSocket;
  15. /**
  16. * @author Dominik Liebler <liebler.dominik@gmail.com>
  17. * @see https://www.hipchat.com/docs/api
  18. */
  19. class FlowdockHandlerTest extends TestCase
  20. {
  21. /**
  22. * @var resource
  23. */
  24. private $res;
  25. /**
  26. * @var FlowdockHandler
  27. */
  28. private $handler;
  29. public function setUp()
  30. {
  31. if (!extension_loaded('openssl')) {
  32. $this->markTestSkipped('This test requires openssl to run');
  33. }
  34. }
  35. public function testWriteHeader()
  36. {
  37. $this->initHandlerAndSocket();
  38. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  39. $content = $this->socket->getOutput();
  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(string $content)
  47. {
  48. $this->assertRegexp('/"source":"test_source"/', $content);
  49. $this->assertRegexp('/"from_address":"source@test\.com"/', $content);
  50. }
  51. private function initHandlerAndSocket($token = 'myToken')
  52. {
  53. $this->socket = LocalSocket::initSocket();
  54. $this->handler = new FlowdockHandler($token);
  55. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  56. $reflectionProperty->setAccessible(true);
  57. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  58. $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com'));
  59. }
  60. public function tearDown()
  61. {
  62. unset($this->socket, $this->handler);
  63. }
  64. }