generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from farzai/add-request-decorator
Add standart request
- Loading branch information
Showing
9 changed files
with
405 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}'); | ||
}); |
Oops, something went wrong.