FlowdockHandlerTest.php 2.4 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()
  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(string $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. $this->res = fopen('php://memory', 'a');
  54. $this->handler = $this->prophesize('Monolog\Handler\FlowdockHandler');
  55. $this->handler = new class($token, Logger::DEBUG) extends FlowdockHandler {
  56. public function fsockopen() {
  57. return $this->mockedResource;
  58. }
  59. public function streamSetTimeout() {
  60. return true;
  61. }
  62. public function closeSocket() {
  63. return true;
  64. }
  65. };
  66. $this->handler->mockedResource = $this->res;
  67. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  68. $reflectionProperty->setAccessible(true);
  69. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  70. $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com'));
  71. }
  72. }