From 7436675e600e7381834e9433203e83e372b18d83 Mon Sep 17 00:00:00 2001 From: Ignacio Date: Fri, 21 Sep 2018 03:18:20 -0300 Subject: [PATCH] Add support for cookies (#9) --- composer.json | 3 ++- src/Bridge/RequestTransformer.php | 24 ++++++++++++++++++++ tests/Unit/Bridge/RequestTransformerTest.php | 19 ++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fb8d264..5fd564e 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ } ], "require": { - "php": "^7.0" + "php": "^7.0", + "dflydev/fig-cookies": "^1.0" }, "require-dev": { "eaglewu/swoole-ide-helper": "dev-master", diff --git a/src/Bridge/RequestTransformer.php b/src/Bridge/RequestTransformer.php index 821e964..332ff4d 100644 --- a/src/Bridge/RequestTransformer.php +++ b/src/Bridge/RequestTransformer.php @@ -4,6 +4,8 @@ use Slim\Http; use swoole_http_request; +use Dflydev\FigCookies\Cookie; +use Dflydev\FigCookies\FigRequestCookies; class RequestTransformer implements RequestTransformerInterface { @@ -43,9 +45,31 @@ public function toSlim(swoole_http_request $request): Http\Request $slimRequest = $this->handleUploadedFiles($request, $slimRequest); } + $slimRequest = $this->copyCookies($request, $slimRequest); + return $this->copyBody($request, $slimRequest); } + /** + * @param swoole_http_request $request + * @param Http\Request $slimRequest + * + * @return Http\Request + */ + private function copyCookies(swoole_http_request $request, Http\Request $slimRequest): Http\Request + { + if (empty($request->cookie)) { + return $slimRequest; + } + + foreach ($request->cookie as $name => $value) { + $cookie = Cookie::create($name, $value); + $slimRequest = FigRequestCookies::set($slimRequest, $cookie); + } + + return $slimRequest; + } + /** * @param swoole_http_request $request * @param Http\Request $slimRequest diff --git a/tests/Unit/Bridge/RequestTransformerTest.php b/tests/Unit/Bridge/RequestTransformerTest.php index bf41a82..f1194e3 100644 --- a/tests/Unit/Bridge/RequestTransformerTest.php +++ b/tests/Unit/Bridge/RequestTransformerTest.php @@ -4,6 +4,8 @@ use Pachico\SlimSwoole\Bridge; use Slim\Http; +use Dflydev\FigCookies\Cookies; +use Dflydev\FigCookies\FigRequestCookies; class RequestTransformerTest extends \Pachico\SlimSwooleUnitTest\AbstractTestCase { @@ -132,4 +134,21 @@ public function testUploadedFilesAreCopiedProperty() $this->assertSame($output->getUploadedFiles()['name2']->getError(), 0); $this->assertSame($output->getUploadedFiles()['name2']->getSize(), 88); } + + public function testCookiesAreCopiedProperly() + { + $this->swooleRequest->cookie = [ + 'some-cookie-1' => 'some-value-1', + 'some-cookie-2' => 'some-value-2', + 'some-cookie-3' => 'some-value-3', + ]; + + // Act + $output = $this->sut->toSlim($this->swooleRequest); + + // Assert + $cookies = Cookies::fromRequest($output)->getAll(); + $this->assertEquals(count($cookies), 3); + $this->assertEquals(FigRequestCookies::get($output, 'some-cookie-2')->getValue(), 'some-value-2'); + } }