SocketTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Monolog\Handler\SocketHandler;
  3. class SocketTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
  7. */
  8. public function testInvalidHostname() {
  9. $socket = new Socket('garbage://here');
  10. $socket->write('data');
  11. }
  12. /**
  13. * @expectedException \InvalidArgumentException
  14. */
  15. public function testBadConnectionTimeout()
  16. {
  17. $socket = new Socket('localhost:1234');
  18. $socket->setConnectionTimeout(-1);
  19. }
  20. public function testSetConnectionTimeout()
  21. {
  22. $socket = new Socket('localhost:1234');
  23. $socket->setConnectionTimeout(10);
  24. $this->assertEquals(10, $socket->getConnectionTimeout());
  25. }
  26. /**
  27. * @expectedException \InvalidArgumentException
  28. */
  29. public function testBadTimeout()
  30. {
  31. $socket = new Socket('localhost:1234');
  32. $socket->setTimeout(-1);
  33. }
  34. public function testSetTimeout()
  35. {
  36. $socket = new Socket('localhost:1234');
  37. $socket->setTimeout(10);
  38. $this->assertEquals(10, $socket->getTimeout());
  39. }
  40. public function testSetConnectionString()
  41. {
  42. $socket = new Socket('tcp://localhost:9090');
  43. $this->assertEquals('tcp://localhost:9090', $socket->getConnectionString());
  44. }
  45. public function testConnectionRefuesed()
  46. {
  47. try {
  48. $socket = new Socket('127.0.0.1:7894');
  49. $socket->setTimeout(1);
  50. $string = 'Hello world';
  51. $socket->write($string);
  52. $this->fail("Shoul not connect - are you running a server on 127.0.0.1:7894 ?");
  53. } catch (\Monolog\Handler\SocketHandler\Exception\ConnectionException $e) {
  54. }
  55. }
  56. /**
  57. * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
  58. */
  59. public function testConnectionTimeoutWithMock()
  60. {
  61. $socket = new MockSocket('localhost:54321');
  62. $socket->setConnectionTimeout(10);
  63. $socket->setFailConnectionTimeout(10);
  64. $socket->write('Hello world');
  65. }
  66. /**
  67. * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
  68. */
  69. public function testWriteTimeoutWithMock()
  70. {
  71. $socket = new MockSocket('localhost:54321');
  72. $socket->setTimeout(10);
  73. $socket->setFailTimeout(10);
  74. $socket->write('Hello world');
  75. }
  76. public function testWriteWithMock()
  77. {
  78. $socket = new MockSocket('localhost:54321');
  79. $socket->write('Hello world');
  80. $res = $socket->getResource();
  81. fseek($res, 0);
  82. $this->assertEquals('Hello world', fread($res, 1024));
  83. }
  84. public function testClose()
  85. {
  86. $resource = fopen('php://memory', 'a+');
  87. $socket = new MockSocket($resource);
  88. $this->assertTrue(is_resource($resource));
  89. $socket->close();
  90. $this->assertFalse(is_resource($resource));
  91. }
  92. /**
  93. * @expectedException \InvalidArgumentException
  94. */
  95. public function testInjectBadResourceThrowsException()
  96. {
  97. $socket = new Socket('');
  98. $socket->setResource('');
  99. }
  100. }