123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace React\Tests\Socket;
- use React\Socket\Connector;
- use React\Promise\Promise;
- class ConnectorTest extends TestCase
- {
- public function testConstructWithoutLoopAssignsLoopAutomatically()
- {
- $connector = new Connector();
- $ref = new \ReflectionProperty($connector, 'connectors');
- $ref->setAccessible(true);
- $connectors = $ref->getValue($connector);
- $ref = new \ReflectionProperty($connectors['tcp'], 'loop');
- $ref->setAccessible(true);
- $loop = $ref->getValue($connectors['tcp']);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
- }
- public function testConstructWithLoopAssignsGivenLoop()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector(array(), $loop);
- $ref = new \ReflectionProperty($connector, 'connectors');
- $ref->setAccessible(true);
- $connectors = $ref->getValue($connector);
- $ref = new \ReflectionProperty($connectors['tcp'], 'loop');
- $ref->setAccessible(true);
- $loop = $ref->getValue($connectors['tcp']);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
- }
- public function testConstructWithContextAssignsGivenContext()
- {
- $tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
- $connector = new Connector(array(
- 'tcp' => $tcp,
- 'dns' => false,
- 'timeout' => false
- ));
- $ref = new \ReflectionProperty($connector, 'connectors');
- $ref->setAccessible(true);
- $connectors = $ref->getValue($connector);
- $this->assertSame($tcp, $connectors['tcp']);
- }
- public function testConstructWithLegacyContextSignatureAssignsGivenContext()
- {
- $tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
- $connector = new Connector(null, array(
- 'tcp' => $tcp,
- 'dns' => false,
- 'timeout' => false
- ));
- $ref = new \ReflectionProperty($connector, 'connectors');
- $ref->setAccessible(true);
- $connectors = $ref->getValue($connector);
- $this->assertSame($tcp, $connectors['tcp']);
- }
- public function testConstructWithLegacyLoopSignatureAssignsGivenLoop()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector($loop);
- $ref = new \ReflectionProperty($connector, 'connectors');
- $ref->setAccessible(true);
- $connectors = $ref->getValue($connector);
- $ref = new \ReflectionProperty($connectors['tcp'], 'loop');
- $ref->setAccessible(true);
- $loop = $ref->getValue($connectors['tcp']);
- $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
- }
- public function testConstructWithInvalidContextThrows()
- {
- $this->setExpectedException('InvalidArgumentException');
- new Connector('foo');
- }
- public function testConstructWithInvalidLoopThrows()
- {
- $this->setExpectedException('InvalidArgumentException');
- new Connector(array(), 'foo');
- }
- public function testConstructWithContextTwiceThrows()
- {
- $this->setExpectedException('InvalidArgumentException');
- new Connector(array(), array());
- }
- public function testConstructWithLoopTwiceThrows()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $this->setExpectedException('InvalidArgumentException');
- new Connector($loop, $loop);
- }
- public function testConstructWithNullContextAndLoopThrows()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $this->setExpectedException('InvalidArgumentException');
- new Connector(null, $loop);
- }
- public function testConstructWithLoopAndNullContextThrows()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $this->setExpectedException('InvalidArgumentException');
- new Connector($loop, null);
- }
- public function testConnectorUsesTcpAsDefaultScheme()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $promise = new Promise(function () { });
- $tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
- $tcp->expects($this->once())->method('connect')->with('127.0.0.1:80')->willReturn($promise);
- $connector = new Connector(array(
- 'tcp' => $tcp
- ), $loop);
- $connector->connect('127.0.0.1:80');
- }
- public function testConnectorPassedThroughHostnameIfDnsIsDisabled()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $promise = new Promise(function () { });
- $tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
- $tcp->expects($this->once())->method('connect')->with('tcp://google.com:80')->willReturn($promise);
- $connector = new Connector(array(
- 'tcp' => $tcp,
- 'dns' => false
- ), $loop);
- $connector->connect('tcp://google.com:80');
- }
- public function testConnectorWithUnknownSchemeAlwaysFails()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector(array(), $loop);
- $promise = $connector->connect('unknown://google.com:80');
- $promise->then(null, $this->expectCallableOnceWithException(
- 'RuntimeException',
- 'No connector available for URI scheme "unknown" (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- ));
- }
- public function testConnectorWithDisabledTcpDefaultSchemeAlwaysFails()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector(array(
- 'tcp' => false
- ), $loop);
- $promise = $connector->connect('google.com:80');
- $promise->then(null, $this->expectCallableOnceWithException(
- 'RuntimeException',
- 'No connector available for URI scheme "tcp" (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- ));
- }
- public function testConnectorWithDisabledTcpSchemeAlwaysFails()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector(array(
- 'tcp' => false
- ), $loop);
- $promise = $connector->connect('tcp://google.com:80');
- $promise->then(null, $this->expectCallableOnceWithException(
- 'RuntimeException',
- 'No connector available for URI scheme "tcp" (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- ));
- }
- public function testConnectorWithDisabledTlsSchemeAlwaysFails()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector(array(
- 'tls' => false
- ), $loop);
- $promise = $connector->connect('tls://google.com:443');
- $promise->then(null, $this->expectCallableOnceWithException(
- 'RuntimeException',
- 'No connector available for URI scheme "tls" (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- ));
- }
- public function testConnectorWithDisabledUnixSchemeAlwaysFails()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $connector = new Connector(array(
- 'unix' => false
- ), $loop);
- $promise = $connector->connect('unix://demo.sock');
- $promise->then(null, $this->expectCallableOnceWithException(
- 'RuntimeException',
- 'No connector available for URI scheme "unix" (EINVAL)',
- defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
- ));
- }
- public function testConnectorUsesGivenResolverInstance()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $promise = new Promise(function () { });
- $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
- $resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn($promise);
- $connector = new Connector(array(
- 'dns' => $resolver,
- 'happy_eyeballs' => false,
- ), $loop);
- $connector->connect('google.com:80');
- }
- public function testConnectorUsesResolvedHostnameIfDnsIsUsed()
- {
- $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
- $promise = new Promise(function ($resolve) { $resolve('127.0.0.1'); });
- $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
- $resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn($promise);
- $promise = new Promise(function () { });
- $tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
- $tcp->expects($this->once())->method('connect')->with('tcp://127.0.0.1:80?hostname=google.com')->willReturn($promise);
- $connector = new Connector(array(
- 'tcp' => $tcp,
- 'dns' => $resolver,
- 'happy_eyeballs' => false,
- ), $loop);
- $connector->connect('tcp://google.com:80');
- }
- }
|