16
16
use Polymorphine \Headers \Cookie \CookieSetup ;
17
17
use Polymorphine \Headers \Cookie \Exception ;
18
18
use Polymorphine \Headers \ResponseHeaders ;
19
- use DateTime ;
19
+ use Polymorphine \ Headers \ Tests \ Fixtures \ FixedDateTime ;
20
20
21
21
require_once __DIR__ . '/Fixtures/time-functions.php ' ;
22
22
23
23
24
24
class HeadersContextCookieTest extends TestCase
25
25
{
26
- public function testInstantiation ()
26
+ public static function cookieData (): iterable
27
+ {
28
+ return [
29
+ ['myCookie=; Path=/; Expires=Thursday, 01-Jan-1970 00:00:00 UTC; MaxAge=-1525132800 ' , [
30
+ 'name ' => 'myCookie ' ,
31
+ 'value ' => null
32
+ ]],
33
+ ['fullCookie=foo; Domain=example.com; Path=/directory/; Expires=Tuesday, 01-May-2018 01:00:00 UTC; MaxAge=3600; Secure; HttpOnly; SameSite=Lax ' , [
34
+ 'name ' => 'fullCookie ' ,
35
+ 'value ' => 'foo ' ,
36
+ 'Secure ' => true ,
37
+ 'MaxAge ' => 3600 ,
38
+ 'HttpOnly ' => true ,
39
+ 'Domain ' => 'example.com ' ,
40
+ 'Path ' => '/directory/ ' ,
41
+ 'SameSite ' => 'Lax '
42
+ ]],
43
+ ['fullCookie=foo; Domain=example.com; Path=/directory/; Expires=Tuesday, 01-May-2018 01:00:00 UTC; MaxAge=3600; Secure; HttpOnly; SameSite=Lax ' , [
44
+ 'name ' => 'fullCookie ' ,
45
+ 'value ' => 'foo ' ,
46
+ 'Secure ' => true ,
47
+ 'Expires ' => FixedDateTime::withOffset (3600 ),
48
+ 'HttpOnly ' => true ,
49
+ 'Domain ' => 'example.com ' ,
50
+ 'Path ' => '/directory/ ' ,
51
+ 'SameSite ' => 'Lax '
52
+ ]]
53
+ ];
54
+ }
55
+
56
+ public static function invalidNames (): iterable
57
+ {
58
+ return [['foo=bar ' ], ['żółty ' ], ['foo{bar} ' ]];
59
+ }
60
+
61
+ public static function invalidValues (): iterable
62
+ {
63
+ return [['foo\bar ' ], ['żółty ' ], ['foo;bar ' ]];
64
+ }
65
+
66
+ public function test_Instantiation ()
27
67
{
28
68
$ this ->assertInstanceOf (CookieSetup::class, $ setup = $ this ->cookieSetup ());
29
69
$ this ->assertInstanceOf (HeadersContextCookie::class, $ setup ->cookie ('new ' ));
30
70
$ this ->assertInstanceOf (HeadersContextCookie::class, $ setup ->permanentCookie ('new ' ));
31
71
$ this ->assertInstanceOf (HeadersContextCookie::class, $ setup ->sessionCookie ('new ' ));
32
72
}
33
73
34
- public function testStandardSetup ()
74
+ public function test_StandardSetup ()
35
75
{
36
76
$ this ->cookieSetup ($ context )
37
- ->expires ($ this ->fixedDate (7200 ))
77
+ ->expires (FixedDateTime::withOffset (7200 ))
78
+ ->secure ()
38
79
->cookie ('name ' )
39
80
->send ('value ' );
40
81
41
- $ expected = ['name=value; Path=/; Expires=Tuesday, 01-May-2018 02:00:00 UTC; MaxAge=7200 ' ];
82
+ $ expected = ['name=value; Path=/; Expires=Tuesday, 01-May-2018 02:00:00 UTC; MaxAge=7200; Secure ' ];
42
83
$ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
43
84
}
44
85
45
- public function testPermanentSetup ()
86
+ public function test_PermanentSetup ()
46
87
{
47
88
$ this ->cookieSetup ($ context )
48
- ->directives (['Expires ' => $ this -> fixedDate (7200 )])
89
+ ->directives (['Expires ' => FixedDateTime:: withOffset (7200 )])
49
90
->permanentCookie ('name ' )
50
91
->send ('value ' );
51
92
52
93
$ expected = ['name=value; Path=/; Expires=Sunday, 30-Apr-2023 00:00:00 UTC; MaxAge=157680000 ' ];
53
94
$ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
54
95
}
55
96
56
- public function testSessionSetup ()
97
+ public function test_SessionSetup ()
57
98
{
58
99
$ this ->cookieSetup ($ context )
59
100
->sessionCookie ('SessionId ' )
@@ -63,18 +104,13 @@ public function testSessionSetup()
63
104
$ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
64
105
}
65
106
66
- public function testName_ReturnsCookieName ()
107
+ public function test_Name_ReturnsCookieName ()
67
108
{
68
109
$ this ->assertSame ('cookieName ' , $ this ->cookieSetup ($ context )->cookie ('cookieName ' )->name ());
69
110
}
70
111
71
- /**
72
- * @dataProvider cookieData
73
- *
74
- * @param string $expectedHeader
75
- * @param array $data
76
- */
77
- public function testConstructorDirectivesSetting (string $ expectedHeader , array $ data )
112
+ /** @dataProvider cookieData */
113
+ public function test_ConstructorDirectivesSetting (string $ expectedHeader , array $ data )
78
114
{
79
115
$ cookie = $ this ->cookieSetup ($ context )
80
116
->directives ($ data )
@@ -83,7 +119,7 @@ public function testConstructorDirectivesSetting(string $expectedHeader, array $
83
119
$ this ->assertEquals ([$ expectedHeader ], $ this ->responseHeader ($ context ));
84
120
}
85
121
86
- public function testHeadersAreAdded ()
122
+ public function test_HeadersAreAdded ()
87
123
{
88
124
$ this ->cookieSetup ($ context )
89
125
->sessionCookie ('cookie1 ' )
@@ -95,18 +131,18 @@ public function testHeadersAreAdded()
95
131
$ this ->assertCount (2 , $ this ->responseHeader ($ context ));
96
132
}
97
133
98
- public function testGivenBothExpiryDirectivesToSetupConstructor_FirstOneIsOverwritten ()
134
+ public function test_GivenBothExpiryDirectivesToSetupConstructor_FirstOneIsOverwritten ()
99
135
{
100
136
$ this ->cookieSetup ($ context )
101
- ->directives (['Expires ' => $ this -> fixedDate (3600 ), 'MaxAge ' => 100 ])
137
+ ->directives (['Expires ' => FixedDateTime:: withOffset (3600 ), 'MaxAge ' => 100 ])
102
138
->cookie ('name ' )
103
139
->send ('value ' );
104
140
105
141
$ expected = ['name=value; Path=/; Expires=Tuesday, 01-May-2018 00:01:40 UTC; MaxAge=100 ' ];
106
142
$ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
107
143
}
108
144
109
- public function testSecureNamePrefix_ForcesSecureDirective ()
145
+ public function test_SecureNamePrefix_ForcesSecureDirective ()
110
146
{
111
147
$ this ->cookieSetup ($ context )
112
148
->directives (['Domain ' => 'example.com ' , 'Path ' => '/test ' ])
@@ -117,7 +153,7 @@ public function testSecureNamePrefix_ForcesSecureDirective()
117
153
$ this ->assertEquals ($ expected , $ this ->responseHeader ($ context ));
118
154
}
119
155
120
- public function testHostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain ()
156
+ public function test_HostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain ()
121
157
{
122
158
$ this ->cookieSetup ($ context )
123
159
->directives (['Domain ' => 'example.com ' , 'Path ' => '/test ' ])
@@ -128,30 +164,22 @@ public function testHostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain()
128
164
$ this ->assertEquals ($ expected , $ this ->responseHeader ($ context ));
129
165
}
130
166
131
- /**
132
- * @dataProvider invalidNames
133
- *
134
- * @param string $invalidName
135
- */
136
- public function testInvalidCharacterInCookieName_ThrowsException (string $ invalidName )
167
+ /** @dataProvider invalidNames */
168
+ public function test_InvalidCharacterInCookieName_ThrowsException (string $ invalidName )
137
169
{
138
170
$ this ->expectException (Exception \IllegalCharactersException::class);
139
171
$ this ->cookieSetup ()->cookie ($ invalidName );
140
172
}
141
173
142
- /**
143
- * @dataProvider invalidValues
144
- *
145
- * @param string $invalidValue
146
- */
147
- public function testInvalidCharacterInCookieValue_ThrowsException (string $ invalidValue )
174
+ /** @dataProvider invalidValues */
175
+ public function test_InvalidCharacterInCookieValue_ThrowsException (string $ invalidValue )
148
176
{
149
177
$ cookie = $ this ->cookieSetup ()->cookie ('testValue ' );
150
178
$ this ->expectException (Exception \IllegalCharactersException::class);
151
179
$ cookie ->send ($ invalidValue );
152
180
}
153
181
154
- public function testGivenCookieWasSent_SendCookie_ThrowsException ()
182
+ public function test_GivenCookieWasSent_SendCookie_ThrowsException ()
155
183
{
156
184
$ cookie = $ this ->cookieSetup ($ context )->cookie ('name ' );
157
185
@@ -160,56 +188,9 @@ public function testGivenCookieWasSent_SendCookie_ThrowsException()
160
188
$ cookie ->send ('value ' );
161
189
}
162
190
163
- public function cookieData (): array
164
- {
165
- return [
166
- ['myCookie=; Path=/; Expires=Thursday, 01-Jan-1970 00:00:00 UTC; MaxAge=-1525132800 ' , [
167
- 'name ' => 'myCookie ' ,
168
- 'value ' => null
169
- ]],
170
- ['fullCookie=foo; Domain=example.com; Path=/directory/; Expires=Tuesday, 01-May-2018 01:00:00 UTC; MaxAge=3600; Secure; HttpOnly; SameSite=Lax ' , [
171
- 'name ' => 'fullCookie ' ,
172
- 'value ' => 'foo ' ,
173
- 'Secure ' => true ,
174
- 'MaxAge ' => 3600 ,
175
- 'HttpOnly ' => true ,
176
- 'Domain ' => 'example.com ' ,
177
- 'Path ' => '/directory/ ' ,
178
- 'SameSite ' => 'Lax '
179
- ]],
180
- ['fullCookie=foo; Domain=example.com; Path=/directory/; Expires=Tuesday, 01-May-2018 01:00:00 UTC; MaxAge=3600; Secure; HttpOnly; SameSite=Lax ' , [
181
- 'name ' => 'fullCookie ' ,
182
- 'value ' => 'foo ' ,
183
- 'Secure ' => true ,
184
- 'Expires ' => $ this ->fixedDate (3600 ),
185
- 'HttpOnly ' => true ,
186
- 'Domain ' => 'example.com ' ,
187
- 'Path ' => '/directory/ ' ,
188
- 'SameSite ' => 'Lax '
189
- ]]
190
- ];
191
- }
192
-
193
- public function invalidNames (): array
194
- {
195
- return [['foo=bar ' ], ['żółty ' ], ['foo{bar} ' ]];
196
- }
197
-
198
- public function invalidValues (): array
199
- {
200
- return [['foo\bar ' ], ['żółty ' ], ['foo;bar ' ]];
201
- }
202
-
203
- private function fixedDate (int $ secondsFromNow = 0 ): DateTime
204
- {
205
- $ date = new DateTime ();
206
- return $ date ->setTimestamp (\Polymorphine \Headers \Cookie \time () + $ secondsFromNow );
207
- }
208
-
209
- private function cookieSetup (&$ context = null ): CookieSetup
191
+ private function cookieSetup (?ResponseHeaders &$ context = null ): CookieSetup
210
192
{
211
- $ context or $ context = new ResponseHeaders ();
212
- return new CookieSetup ($ context );
193
+ return new CookieSetup ($ context ??= new ResponseHeaders ());
213
194
}
214
195
215
196
private function responseHeader (ResponseHeaders $ context ): array
0 commit comments