Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierLamyCanuel committed Nov 20, 2024
1 parent 31883ab commit f986166
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 97 deletions.
57 changes: 26 additions & 31 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
name: CI Backend

on:
push:
branches:
- master
pull_request:
push:
branches:
- master
pull_request:

concurrency:
# Concurrency is only limited on pull requests. head_ref is only defined on PR triggers so otherwise it will use the random run id and always build all pushes.
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
# Concurrency is only limited on pull requests. head_ref is only defined on PR triggers so otherwise it will use the random run id and always build all pushes.
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

permissions:
contents: read
contents: read

jobs:
phpunit-tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: [ "7.4", "8.0", "8.1", "8.2" ]
steps:
-
uses: actions/checkout@v3
-
name: Installing PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
-
name: Composer Install
run: composer install -o
-
name: Start test server
run: php -S 0.0.0.0:8091 ./tests/test-server.php > /dev/null 2>&1 &
-
name: PHPUnit
run: ./vendor/bin/phpunit -c ./tests/phpunit.xml.dist
phpunit-tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ["8.0", "8.1", "8.2"]
steps:
- uses: actions/checkout@v3
- name: Installing PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
- name: Composer Install
run: composer install -o
- name: Start test server
run: php -S 0.0.0.0:8091 ./tests/test-server.php > /dev/null 2>&1 &
- name: PHPUnit
run: ./vendor/bin/phpunit -c ./tests/phpunit.xml.dist
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=8.0",
"ext-curl": "*",
"ext-json": "*",
"vanilla/garden-utils": "^1.1",
Expand Down
12 changes: 7 additions & 5 deletions src/Mocks/MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
/**
* Mock HTTP client for testing. Does send actual HTTP requests.
*/
class MockHttpClient extends HttpClient {

class MockHttpClient extends HttpClient
{
/** @var MockHttpHandler */
private $mockHandler;

/**
* @inheritdoc
*/
public function __construct(string $baseUrl = '') {
public function __construct(string $baseUrl = "")
{
parent::__construct($baseUrl);
$this->mockHandler = new MockHttpHandler();
$this->setHandler($this->mockHandler);
Expand All @@ -36,7 +37,8 @@ public function __construct(string $baseUrl = '') {
*
* @return $this
*/
public function addMockRequest(HttpRequest $request, HttpResponse $response) {
public function addMockRequest(HttpRequest $request, HttpResponse $response)
{
$this->mockHandler->addMockRequest($request, $response);
return $this;
}
Expand All @@ -55,7 +57,7 @@ public function addMockResponse(
string $uri,
HttpResponse $response,
string $method = HttpRequest::METHOD_GET
) {
): static {
$this->mockHandler->addMockResponse($uri, $response, $method);
return $this;
}
Expand Down
23 changes: 15 additions & 8 deletions tests/Fixtures/EchoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@
use Garden\Http\HttpRequest;
use Garden\Http\HttpResponse;

class EchoHandler implements HttpHandlerInterface {
public function send(HttpRequest $request): HttpResponse {
parse_str(parse_url($request->getUrl(), PHP_URL_QUERY), $get);
class EchoHandler implements HttpHandlerInterface
{
public function send(HttpRequest $request): HttpResponse
{
$url = parse_url($request->getUrl(), PHP_URL_QUERY);
if ($url) {
parse_str($url, $get);
} else {
$get = [];
}

$response = new HttpResponse();
$response->setBody([
'method' => $request->getMethod(),
'url' => $request->getUrl(),
'headers' => $request->getHeaders(),
'get' => $get,
'body' => $request->getBody(),
"method" => $request->getMethod(),
"url" => $request->getUrl(),
"headers" => $request->getHeaders(),
"get" => $get,
"body" => $request->getBody(),
]);

$response->setRequest($request);
Expand Down
26 changes: 18 additions & 8 deletions tests/HttpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,42 @@

namespace Garden\Http\Tests;

require_once __DIR__ . "/Fixtures/EchoHandler.php";

use Garden\Http\HttpClient;
use Garden\Http\HttpRequest;
use Garden\Http\Tests\Fixtures\EchoHandler;
use PHPUnit\Framework\TestCase;

class HttpHandlerTest extends TestCase {
class HttpHandlerTest extends TestCase
{
/**
* Test sending a request through a handler.
*/
public function testRequestWithHandler() {
$request = new HttpRequest('GET', 'http://example.com', ['foo' => 'bar']);
public function testRequestWithHandler()
{
$request = new HttpRequest("GET", "http://example.com", [
"foo" => "bar",
]);
$handler = new EchoHandler();

$response = $handler->send($request);

$this->assertSame($request->getBody(), $response->getBody()['body']);
$this->assertSame($request->getBody(), $response->getBody()["body"]);
}

/**
* Test a handler with an HTTP client.
*/
public function testClientWithHandler() {
$api = new HttpClient('https://example.com', new EchoHandler());
public function testClientWithHandler()
{
$api = new HttpClient("https://example.com", new EchoHandler());

$response = $api->post('', ['foo' => 'bar']);
$response = $api->post("", ["foo" => "bar"]);

$this->assertSame($response->getRequest()->getBody(), $response->getBody()['body']);
$this->assertSame(
$response->getRequest()->getBody(),
$response->getBody()["body"]
);
}
}
66 changes: 43 additions & 23 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Garden\Http\Tests;

require_once __DIR__ . "/Fixtures/MockRequest.php";

use Garden\Http\HttpClient;
use Garden\Http\HttpRequest;
use Garden\Http\HttpResponse;
Expand All @@ -16,42 +18,56 @@
/**
* Tests for HTTP client middleware.
*/
class MiddlewareTest extends TestCase {
class MiddlewareTest extends TestCase
{
/**
* Middleware should pass its call off to the inner request sending.
*/
public function testInnerSend() {
$api = new HttpClient('http://example.com/api');
$api->addMiddleware([MockRequest::class, 'middleware']);
public function testInnerSend()
{
$api = new HttpClient("http://example.com/api");
$requester = new MockRequest();
$api->addMiddleware([$requester, "middleware"]);

$response = $api->post('/stuff?c=123', ['foo' => 'bar'], ['X-Baz' => 'bam']);
$this->assertEquals(MockRequest::class, $response->getBody()['class']);
$response = $api->post(
"/stuff?c=123",
["foo" => "bar"],
["X-Baz" => "bam"]
);
$this->assertEquals(MockRequest::class, $response->getBody()["class"]);
}

/**
* Middleware should be able to short-circuit the response.
*/
public function testOverwriteResponse() {
$api = new HttpClient('http://example.com/api');
$api->addMiddleware([MockRequest::class, 'echoRequest']);
public function testOverwriteResponse()
{
$api = new HttpClient("http://example.com/api");
$requester = new MockRequest();
$api->addMiddleware([$requester, "echoRequest"]);

$response = $api->post('/stuff?c=123', ['foo' => 'bar'], ['X-Baz' => 'bam']);
$this->assertEquals(HttpRequest::class, $response->getBody()['class']);
$response = $api->post(
"/stuff?c=123",
["foo" => "bar"],
["X-Baz" => "bam"]
);
$this->assertEquals(HttpRequest::class, $response->getBody()["class"]);
}

/**
* Multiple middleware should chain, calling the last middleware first.
*/
public function testMiddlewareChaining() {
$api = new HttpClient('http://example.com/api');
$api->addMiddleware($this->makeChainMiddleware('a'))
->addMiddleware($this->makeChainMiddleware('b'))
->addMiddleware($this->makeChainMiddleware('c'));
public function testMiddlewareChaining()
{
$api = new HttpClient("http://example.com/api");
$api->addMiddleware($this->makeChainMiddleware("a"))
->addMiddleware($this->makeChainMiddleware("b"))
->addMiddleware($this->makeChainMiddleware("c"));

$response = $api->post('/');
$response = $api->post("/");

$this->assertEquals('abc', $response->getHeader('X-Foo'));
$this->assertEquals('cba', $response->getRequest()->getHeader('X-Foo'));
$this->assertEquals("abc", $response->getHeader("X-Foo"));
$this->assertEquals("cba", $response->getRequest()->getHeader("X-Foo"));
}

/**
Expand All @@ -60,12 +76,16 @@ public function testMiddlewareChaining() {
* @param string $val The value to append.
* @return \Closure Returns the middleware.
*/
protected function makeChainMiddleware(string $val) {
return function (HttpRequest $request, callable $next) use ($val): HttpResponse {
$request->setHeader('X-Foo', $request->getHeader('X-Foo').$val);
protected function makeChainMiddleware(string $val)
{
return function (
HttpRequest $request,
callable $next
) use ($val): HttpResponse {
$request->setHeader("X-Foo", $request->getHeader("X-Foo") . $val);

$response = $next($request);
$response->setHeader('X-Foo', $response->getHeader('X-Foo').$val);
$response->setHeader("X-Foo", $response->getHeader("X-Foo") . $val);

return $response;
};
Expand Down
Loading

0 comments on commit f986166

Please sign in to comment.