Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new class RequestHeaders #14

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
51 changes: 51 additions & 0 deletions src/RequestHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

vjik marked this conversation as resolved.
Show resolved Hide resolved
namespace Yiisoft\RequestProvider;

final class RequestHeaders {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class RequestHeaders {
final class RequestHeaderProvider {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested name is not consistent with RequestCookies.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestCookies is inconsistent with the rest Yii3 packages as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. RequestHeaderProvider is better name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a RequestHeaderCollection?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also good, but it's not a classic collection. Very custom implementation that depends on another class. It's more proxy than collection.
ProxyCollection? 😁

DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param RequestProviderInterface $requestProvider
*/
public function __construct(
private readonly RequestProviderInterface $requestProvider
) {
}

/**
* @param string $name
* @return string|null
*/
public function getHeaderLine(string $name): string|null {
DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
return $this->requestProvider->get()->hasHeader($name) ? $this->requestProvider->get()->getHeaderLine($name) : null;
DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param string $name
* @return array
DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getHeader(string $name): array {
return $this->requestProvider->get()->getHeader($name);
}

/**
* @return array
DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getHeaders(): array {
return $this->requestProvider->get()->getHeaders();
}

/**
* @return array
DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getFirstHeaders(): array {
return array_map(static fn($lines) => $lines[0], $this->requestProvider->get()->getHeaders());
DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param string $name
* @return bool
*/
public function hasHeader(string $name): bool {
return $this->requestProvider->get()->hasHeader($name);
}
}
98 changes: 98 additions & 0 deletions tests/RequestHeadersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class RequestHeadersTest extends TestCase {
final class RequestHeaderProviderTest extends TestCase {

private const HEADER_NAME = 'test';
private const HEADER_VALUE = 'value';

/**
* @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 testGetHeaders(): void {
$requestHeaders = $this->createRequestHeaders([self::HEADER_NAME => [self::HEADER_VALUE]]);

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

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

$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'));
}

/**
* @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('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));

/** @var RequestProviderInterface $requestProvider */
$requestProvider = $this->createMock(RequestProviderInterface::class);
$requestProvider->method('get')->willReturn($serverRequestMock);

return new RequestHeaders($requestProvider);
}
}
Loading