SocketTest.php 3.4 KB

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