Skip to content

Commit

Permalink
Using a dynamic method for getting a request. Renaming methods and ad…
Browse files Browse the repository at this point in the history
…ding new methods for getting request headers in different formats and corresponding tests
  • Loading branch information
DikoIbragimov committed Dec 11, 2024
1 parent 0382689 commit d48b8e6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
37 changes: 27 additions & 10 deletions src/RequestHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,50 @@

namespace Yiisoft\RequestProvider;

use Psr\Http\Message\ServerRequestInterface;

final class RequestHeaders {
private ServerRequestInterface $request;

/**
* @param RequestProviderInterface $requestProvider
*/
public function __construct(
RequestProviderInterface $requestProvider
private readonly 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;
public function getHeaderLine(string $name): string|null {
return $this->requestProvider->get()->hasHeader($name) ? $this->requestProvider->get()->getHeaderLine($name) : null;
}

/**
* @param string $name
* @return array
*/
public function getHeader(string $name): array {
return $this->requestProvider->get()->getHeader($name);
}

/**
* @return array
*/
public function getHeaders(): array {
return $this->requestProvider->get()->getHeaders();
}

/**
* @return array
*/
public function getFirstHeaders(): array {
return array_map(static fn($lines) => $lines[0], $this->requestProvider->get()->getHeaders());
}

/**
* @param string $name
* @return bool
*/
public function has(string $name): bool {
return $this->request->hasHeader($name);
public function hasHeader(string $name): bool {
return $this->requestProvider->get()->hasHeader($name);
}
}
50 changes: 45 additions & 5 deletions tests/RequestHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,53 @@ final class RequestHeadersTest extends TestCase {

/**
* @return void
* @throws Exception
*/
public function testGetHeaderLine(): void {
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]);

$this->assertSame(self::HEADER_VALUE, $requestHeaders->getHeaderLine(self::HEADER_NAME));
}

/**
* @return void
* @throws Exception
*/
public function testGetHeader(): void {
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]);

$this->assertSame([self::HEADER_VALUE], $requestHeaders->getHeader(self::HEADER_NAME));
}

/**
* @return void
* @throws Exception
*/
public function testGet(): void {
public function testGetHeaders(): void {
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]);

$this->assertSame(self::HEADER_VALUE, $requestHeaders->get(self::HEADER_NAME));
$this->assertSame([self::HEADER_NAME => [self::HEADER_VALUE]], $requestHeaders->getHeaders());
}

/**
* @return void
* @throws Exception
*/
public function testHas(): void {
public function testGetFirstHeader(): void {
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]);

$this->assertTrue($requestHeaders->has(self::HEADER_NAME));
$this->assertFalse($requestHeaders->has('non-exist'));
$this->assertSame([self::HEADER_NAME => self::HEADER_VALUE], $requestHeaders->getFirstHeaders());
}

/**
* @return void
* @throws Exception
*/
public function testHasHeader(): void {
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]);

$this->assertTrue($requestHeaders->hasHeader(self::HEADER_NAME));
$this->assertFalse($requestHeaders->hasHeader('non-exist'));
}

/**
Expand All @@ -45,6 +77,14 @@ private function createRequestHeaders(array $headers = []): RequestHeaders {
->method('getHeaderLine')
->willReturnCallback(fn(string $name): string => $headers[$name][0] ?? null);

$serverRequestMock
->method('getHeader')
->willReturnCallback(fn(string $name): array => $headers[$name] ?? []);

$serverRequestMock
->method('getHeaders')
->willReturnCallback(fn(): array => $headers);

$serverRequestMock
->method('hasHeader')
->willReturnCallback(fn(string $name) => array_key_exists($name, $headers));
Expand Down

0 comments on commit d48b8e6

Please sign in to comment.