From f986166aea6e098085195783f4ca955dd2af0307 Mon Sep 17 00:00:00 2001 From: Olivier Lamy-Canuel Date: Wed, 20 Nov 2024 15:02:24 -0500 Subject: [PATCH] fix tests --- .github/workflows/ci.yml | 57 ++++++++++++++--------------- composer.json | 2 +- src/Mocks/MockHttpClient.php | 12 ++++--- tests/Fixtures/EchoHandler.php | 23 +++++++----- tests/HttpHandlerTest.php | 26 +++++++++----- tests/MiddlewareTest.php | 66 ++++++++++++++++++++++------------ tests/ReadmeTest.php | 55 +++++++++++++++++----------- 7 files changed, 144 insertions(+), 97 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6d9a9bf..fa81864 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/composer.json b/composer.json index 65a8ccc..cdc092b 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": ">=7.4", + "php": ">=8.0", "ext-curl": "*", "ext-json": "*", "vanilla/garden-utils": "^1.1", diff --git a/src/Mocks/MockHttpClient.php b/src/Mocks/MockHttpClient.php index 116a606..4bc0128 100644 --- a/src/Mocks/MockHttpClient.php +++ b/src/Mocks/MockHttpClient.php @@ -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); @@ -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; } @@ -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; } diff --git a/tests/Fixtures/EchoHandler.php b/tests/Fixtures/EchoHandler.php index de2991e..93f7b5f 100644 --- a/tests/Fixtures/EchoHandler.php +++ b/tests/Fixtures/EchoHandler.php @@ -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); diff --git a/tests/HttpHandlerTest.php b/tests/HttpHandlerTest.php index f24fa53..e8006df 100644 --- a/tests/HttpHandlerTest.php +++ b/tests/HttpHandlerTest.php @@ -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"] + ); } } diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index e2d1db0..8b750e3 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -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; @@ -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")); } /** @@ -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; }; diff --git a/tests/ReadmeTest.php b/tests/ReadmeTest.php index 4aa97e9..c74aee6 100644 --- a/tests/ReadmeTest.php +++ b/tests/ReadmeTest.php @@ -7,6 +7,8 @@ namespace Garden\Http\Tests; +require_once __DIR__ . "/Fixtures/HmacMiddleware.php"; + use Garden\Http\HttpClient; use Garden\Http\HttpResponseException; use Garden\Http\Tests\Fixtures\HmacMiddleware; @@ -15,64 +17,75 @@ /** * Test cases for the README. */ -class ReadmeTest extends TestCase { - public function testBasicExample() { - $api = new HttpClient('http://httpbin.org'); +class ReadmeTest extends TestCase +{ + public function testBasicExample() + { + $api = new HttpClient("http://httpbin.org"); $api->setThrowExceptions(true); - $api->setDefaultHeader('Content-Type', 'application/json'); + $api->setDefaultHeader("Content-Type", "application/json"); // Get some data from the API. - $response = $api->get('/get'); // requests off of base url + $response = $api->get("/get"); // requests off of base url $data = $response->getBody(); // returns array of json decoded data - $response = $api->post('https://httpbin.org/post', ['foo' => 'bar']); + $response = $api->post("https://httpbin.org/post", ["foo" => "bar"]); // Access the response like an array. - $posted = $response['json']; // should be ['foo' => 'bar'] + $posted = $response["json"]; // should be ['foo' => 'bar'] if (!$response->isSuccessful()) { $this->markTestSkipped(); } $this->assertIsArray($data); - $this->assertSame(['foo' => 'bar'], $posted); + $this->assertSame(["foo" => "bar"], $posted); } /** * Test that exceptions can be thrown. */ - public function testExceptionsExample() { + public function testExceptionsExample() + { $this->expectException(HttpResponseException::class); $this->expectExceptionCode(404); - $api = new HttpClient('https://httpbin.org'); + $api = new HttpClient("https://httpbin.org"); $api->setThrowExceptions(true); try { - $api->get('/status/404'); + $api->get("/status/404"); } catch (\Exception $ex) { $code = $ex->getCode(); // should be 404 throw $ex; } } - public function testBasicAuthentication() { - $api = new HttpClient('https://httpbin.org'); - $api->setDefaultOption('auth', ['username', 'password123']); + public function testBasicAuthentication() + { + $api = new HttpClient("https://httpbin.org"); + $api->setDefaultOption("auth", ["username", "password123"]); // This request is made with the default authentication set above. - $r1 = $api->get('/basic-auth/username/password123'); + $r1 = $api->get("/basic-auth/username/password123"); // This request overrides the basic authentication. - $r2 = $api->get('/basic-auth/username/password', [], [], ['auth' => ['username', 'password']]); + $r2 = $api->get( + "/basic-auth/username/password", + [], + [], + ["auth" => ["username", "password"]] + ); $this->assertEquals(200, $r1->getStatusCode()); $this->assertEquals(200, $r2->getStatusCode()); } - public function testRequestMiddleware() { - $api = new HttpClient('https://httpbin.org'); - $api->addMiddleware(new HmacMiddleware('key', 'password')); + public function testRequestMiddleware() + { + $api = new HttpClient("https://httpbin.org"); + $middleware = new HmacMiddleware("key", "password"); + $api->addMiddleware($middleware); - $r = $api->get('/get'); + $r = $api->get("/get"); - $this->assertNotEmpty($r['headers']['Authorization']); + $this->assertNotEmpty($r["headers"]["Authorization"]); } }