Skip to content
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

add psr7 support #6

Merged
merged 7 commits into from
Nov 21, 2024
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
21 changes: 21 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ jobs:
run: vendor/bin/phpunit
env:
DATABASE_DRIVER: none

psr-7:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup PHP 8.3
uses: shivammathur/setup-php@v2
with:
php-version: 8.3

- name: composer install
run: composer install

- name: run tests
run: vendor/bin/phpunit --testsuite=psr7
env:
DATABASE_DRIVER: none

int-psql:
runs-on: ubuntu-latest
Expand Down
8 changes: 0 additions & 8 deletions _/Http/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@
class Request
{
private ParameterBag $request;

private ParameterBag $query;

private ParameterBag $attributes;

private ParameterBag $cookies;

private ParameterBag $server;

private ParameterBag $files;

private ParameterBag $content;

private ParameterBag $header;

private Session $session;

public function __construct(
Expand Down
94 changes: 94 additions & 0 deletions _/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

use RuntimeException;
use Override;

class CurlHttpClient implements HttpClientInterface
{
#[Override]
public function request(
string $uri,
string $method = 'get',
array $header = [],
?string $payload = null,
): HttpResponse {
if ($uri === '') {
throw new RuntimeException('empty uri passed');
}

$c = \curl_init();

\curl_setopt($c, \CURLOPT_CONNECTTIMEOUT, 0);
\curl_setopt($c, \CURLOPT_TIMEOUT, 30);

// this makes signal handling possible
\curl_setopt($c, \CURLOPT_PROGRESSFUNCTION, static function () {});

\curl_setopt($c, \CURLOPT_URL, $uri);
\curl_setopt($c, \CURLOPT_RETURNTRANSFER, true);
\curl_setopt(
$c,
\CURLOPT_USERAGENT,
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.7 Safari/537.36',
);

\curl_setopt($c, \CURLOPT_HTTPHEADER, $header);

$responseHeaders = [];

\curl_setopt(
$c,
\CURLOPT_HEADERFUNCTION,
static function ($curl, $header) use (&$responseHeaders) {
$len = \strlen((string) $header);
$header = \explode(':', (string) $header, 2);

if (\count($header) < 2) {
return $len;
}

$name = \strtolower(\trim($header[0]));
$responseHeaders[$name] = \trim($header[1]);

return $len;
},
);

switch ($method) {
case 'get':
break;
case 'post':
\curl_setopt($c, \CURLOPT_POST, true);

if ($payload !== null) {
\curl_setopt($c, \CURLOPT_POSTFIELDS, $payload);
}

break;
default: throw new RuntimeException('method not supported');
}

$response = \curl_exec($c);

if (!\is_string($response)) {
throw new RuntimeException('request failed');
}

if (\curl_errno($c) === 28) {
throw new HttpRequestTimeoutException("timeout connecting to: $uri");
}

$responseCode = \curl_getinfo($c, \CURLINFO_RESPONSE_CODE);
assert(\is_int($responseCode));

return new HttpResponse(
$responseCode,
$response,
$responseHeaders,
);
}
}
44 changes: 44 additions & 0 deletions _/HttpClient/DumpingHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

use Override;

class DumpingHttpClient implements HttpClientInterface
{
private int $requestNumber = 0;

public function __construct(
private readonly HttpClientInterface $client,
private readonly string $path,
) {}

#[Override]
public function request(
string $uri,
string $method = 'get',
array $header = [],
?string $payload = null,
): HttpResponse {
$response = $this->client->request($uri, $method, $header, $payload);

++$this->requestNumber;

\mkdir("{$this->path}/{$this->requestNumber}", recursive: true);

\file_put_contents("{$this->path}/{$this->requestNumber}/request.json", \json_encode([
'method' => $method,
'path' => $uri,
'header' => $header,
'payload' => $payload,
], JSON_THROW_ON_ERROR));

\file_put_contents("{$this->path}/{$this->requestNumber}/payload.json", $response->response);
\file_put_contents("{$this->path}/{$this->requestNumber}/header.json", \json_encode($response->header, \JSON_THROW_ON_ERROR));
\file_put_contents("{$this->path}/{$this->requestNumber}/statuscode.json", $response->statusCode);

return $response;
}
}
20 changes: 20 additions & 0 deletions _/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

interface HttpClientInterface
{
/**
* @param array<string> $header
*
* @throws HttpRequestTimeoutException
*/
public function request(
string $uri,
string $method = 'get',
array $header = [],
?string $payload = null,
): HttpResponse;
}
18 changes: 18 additions & 0 deletions _/HttpClient/HttpRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

final readonly class HttpRequest
{
/**
* @param array<string,string> $header
*/
public function __construct(
public string $uri,
public string $method = 'get',
public array $header = [],
public ?string $payload = null,
) {}
}
9 changes: 9 additions & 0 deletions _/HttpClient/HttpRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

use RuntimeException;

class HttpRequestException extends RuntimeException {}
7 changes: 7 additions & 0 deletions _/HttpClient/HttpRequestRetryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

class HttpRequestRetryException extends HttpRequestException {}
7 changes: 7 additions & 0 deletions _/HttpClient/HttpRequestTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

class HttpRequestTimeoutException extends HttpRequestException {}
29 changes: 29 additions & 0 deletions _/HttpClient/HttpResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

final readonly class HttpResponse
{
/**
* @param array<string,string> $header
*/
public function __construct(
public int $statusCode,
public string $response = '',
public array $header = [],
) {}

/**
* @return array<string,mixed>
*/
public function __debugInfo(): array
{
return [
'statusCode' => $this->statusCode,
'header' => \json_encode($this->header, JSON_THROW_ON_ERROR),
'response' => $this->response,
];
}
}
55 changes: 55 additions & 0 deletions _/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\HttpClient;

use Override;
use RuntimeException;

final class MockHttpClient implements HttpClientInterface
{
/** @var HttpResponse[] */
private array $responses;

/** @var HttpRequest[] */
private array $requests = [];

public function __construct(
HttpResponse ...$responses,
) {
$this->responses = $responses;
}

public function addResponse(HttpResponse ... $responses): static
{
foreach ($responses as $response) {
$this->responses[] = $response;
}
return $this;
}

/**
* @return HttpRequest[]
*/
public function getRequests(): array
{
return $this->requests;
}

#[Override]
public function request(
string $uri,
string $method = 'get',
array $header = [],
?string $payload = null,
): HttpResponse {
$this->requests[] = new HttpRequest($uri, $method, $header, $payload);

if (count($this->responses) === 0) {
throw new RuntimeException("no responses left for serving: {$method} {$uri}");
}

return \array_shift($this->responses);
}
}
Loading