generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sergei Predvoditelev <[email protected]> Co-authored-by: Alexander Makarov <[email protected]>
- Loading branch information
1 parent
bd57043
commit 0043ff5
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestProvider; | ||
|
||
final class RequestCookies | ||
{ | ||
/** | ||
* @var array The cookies in this collection (indexed by the cookie name). | ||
* | ||
* @psalm-var array<string, string> | ||
*/ | ||
private array $cookies; | ||
|
||
public function __construct(RequestProviderInterface $requestProvider) | ||
{ | ||
/** @psalm-var array<string, string> */ | ||
$this->cookies = $requestProvider->get()->getCookieParams(); | ||
} | ||
|
||
public function get(string $name): ?string | ||
{ | ||
return $this->cookies[$name] ?? null; | ||
} | ||
|
||
public function has(string $name): bool | ||
{ | ||
return array_key_exists($name, $this->cookies); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\RequestProvider\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Yiisoft\RequestProvider\RequestCookies; | ||
use Yiisoft\RequestProvider\RequestProviderInterface; | ||
|
||
final class RequestCookiesTest extends TestCase | ||
{ | ||
public function testGet(): void | ||
{ | ||
$requestCookies = $this->createRequestCookies(['test' => 'value']); | ||
|
||
$this->assertSame('value', $requestCookies->get('test')); | ||
} | ||
|
||
public function testHas(): void | ||
{ | ||
$requestCookies = $this->createRequestCookies(['test' => 'value']); | ||
|
||
$this->assertTrue($requestCookies->has('test')); | ||
$this->assertFalse($requestCookies->has('non-exist')); | ||
} | ||
|
||
private function createRequestCookies(array $cookies = []): RequestCookies | ||
{ | ||
/** @var ServerRequestInterface $serverRequestMock */ | ||
$serverRequestMock = $this->createMock(ServerRequestInterface::class); | ||
$serverRequestMock | ||
->method('getCookieParams') | ||
->willReturn($cookies); | ||
|
||
/** @var RequestProviderInterface $requestProvider */ | ||
$requestProvider = $this->createMock(RequestProviderInterface::class); | ||
$requestProvider->method('get')->willReturn($serverRequestMock); | ||
|
||
return new RequestCookies($requestProvider); | ||
} | ||
} |