Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept DNS ResolverInterface and fix failing test suite #208

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ $connector->connect('127.0.0.1:80')->then(function (React\Socket\ConnectionInter
});
```

Advanced: If you need a custom DNS `Resolver` instance, you can also set up
your `Connector` like this:
Advanced: If you need a custom DNS `React\Dns\Resolver\ResolverInterface` instance, you
can also set up your `Connector` like this:

```php
$dnsResolverFactory = new React\Dns\Resolver\Factory();
Expand Down Expand Up @@ -1193,8 +1193,8 @@ $promise->cancel();
Calling `cancel()` on a pending promise will cancel the underlying DNS lookup
and/or the underlying TCP/IP connection and reject the resulting promise.

> Advanced usage: Internally, the `DnsConnector` relies on a `Resolver` to
look up the IP address for the given hostname.
> Advanced usage: Internally, the `DnsConnector` relies on a `React\Dns\Resolver\ResolverInterface`
to look up the IP address for the given hostname.
It will then replace the hostname in the destination URI with this IP and
append a `hostname` query parameter and pass this updated URI to the underlying
connector.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": ">=5.3.0",
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/dns": "^1.0 || ^0.4.13",
"react/dns": "^1.1",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/promise": "^2.6.0 || ^1.2.1",
"react/promise-timer": "^1.4.0",
Expand Down
16 changes: 7 additions & 9 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace React\Socket;

use React\Dns\Config\Config;
use React\Dns\Resolver\Factory;
use React\Dns\Resolver\Resolver;
use React\Dns\Config\Config as DnsConfig;
use React\Dns\Resolver\Factory as DnsFactory;
use React\Dns\Resolver\ResolverInterface;
use React\EventLoop\LoopInterface;
use React\Promise;
use RuntimeException;

/**
* The `Connector` class is the main class in this package that implements the
Expand Down Expand Up @@ -54,18 +52,18 @@ public function __construct(LoopInterface $loop, array $options = array())
}

if ($options['dns'] !== false) {
if ($options['dns'] instanceof Resolver) {
if ($options['dns'] instanceof ResolverInterface) {
$resolver = $options['dns'];
} else {
if ($options['dns'] !== true) {
$server = $options['dns'];
} else {
// try to load nameservers from system config or default to Google's public DNS
$config = Config::loadSystemConfigBlocking();
$config = DnsConfig::loadSystemConfigBlocking();
$server = $config->nameservers ? \reset($config->nameservers) : '8.8.8.8';
}

$factory = new Factory();
$factory = new DnsFactory();
$resolver = $factory->create(
$server,
$loop
Expand Down Expand Up @@ -125,7 +123,7 @@ public function connect($uri)
}

if (!isset($this->connectors[$scheme])) {
return Promise\reject(new \RuntimeException(
return \React\Promise\reject(new \RuntimeException(
'No connector available for URI scheme "' . $scheme . '"'
));
}
Expand Down
6 changes: 2 additions & 4 deletions src/DnsConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace React\Socket;

use React\Dns\Resolver\Resolver;
use React\Dns\Resolver\ResolverInterface;
use React\Promise;
use React\Promise\CancellablePromiseInterface;
use InvalidArgumentException;
use RuntimeException;

final class DnsConnector implements ConnectorInterface
{
private $connector;
private $resolver;

public function __construct(ConnectorInterface $connector, Resolver $resolver)
public function __construct(ConnectorInterface $connector, ResolverInterface $resolver)
{
$this->connector = $connector;
$this->resolver = $resolver;
Expand Down
4 changes: 2 additions & 2 deletions tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testConnectorUsesGivenResolverInstance()
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$promise = new Promise(function () { });
$resolver = $this->getMockBuilder('React\Dns\Resolver\Resolver')->disableOriginalConstructor()->getMock();
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
$resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn($promise);

$connector = new Connector($loop, array(
Expand All @@ -111,7 +111,7 @@ 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\Resolver')->disableOriginalConstructor()->getMock();
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
$resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn($promise);

$promise = new Promise(function () { });
Expand Down
2 changes: 1 addition & 1 deletion tests/DnsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DnsConnectorTest extends TestCase
public function setUp()
{
$this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->resolver = $this->getMockBuilder('React\Dns\Resolver\Resolver')->disableOriginalConstructor()->getMock();
$this->resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();

$this->connector = new DnsConnector($this->tcp, $this->resolver);
}
Expand Down