UnixConnectorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 testConstructWithoutLoopAssignsLoopAutomatically()
  18. {
  19. $connector = new UnixConnector();
  20. $ref = new \ReflectionProperty($connector, 'loop');
  21. $ref->setAccessible(true);
  22. $loop = $ref->getValue($connector);
  23. $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
  24. }
  25. public function testInvalid()
  26. {
  27. $promise = $this->connector->connect('google.com:80');
  28. $promise->then(null, $this->expectCallableOnceWithException(
  29. 'RuntimeException'
  30. ));
  31. }
  32. public function testInvalidScheme()
  33. {
  34. $promise = $this->connector->connect('tcp://google.com:80');
  35. $promise->then(null, $this->expectCallableOnceWithException(
  36. 'InvalidArgumentException',
  37. 'Given URI "tcp://google.com:80" is invalid (EINVAL)',
  38. defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
  39. ));
  40. }
  41. public function testValid()
  42. {
  43. if (!in_array('unix', stream_get_transports())) {
  44. $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)');
  45. }
  46. // random unix domain socket path
  47. $path = sys_get_temp_dir() . '/test' . uniqid() . '.sock';
  48. // temporarily create unix domain socket server to connect to
  49. $server = stream_socket_server('unix://' . $path, $errno, $errstr);
  50. // skip test if we can not create a test server (Windows etc.)
  51. if (!$server) {
  52. $this->markTestSkipped('Unable to create socket "' . $path . '": ' . $errstr . '(' . $errno .')');
  53. return;
  54. }
  55. // tests succeeds if we get notified of successful connection
  56. $promise = $this->connector->connect($path);
  57. $promise->then($this->expectCallableOnce());
  58. // remember remote and local address of this connection and close again
  59. $remote = $local = false;
  60. $promise->then(function(ConnectionInterface $conn) use (&$remote, &$local) {
  61. $remote = $conn->getRemoteAddress();
  62. $local = $conn->getLocalAddress();
  63. $conn->close();
  64. });
  65. // clean up server
  66. fclose($server);
  67. unlink($path);
  68. $this->assertNull($local);
  69. $this->assertEquals('unix://' . $path, $remote);
  70. }
  71. }