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 12 commits into
base: master
Choose a base branch
from
34 changes: 34 additions & 0 deletions src/RequestHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

use Psr\Http\Message\ServerRequestInterface;

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? 😁

Copy link
Author

Choose a reason for hiding this comment

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

In the end, what decision did you make?

Copy link
Member

Choose a reason for hiding this comment

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

Let's call it RequestHeaderProvider

DikoIbragimov marked this conversation as resolved.
Show resolved Hide resolved
private ServerRequestInterface $request;

/**
* @param RequestProviderInterface $requestProvider
*/
public function __construct(
RequestProviderInterface $requestProvider
) {
$this->request = $requestProvider->get();
vjik marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param string $name
* @return string|null
*/
public function get(string $name): string|null {
vjik marked this conversation as resolved.
Show resolved Hide resolved
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 {
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
*/
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);
}
}
Loading