Skip to content

Commit

Permalink
Merge pull request #4 from farzai/add-request-decorator
Browse files Browse the repository at this point in the history
Add standart request
  • Loading branch information
parsilver authored Aug 29, 2023
2 parents 6d1dd15 + 09176a8 commit 79b4b29
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 102 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
"guzzlehttp/guzzle": "^7.7",
"guzzlehttp/psr7": "^2.5",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0|^2.0"
"psr/http-message": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.2",
"mockery/mockery": "^1.5",
"pestphp/pest": "^1.20",
"spatie/ray": "^1.28"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function isSuccessfull(): bool;
/**
* Return the json decoded response.
*/
public function json(?string $key = null): mixed;
public function json(string $key = null): mixed;

/**
* Throw an exception if the response is not successfull.
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ResponseExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ResponseExceptionFactory
/**
* Create a new exception instance.
*/
public static function create(ResponseInterface $response, ?Throwable $previous = null)
public static function create(ResponseInterface $response, Throwable $previous = null)
{
$statusCode = $response->statusCode();

Expand Down
22 changes: 22 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Farzai\Transport;

use Farzai\Transport\Traits\PsrRequestTrait;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Psr\Http\Message\RequestInterface as PsrRequestInterface;

class Request implements PsrRequestInterface
{
use PsrRequestTrait;

public function __construct(
string $method,
$uri,
array $headers = [],
$body = null,
string $version = '1.1'
) {
$this->request = new GuzzleRequest($method, $uri, $headers, $body, $version);
}
}
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function isSuccessfull(): bool
/**
* Return the json decoded response.
*/
public function json(?string $key = null): mixed
public function json(string $key = null): mixed
{
if (is_null($this->jsonDecoded)) {
$this->jsonDecoded = @json_decode($this->body(), true) ?: false;
Expand Down
113 changes: 113 additions & 0 deletions src/Traits/PsrRequestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Farzai\Transport\Traits;

use Psr\Http\Message\RequestInterface as PsrRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

trait PsrRequestTrait
{
protected PsrRequestInterface $request;

public function getRequestTarget(): string
{
return $this->request->getRequestTarget();
}

public function withRequestTarget($requestTarget): PsrRequestInterface
{
$this->request = $this->request->withRequestTarget($requestTarget);

return $this;
}

public function getMethod(): string
{
return $this->request->getMethod();
}

public function withMethod($method): PsrRequestInterface
{
$this->request = $this->request->withMethod($method);

return $this;
}

public function getUri(): UriInterface
{
return $this->request->getUri();
}

public function withUri($uri, $preserveHost = false): PsrRequestInterface
{
$this->request = $this->request->withUri($uri, $preserveHost);

return $this;
}

public function getProtocolVersion(): string
{
return $this->request->getProtocolVersion();
}

public function withProtocolVersion($version): PsrRequestInterface
{
$this->request = $this->request->withProtocolVersion($version);

return $this;
}

public function getHeaders(): array
{
return $this->request->getHeaders();
}

public function hasHeader($name): bool
{
return $this->request->hasHeader($name);
}

public function getHeader($name): array
{
return $this->request->getHeader($name);
}

public function getHeaderLine($name): string
{
return $this->request->getHeaderLine($name);
}

public function withHeader($name, $value): PsrRequestInterface
{
$this->request = $this->request->withHeader($name, $value);

return $this;
}

public function withAddedHeader($name, $value): PsrRequestInterface
{
$this->request = $this->request->withAddedHeader($name, $value);

return $this;
}

public function withoutHeader($name): PsrRequestInterface
{
$this->request = $this->request->withoutHeader($name);

return $this;
}

public function getBody(): StreamInterface
{
return $this->request->getBody();
}

public function withBody(StreamInterface $body): PsrRequestInterface
{
$this->request = $this->request->withBody($body);

return $this;
}
}
151 changes: 151 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

use Farzai\Transport\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\StreamInterface;

it('can get the request method', function () {
$request = new Request('GET', new Uri('https://example.com'));

expect($request->getMethod())->toBe('GET');
});

it('can set the request method', function () {
$request = new Request('GET', new Uri('https://example.com'));

$newRequest = $request->withMethod('POST');

expect($newRequest->getMethod())->toBe('POST');
});

it('can get request target', function () {
$request = new Request('GET', new Uri('https://example.com/foo'));

expect($request->getRequestTarget())->toBe('/foo');
});

it('can set request target', function () {
$request = new Request('GET', new Uri('https://example.com'));

$newRequest = $request->withRequestTarget('/foo');

expect($newRequest->getRequestTarget())->toBe('/foo');
});

it('can get the request URI', function () {
$uri = new Uri('https://example.com');
$request = new Request('GET', $uri);

expect($request->getUri())->toBe($uri);
});

it('can set the request URI', function () {
$uri = new Uri('https://example.com');
$request = new Request('GET', $uri);

$newUri = new Uri('https://example.org');
$newRequest = $request->withUri($newUri);

expect($newRequest->getUri())->toBe($newUri);
});

it('can get the request protocol version', function () {
$request = new Request('GET', new Uri('https://example.com'));

expect($request->getProtocolVersion())->toBe('1.1');
});

it('can set the request protocol version', function () {
$request = new Request('GET', new Uri('https://example.com'));

$newRequest = $request->withProtocolVersion('2.0');

expect($newRequest->getProtocolVersion())->toBe('2.0');
});

it('can get the request headers', function () {
$request = new Request('GET', new Uri('https://example.com'), [
'Content-Type' => 'application/json',
]);

expect($request->getHeaders())->toBe([
'Host' => ['example.com'],
'Content-Type' => ['application/json'],
]);
});

it('can set a request header', function () {
$request = new Request('GET', new Uri('https://example.com'));

$newRequest = $request->withHeader('Content-Type', 'application/json');

expect($newRequest->getHeaders())->toBe([
'Host' => ['example.com'],
'Content-Type' => ['application/json'],
]);
});

it('can add a request header', function () {
$request = new Request('GET', new Uri('https://example.com'), [
'Content-Type' => 'application/json',
]);

$newRequest = $request->withAddedHeader('Accept', 'application/json');

expect($newRequest->getHeaders())->toBe([
'Host' => ['example.com'],
'Content-Type' => ['application/json'],
'Accept' => ['application/json'],
]);
});

it('can check exists header', function () {
$request = new Request('GET', new Uri('https://example.com'), [
'Content-Type' => 'application/json',
]);

expect($request->hasHeader('Content-Type'))->toBeTrue();
expect($request->hasHeader('Accept'))->toBeFalse();
});

it('can get header', function () {
$request = new Request('GET', new Uri('https://example.com'), [
'Content-Type' => 'application/json',
]);

expect($request->getHeader('Content-Type'))->toBe(['application/json']);
expect($request->getHeader('Accept'))->toBe([]);
});

it('can remove a request header', function () {
$request = new Request('GET', new Uri('https://example.com'), [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
]);

$newRequest = $request->withoutHeader('Accept');

expect($newRequest->getHeaders())->toBe([
'Host' => ['example.com'],
'Content-Type' => ['application/json'],
]);
});

it('can get a request header line', function () {
$request = new Request('GET', new Uri('https://example.com'), [
'Content-Type' => 'application/json',
]);

expect($request->getHeaderLine('Content-Type'))->toBe('application/json');
});

it('can set the request body', function () {
$request = new Request('GET', new Uri('https://example.com'));

$stream = $this->createMock(StreamInterface::class);
$stream->method('__toString')->willReturn('{"foo":"bar"}');

$newRequest = $request->withBody($stream);

expect((string) $newRequest->getBody())->toBe('{"foo":"bar"}');
});
Loading

0 comments on commit 79b4b29

Please sign in to comment.