Skip to content

Commit

Permalink
Add new class for request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
DikoIbragimov committed Dec 11, 2024
1 parent 665a0f0 commit 0382689
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/RequestHeaders.php
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);
}
}
58 changes: 58 additions & 0 deletions tests/RequestHeadersTest.php
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);
}
}

0 comments on commit 0382689

Please sign in to comment.