generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
665a0f0
commit 0382689
Showing
2 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Yiisoft\RequestProvider; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
final class RequestHeaders { | ||
private ServerRequestInterface $request; | ||
|
||
/** | ||
* @param RequestProviderInterface $requestProvider | ||
*/ | ||
public function __construct( | ||
RequestProviderInterface $requestProvider | ||
) { | ||
$this->request = $requestProvider->get(); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return string|null | ||
*/ | ||
public function get(string $name): string|null { | ||
return $this->request->hasHeader($name) ? $this->request->getHeaderLine($name) : null; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return bool | ||
*/ | ||
public function has(string $name): bool { | ||
return $this->request->hasHeader($name); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestProvider\Tests; | ||
|
||
use PHPUnit\Framework\MockObject\Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Yiisoft\RequestProvider\RequestHeaders; | ||
use Yiisoft\RequestProvider\RequestProviderInterface; | ||
|
||
final class RequestHeadersTest extends TestCase { | ||
private const HEADER_NAME = 'test'; | ||
private const HEADER_VALUE = 'value'; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testGet(): void { | ||
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]); | ||
|
||
$this->assertSame(self::HEADER_VALUE, $requestHeaders->get(self::HEADER_NAME)); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testHas(): void { | ||
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]); | ||
|
||
$this->assertTrue($requestHeaders->has(self::HEADER_NAME)); | ||
$this->assertFalse($requestHeaders->has('non-exist')); | ||
} | ||
|
||
/** | ||
* @param array $headers | ||
* @return RequestHeaders | ||
* @throws Exception | ||
*/ | ||
private function createRequestHeaders(array $headers = []): RequestHeaders { | ||
/** @var ServerRequestInterface $serverRequestMock */ | ||
$serverRequestMock = $this->createMock(ServerRequestInterface::class); | ||
$serverRequestMock | ||
->method('getHeaderLine') | ||
->willReturnCallback(fn(string $name): string => $headers[$name][0] ?? null); | ||
|
||
$serverRequestMock | ||
->method('hasHeader') | ||
->willReturnCallback(fn(string $name) => array_key_exists($name, $headers)); | ||
|
||
/** @var RequestProviderInterface $requestProvider */ | ||
$requestProvider = $this->createMock(RequestProviderInterface::class); | ||
$requestProvider->method('get')->willReturn($serverRequestMock); | ||
|
||
return new RequestHeaders($requestProvider); | ||
} | ||
} |