From 7d5fe4deb07494ddddc49296972de8adfcc59bd6 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Mon, 31 Dec 2018 12:15:51 +0100 Subject: [PATCH 1/3] init --- .travis.yml | 2 ++ phpunit.xml.dist | 14 ++++++++++++++ tests/PluginClientTest.php | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 phpunit.xml.dist create mode 100644 tests/PluginClientTest.php diff --git a/.travis.yml b/.travis.yml index 84eac28..10d366e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,8 @@ jobs: env: DEPENDENCIES="dunglas/symfony-lock:^3" - php: 7.2 env: DEPENDENCIES="dunglas/symfony-lock:^4" + - php: 7.2 + env: TEST_COMMAND="./vendor/bin/phpunit" DEPENDENCIES="phpunit/phpunit:^7.5 nyholm/psr7:^1.0" # Latest dev release - php: 7.3 diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..d353b7c --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,14 @@ + + + + + + + ./tests + + + diff --git a/tests/PluginClientTest.php b/tests/PluginClientTest.php new file mode 100644 index 0000000..d88bbf1 --- /dev/null +++ b/tests/PluginClientTest.php @@ -0,0 +1,18 @@ + Date: Mon, 31 Dec 2018 12:36:17 +0100 Subject: [PATCH 2/3] Small test to prove a bug. --- tests/PluginClientTest.php | 64 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/tests/PluginClientTest.php b/tests/PluginClientTest.php index d88bbf1..47bf8c3 100644 --- a/tests/PluginClientTest.php +++ b/tests/PluginClientTest.php @@ -4,15 +4,73 @@ namespace tests\Http\Client\Common; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\HeaderAppendPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\HttpAsyncClient; +use Http\Client\Promise\HttpFulfilledPromise; +use Http\Promise\Promise; +use Nyholm\Psr7\Request; +use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; class PluginClientTest extends TestCase { - private $syncClient; - private $asyncClient; + /** + * @dataProvider clientAndMethodProvider + */ + public function testRestartChain(PluginClient $client, string $method, string $returnType) + { + $request = new Request('GET', 'https://example.com'); + $result = call_user_func([$client, $method], $request); + + $this->assertInstanceOf($returnType, $result); + } - protected function setUp() + public function clientAndMethodProvider() { + $syncClient = new class implements ClientInterface { + public function sendRequest(RequestInterface $request): ResponseInterface + { + return new Response(); + } + }; + + $asyncClient = new class implements HttpAsyncClient { + public function sendAsyncRequest(RequestInterface $request) + { + return new HttpFulfilledPromise(new Response()); + } + }; + + $headerAppendPlugin = new HeaderAppendPlugin(['Content-Type'=>'text/html']); + $redirectPlugin = new RedirectPlugin(); + $restartOncePlugin = new class implements Plugin { + private $firstRun = true; + + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise + { + if ($this->firstRun) { + $this->firstRun = false; + return $first($request)->wait(); + } + return $next($request); + } + }; + + $plugins = [$headerAppendPlugin, $restartOncePlugin, $redirectPlugin]; + + $pluginClient = new PluginClient($syncClient, $plugins); + yield [$pluginClient, 'sendRequest', ResponseInterface::class]; + yield [$pluginClient, 'sendAsyncRequest', Promise::class]; + // Async + $pluginClient = new PluginClient($asyncClient, $plugins); + yield [$pluginClient, 'sendRequest', ResponseInterface::class]; + yield [$pluginClient, 'sendAsyncRequest', Promise::class]; } } \ No newline at end of file From aea593686d77bcf347d0971dbaf284b32fe6f34e Mon Sep 17 00:00:00 2001 From: Nyholm Date: Mon, 31 Dec 2018 12:43:12 +0100 Subject: [PATCH 3/3] cs --- tests/PluginClientTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/PluginClientTest.php b/tests/PluginClientTest.php index 47bf8c3..eb55685 100644 --- a/tests/PluginClientTest.php +++ b/tests/PluginClientTest.php @@ -33,31 +33,34 @@ public function testRestartChain(PluginClient $client, string $method, string $r public function clientAndMethodProvider() { - $syncClient = new class implements ClientInterface { + $syncClient = new class() implements ClientInterface { public function sendRequest(RequestInterface $request): ResponseInterface { return new Response(); } }; - $asyncClient = new class implements HttpAsyncClient { + $asyncClient = new class() implements HttpAsyncClient { public function sendAsyncRequest(RequestInterface $request) { return new HttpFulfilledPromise(new Response()); } }; - $headerAppendPlugin = new HeaderAppendPlugin(['Content-Type'=>'text/html']); + $headerAppendPlugin = new HeaderAppendPlugin(['Content-Type' => 'text/html']); $redirectPlugin = new RedirectPlugin(); - $restartOncePlugin = new class implements Plugin { + $restartOncePlugin = new class() implements Plugin { private $firstRun = true; public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { if ($this->firstRun) { $this->firstRun = false; - return $first($request)->wait(); + + return $first($request); } + $this->firstRun = true; + return $next($request); } }; @@ -73,4 +76,4 @@ public function handleRequest(RequestInterface $request, callable $next, callabl yield [$pluginClient, 'sendRequest', ResponseInterface::class]; yield [$pluginClient, 'sendAsyncRequest', Promise::class]; } -} \ No newline at end of file +}