Skip to content

Commit

Permalink
code review fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hcan359 committed Oct 27, 2024
1 parent 459ed8f commit fc1f11c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 1.1.0 under development

- Enh #10: Add `yiisoft/middleware-dispatcher` event listener to keep `Request` always available (@xepozz)
- Add `Yiisoft/RequestProvider/RequestCookies` request cookie collection object (@hacan359)
- Add `RequestCookies` class that provides convenient access to request cookies (@hacan359)

## 1.0.0 March 02, 2024

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ final class MyService

### Request cookies collection

You can work with cookie request from any place in you project. Add in config you app in middleware block
You can work with cookies like the following:

```php
class MyClass {
class MyClass
{
public function __construct(
private \Yiisoft\RequestProvider\RequestCookies $cookies
) {}

public function go(): void {
public function go(): void
{
$this->cookies->has(...);
$this->cookies->get(...)
}
Expand Down
2 changes: 1 addition & 1 deletion src/RequestCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function get(string $name): ?string

public function has(string $name): bool
{
return isset($this->cookies[$name]);
return array_key_exists($name, $this->cookies);
}
}
38 changes: 12 additions & 26 deletions tests/RequestCookiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,40 @@

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\RequestProvider\RequestCookies;
use Yiisoft\RequestProvider\RequestProviderInterface;

use function dirname;

final class RequestCookiesTest extends TestCase
{
public function testRequestCookiesGet(): void
public function testGet(): void
{
$requestCoockies = $this->getRequestCookies();
$requestCoockie = $this->getRequestCookies();

$this->assertSame('value', $requestCoockies->get('test'));
$this->assertSame('value', $requestCoockie->get('test'));
}

public function testRequestCookiesHas(): void
public function testHas(): void
{
$requestCoockies = $this->getRequestCookies();
$requestCoockie = $this->getRequestCookies();

$this->assertTrue($requestCoockies->has('test'));
$this->assertTrue($requestCoockie->has('test'));

$this->assertFalse($requestCoockies->has('value'));
$this->assertFalse($requestCoockie->has('value'));
}

private function getRequestCookies(): RequestCookies
{
$container = $this->createContainer();
$requestProvider = $container->get(RequestProviderInterface::class);
/** @var ServerRequestInterface $serverRequestMock */
$serverRequestMock = $this->createMock(ServerRequestInterface::class);
$serverRequestMock->method('getCookieParams')->willReturn([
'test' => 'value',
]);
$requestProvider->set($serverRequestMock);

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

private function createContainer(): Container
{
return new Container(
ContainerConfig::create()->withDefinitions($this->getContainerDefinitions())
);
}

private function getContainerDefinitions(): array
{
return require dirname(__DIR__) . '/config/di-web.php';
return new RequestCookies($requestProvider);
}
}

0 comments on commit fc1f11c

Please sign in to comment.