Skip to content

Fix composer version conflict with react/http v0.8 #28

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

Merged
merged 5 commits into from
Dec 21, 2017
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## 2.1.0 - 2017-12-20

### Changed

- Added compatibility with `react/http-client` v0.5 (compatibility with v0.4 kept)
- `ReactFactory::buildHttpClient` now accepts a `\React\Socket\ConnectorInterface` (only for `react/http-client` v0.5).
If none provided, will use React HTTP Client defaults.

### Deprecations
- Passing a `\React\Dns\Resolver\Resolver` to `ReactFactory::buildHttpClient` is deprecated and will be removed in **3.0.0**.
To control connector behavior (DNS, timeout, etc), pass a `\React\Socket\ConnectorInterface` instead.

## 2.0.0 - 2017-09-18

### Changed
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
"require": {
"php": "^5.5 || ^7.0",
"php-http/httplug": "^1.0",
"react/http-client": "^0.4.8",
"react/http-client": "^0.4.8 || ^0.5",
"react/dns": "^0.4.1",
"react/stream": "^0.4.3",
"react/stream": "^0.4.3 || ^0.7",
"react/socket": "^0.8",
"php-http/discovery": "^1.0"
},
"require-dev": {
"php-http/client-integration-tests": "^0.6",
"php-http/message": "^1.0"
"php-http/message": "^1.0",
"phpunit/phpunit": "^4.8.36 || ^5.4"
},
"provide": {
"php-http/client-implementation": "1.0",
Expand Down
90 changes: 85 additions & 5 deletions src/ReactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use React\EventLoop\Factory as EventLoopFactory;
use React\Dns\Resolver\Resolver as DnsResolver;
use React\Dns\Resolver\Factory as DnsResolverFactory;
use React\HttpClient\Factory as HttpClientFactory;
use React\HttpClient\Client as HttpClient;
use React\HttpClient\Factory as HttpClientFactory;
use React\Socket\Connector;
use React\Socket\ConnectorInterface;

/**
* Factory wrapper for React instances.
Expand Down Expand Up @@ -43,24 +45,102 @@ public static function buildDnsResolver(
return $factory->createCached($dns, $loop);
}

/**
* @param LoopInterface $loop
* @param DnsResolver|null $dns
*
* @return ConnectorInterface
*/
public static function buildConnector(
LoopInterface $loop,
DnsResolver $dns = null
) {
return null !== $dns
? new Connector($loop, ['dns' => $dns])
: new Connector($loop);
}

/**
* Build a React Http Client.
*
* @param LoopInterface $loop
* @param DnsResolver $dns
* @param LoopInterface $loop
* @param ConnectorInterface|DnsResolver|null $connector Only pass this argument if you need to customize DNS
* behaviour. With react http client v0.5, pass a connector,
* with v0.4 this must be a DnsResolver.
*
* @return HttpClient
*/
public static function buildHttpClient(
LoopInterface $loop,
DnsResolver $dns = null
$connector = null
) {
if (class_exists(HttpClientFactory::class)) {
// if HttpClientFactory class exists, use old behavior for backwards compatibility
return static::buildHttpClient04($loop, $connector);
} else {
return static::buildHttpClient05($loop, $connector);
}
}

/**
* Builds a React Http client v0.4 style.
*
* @param LoopInterface $loop
* @param DnsResolver|null $dns
*
* @return HttpClient
*/
protected static function buildHttpClient04(
LoopInterface $loop,
$dns = null
) {
// create dns resolver if one isn't provided
if (null === $dns) {
$dns = self::buildDnsResolver($loop);
$dns = static::buildDnsResolver($loop);
}

// validate connector instance for proper error reporting
if (!$dns instanceof DnsResolver) {
throw new \InvalidArgumentException('For react http client v0.4, $dns must be an instance of DnsResolver');
}

$factory = new HttpClientFactory();

return $factory->create($loop, $dns);
}

/**
* Builds a React Http client v0.5 style.
*
* @param LoopInterface $loop
* @param DnsResolver|ConnectorInterface|null $connector
*
* @return HttpClient
*/
protected static function buildHttpClient05(
LoopInterface $loop,
$connector = null
) {
// build a connector with given DnsResolver if provided (old deprecated behavior)
if ($connector instanceof DnsResolver) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a deprecation warning inside the if?

@trigger_error(sprintf('Passing a %s to buildHttpClient is deprecated since version 2.1.0 and will be removed in 3.0. If you need no specific behaviour, omit the $dns argument, otherwise pass a %s', DnsResolver::class, ConnectorInterface::class), E_USER_DEPRECATED);

@trigger_error(
sprintf(
'Passing a %s to buildHttpClient is deprecated since version 2.1.0 and will be removed in 3.0. If you need no specific behaviour, omit the $dns argument, otherwise pass a %s',
DnsResolver::class,
ConnectorInterface::class
),
E_USER_DEPRECATED
);
$connector = static::buildConnector($loop, $connector);
}

// validate connector instance for proper error reporting
if (null !== $connector && !$connector instanceof ConnectorInterface) {
throw new \InvalidArgumentException(
'$connector must be an instance of DnsResolver or ConnectorInterface'
);
}

return new HttpClient($loop, $connector);
}
}
67 changes: 67 additions & 0 deletions tests/ReactFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Http\Adapter\React\Tests;

use Http\Adapter\React\ReactFactory;
use PHPUnit\Framework\TestCase;
use React\Dns\Resolver\Resolver;
use React\EventLoop\LoopInterface;
use React\HttpClient\Client;
use React\HttpClient\Factory;
use React\Socket\ConnectorInterface;

/**
* These tests don't really ensure the correct instances are baked into the returned http client, instead, they are
* just testing the code against the expected use cases.
*
* @author Samuel Nogueira <[email protected]>
*/
class ReactFactoryTest extends TestCase
{
/**
* @var \React\EventLoop\LoopInterface
*/
private $loop;

protected function setUp()
{
$this->loop = $this->getMockBuilder(LoopInterface::class)->getMock();
}

public function testBuildHttpClientWithConnector()
{
if (class_exists(Factory::class)) {
$this->markTestSkipped('This test only runs with react http client v0.5 and above');
}

$connector = $this->getMockBuilder(ConnectorInterface::class)->getMock();
$client = ReactFactory::buildHttpClient($this->loop, $connector);
$this->assertInstanceOf(Client::class, $client);
}

/**
* @deprecated Building HTTP client passing a DnsResolver instance is deprecated. Should pass a ConnectorInterface
* instance instead.
*/
public function testBuildHttpClientWithDnsResolver()
{
$connector = $this->getMockBuilder(Resolver::class)->disableOriginalConstructor()->getMock();
$client = ReactFactory::buildHttpClient($this->loop, $connector);
$this->assertInstanceOf(Client::class, $client);
}

public function testBuildHttpClientWithoutConnector()
{
$client = ReactFactory::buildHttpClient($this->loop);
$this->assertInstanceOf(Client::class, $client);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testBuildHttpClientWithInvalidConnectorThrowsException()
{
$connector = $this->getMockBuilder(LoopInterface::class)->getMock();
ReactFactory::buildHttpClient($this->loop, $connector);
}
}