Skip to content

Functional test to ensure no issue with plugins in sync nor async requests #144

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 3 commits into from
Jan 3, 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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">

<testsuites>
<testsuite name="HTTPlug unit tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
79 changes: 79 additions & 0 deletions tests/PluginClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

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
{
/**
* @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);
}

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);
}
$this->firstRun = true;

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];
}
}