SlackHandlerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  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\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @author Greg Kedzierski <greg@gregkedzierski.com>
  15. * @see https://api.slack.com/
  16. */
  17. class SlackHandlerTest extends TestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $res;
  23. /**
  24. * @var SlackHandler
  25. */
  26. private $handler;
  27. public function setUp()
  28. {
  29. if (!extension_loaded('openssl')) {
  30. $this->markTestSkipped('This test requires openssl to run');
  31. }
  32. }
  33. public function testWriteHeader()
  34. {
  35. $this->createHandler();
  36. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  37. fseek($this->res, 0);
  38. $content = fread($this->res, 1024);
  39. $this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  40. return $content;
  41. }
  42. /**
  43. * @depends testWriteHeader
  44. */
  45. public function testWriteContent($content)
  46. {
  47. $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&attachments=.*$/', $content);
  48. }
  49. private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog')
  50. {
  51. $constructorArgs = array($token, $channel, $username, Logger::DEBUG);
  52. $this->res = fopen('php://memory', 'a');
  53. $this->handler = $this->getMock(
  54. '\Monolog\Handler\SlackHandler',
  55. array('fsockopen', 'streamSetTimeout', 'closeSocket'),
  56. $constructorArgs
  57. );
  58. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  59. $reflectionProperty->setAccessible(true);
  60. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  61. $this->handler->expects($this->any())
  62. ->method('fsockopen')
  63. ->will($this->returnValue($this->res));
  64. $this->handler->expects($this->any())
  65. ->method('streamSetTimeout')
  66. ->will($this->returnValue(true));
  67. $this->handler->expects($this->any())
  68. ->method('closeSocket')
  69. ->will($this->returnValue(true));
  70. $this->handler->setFormatter($this->getIdentityFormatter());
  71. }
  72. }