UnixConnectorTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace React\Tests\Socket;
  3. use React\Socket\ConnectionInterface;
  4. use React\Socket\UnixConnector;
  5. class UnixConnectorTest extends TestCase
  6. {
  7. private $loop;
  8. private $connector;
  9. /**
  10. * @before
  11. */
  12. public function setUpConnector()
  13. {
  14. $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  15. $this->connector = new UnixConnector($this->loop);
  16. }
  17. public function testCtorThrowsForInvalidLoop()
  18. {
  19. $this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
  20. new UnixConnector('loop');
  21. }
  22. public function testConstructWithoutLoopAssignsLoopAutomatically()
  23. {
  24. $connector = new UnixConnector();
  25. $ref = new \ReflectionProperty($connector, 'loop');
  26. $ref->setAccessible(true);
  27. $loop = $ref->getValue($connector);
  28. $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
  29. }
  30. public function testInvalid()
  31. {
  32. $promise = $this->connector->connect('google.com:80');
  33. $promise->then(null, $this->expectCallableOnceWithException(
  34. 'RuntimeException'
  35. ));
  36. }
  37. public function testInvalidScheme()
  38. {
  39. $promise = $this->connector->connect('tcp://google.com:80');
  40. $promise->then(null, $this->expectCallableOnceWithException(
  41. 'InvalidArgumentException',
  42. 'Given URI "tcp://google.com:80" is invalid (EINVAL)',
  43. defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
  44. ));
  45. }
  46. public function testValid()
  47. {
  48. if (!in_array('unix', stream_get_transports())) {
  49. $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)');
  50. }
  51. // random unix domain socket path
  52. $path = sys_get_temp_dir() . '/test' . uniqid() . '.sock';
  53. // temporarily create unix domain socket server to connect to
  54. $server = stream_socket_server('unix://' . $path, $errno, $errstr);
  55. // skip test if we can not create a test server (Windows etc.)
  56. if (!$server) {
  57. $this->markTestSkipped('Unable to create socket "' . $path . '": ' . $errstr . '(' . $errno .')');
  58. return;
  59. }
  60. // tests succeeds if we get notified of successful connection
  61. $promise = $this->connector->connect($path);
  62. $promise->then($this->expectCallableOnce());
  63. // remember remote and local address of this connection and close again
  64. $remote = $local = false;
  65. $promise->then(function(ConnectionInterface $conn) use (&$remote, &$local) {
  66. $remote = $conn->getRemoteAddress();
  67. $local = $conn->getLocalAddress();
  68. $conn->close();
  69. });
  70. // clean up server
  71. fclose($server);
  72. unlink($path);
  73. $this->assertNull($local);
  74. $this->assertEquals('unix://' . $path, $remote);
  75. }
  76. }