Skip to content

Commit 92a1736

Browse files
committed
Added cookieSetup method to ResponseHeaders
1 parent 63eb4d9 commit 92a1736

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
php composer.phar require polymorphine/headers
1010

1111
### Basic usage
12+
##### *Set-Cookie header*
1213

13-
1. Instantiate a cookie setup with `ResponseHeaders` context and (*optional*) array
14-
of directives/attributes (see [`resetDirectives()`](/src/Cookie/CookieSetup.php#L54) method):
14+
1. Instantiate a cookie setup using `ResponseHeaders` context and configure with array
15+
of directives/attributes (or builder methods) when their value needs to be specified
16+
(see [`directives()`](/src/Cookie/CookieSetup.php#L48) method):
1517

1618
$context = new ResponseHeaders();
17-
$cookieSetup = new CookieSetup($context, [
19+
$cookieSetup = $context->directives([
1820
'Domain' => 'example.com',
1921
'Path' => '/admin',
2022
'Expires' => new DateTime(...),
@@ -40,7 +42,7 @@
4042

4143
Each cookie can send/revoke header only once
4244

43-
### Directives and Attributes
45+
##### Directives and Attributes
4446

4547
Directives are used according to [RFC6265](https://tools.ietf.org/html/rfc6265#section-4.1.2)
4648
section about Set-Cookie header attributes (except relatively new `SameSite` directive). Their
@@ -55,7 +57,7 @@ Here are some class-specific rules for setting those directives:
5557
If both directives will be passed into constructor or `directivesArray()` method,
5658
last value will be used due to overwrite.
5759

58-
### Cookie with predefined directives
60+
##### Cookie with predefined directives
5961

6062
`CookieSetup` has two alternative methods creating `Cookie` instance: `CookieSetup::permanentCookie()` and
6163
`CookieSetup::sessionCookie()`.

src/Cookie/CookieSetup.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ class CookieSetup
2323
private $responseHeaders;
2424
private $directives;
2525

26-
/**
27-
* @see CookieSetup::resetDirectives() for $directives parameter details
28-
*
29-
* @param ResponseHeaders $responseHeaders
30-
* @param array $directives
31-
*/
32-
public function __construct(ResponseHeaders $responseHeaders, array $directives = [])
26+
public function __construct(ResponseHeaders $responseHeaders)
3327
{
3428
$this->responseHeaders = $responseHeaders;
35-
$this->resetDirectives($directives);
29+
$this->directives();
3630
}
3731

3832
/**
@@ -51,7 +45,7 @@ public function __construct(ResponseHeaders $responseHeaders, array $directives
5145
*
5246
* @return static
5347
*/
54-
public function resetDirectives(array $directives): self
48+
public function directives(array $directives = []): self
5549
{
5650
$this->directives = [
5751
'Domain' => null,

src/ResponseHeaders.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Polymorphine\Headers;
1313

14+
use Polymorphine\Headers\Cookie\CookieSetup;
1415
use Psr\Http\Server\MiddlewareInterface;
1516
use Psr\Http\Server\RequestHandlerInterface;
1617
use Psr\Http\Message\ServerRequestInterface;
@@ -42,4 +43,9 @@ public function push(Header $header): void
4243
{
4344
$this->headers[] = $header;
4445
}
46+
47+
public function cookieSetup(): CookieSetup
48+
{
49+
return new CookieSetup($this);
50+
}
4551
}

tests/HeadersContextCookieTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testInstantiation()
3333

3434
public function testStandardSetup()
3535
{
36-
$this->cookieSetup($context, ['Expires' => $this->fixedDate(7200)])
36+
$this->cookieSetup($context)->directives(['Expires' => $this->fixedDate(7200)])
3737
->cookie('name')
3838
->send('value');
3939

@@ -43,7 +43,8 @@ public function testStandardSetup()
4343

4444
public function testPermanentSetup()
4545
{
46-
$this->cookieSetup($context, ['Expires' => $this->fixedDate(7200)])
46+
$this->cookieSetup($context)
47+
->directives(['Expires' => $this->fixedDate(7200)])
4748
->permanentCookie('name')
4849
->send('value');
4950

@@ -74,7 +75,8 @@ public function testName_ReturnsCookieName()
7475
*/
7576
public function testConstructorDirectivesSetting(string $expectedHeader, array $data)
7677
{
77-
$cookie = $this->cookieSetup($context, $data)
78+
$cookie = $this->cookieSetup($context)
79+
->directives($data)
7880
->cookie($data['name']);
7981
$data['value'] ? $cookie->send($data['value']) : $cookie->revoke();
8082
$this->assertEquals([$expectedHeader], $this->responseHeader($context));
@@ -94,7 +96,8 @@ public function testHeadersAreAdded()
9496

9597
public function testGivenBothExpiryDirectivesToSetupConstructor_FirstOneIsOverwritten()
9698
{
97-
$this->cookieSetup($context, ['Expires' => $this->fixedDate(3600), 'MaxAge' => 100])
99+
$this->cookieSetup($context)
100+
->directives(['Expires' => $this->fixedDate(3600), 'MaxAge' => 100])
98101
->cookie('name')
99102
->send('value');
100103

@@ -104,7 +107,8 @@ public function testGivenBothExpiryDirectivesToSetupConstructor_FirstOneIsOverwr
104107

105108
public function testSecureNamePrefix_ForcesSecureDirective()
106109
{
107-
$this->cookieSetup($context, ['Domain' => 'example.com', 'Path' => '/test'])
110+
$this->cookieSetup($context)
111+
->directives(['Domain' => 'example.com', 'Path' => '/test'])
108112
->cookie('__SECURE-name')
109113
->send('test');
110114

@@ -114,7 +118,8 @@ public function testSecureNamePrefix_ForcesSecureDirective()
114118

115119
public function testHostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain()
116120
{
117-
$this->cookieSetup($context, ['Domain' => 'example.com', 'Path' => '/test'])
121+
$this->cookieSetup($context)
122+
->directives(['Domain' => 'example.com', 'Path' => '/test'])
118123
->cookie('__host-name')
119124
->send('test');
120125

@@ -199,10 +204,10 @@ private function fixedDate(int $secondsFromNow = 0): DateTime
199204
return (new DateTime())->setTimestamp(\Polymorphine\Headers\Cookie\time() + $secondsFromNow);
200205
}
201206

202-
private function cookieSetup(&$context = null, array $attributes = []): CookieSetup
207+
private function cookieSetup(&$context = null): CookieSetup
203208
{
204209
$context or $context = new ResponseHeaders();
205-
return new CookieSetup($context, $attributes);
210+
return new CookieSetup($context);
206211
}
207212

208213
private function responseHeader(ResponseHeaders $context): array

tests/ResponseHeadersTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Polymorphine\Headers\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Polymorphine\Headers\Cookie\CookieSetup;
1516
use Polymorphine\Headers\ResponseHeaders;
1617
use Polymorphine\Headers\Header;
1718
use Polymorphine\Headers\Tests\Doubles\FakeHeader;
@@ -36,6 +37,11 @@ public function testAddHeaders()
3637
$this->assertSame(['Set-Cookie' => ['default=value', 'name=value']], $this->response($headers)->getHeaders());
3738
}
3839

40+
public function testCookieSetup()
41+
{
42+
$this->assertInstanceOf(CookieSetup::class, $this->middleware()->cookieSetup());
43+
}
44+
3945
private function middleware(Header ...$headers)
4046
{
4147
return new ResponseHeaders(...$headers);

0 commit comments

Comments
 (0)