1616use Polymorphine \Headers \Cookie \CookieSetup ;
1717use Polymorphine \Headers \Cookie \Exception ;
1818use Polymorphine \Headers \ResponseHeaders ;
19- use DateTime ;
19+ use Polymorphine \ Headers \ Tests \ Fixtures \ FixedDateTime ;
2020
2121require_once __DIR__ . '/Fixtures/time-functions.php ' ;
2222
2323
2424class HeadersContextCookieTest extends TestCase
2525{
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 ()
2767 {
2868 $ this ->assertInstanceOf (CookieSetup::class, $ setup = $ this ->cookieSetup ());
2969 $ this ->assertInstanceOf (HeadersContextCookie::class, $ setup ->cookie ('new ' ));
3070 $ this ->assertInstanceOf (HeadersContextCookie::class, $ setup ->permanentCookie ('new ' ));
3171 $ this ->assertInstanceOf (HeadersContextCookie::class, $ setup ->sessionCookie ('new ' ));
3272 }
3373
34- public function testStandardSetup ()
74+ public function test_StandardSetup ()
3575 {
3676 $ this ->cookieSetup ($ context )
37- ->expires ($ this ->fixedDate (7200 ))
77+ ->expires (FixedDateTime::withOffset (7200 ))
78+ ->secure ()
3879 ->cookie ('name ' )
3980 ->send ('value ' );
4081
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 ' ];
4283 $ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
4384 }
4485
45- public function testPermanentSetup ()
86+ public function test_PermanentSetup ()
4687 {
4788 $ this ->cookieSetup ($ context )
48- ->directives (['Expires ' => $ this -> fixedDate (7200 )])
89+ ->directives (['Expires ' => FixedDateTime:: withOffset (7200 )])
4990 ->permanentCookie ('name ' )
5091 ->send ('value ' );
5192
5293 $ expected = ['name=value; Path=/; Expires=Sunday, 30-Apr-2023 00:00:00 UTC; MaxAge=157680000 ' ];
5394 $ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
5495 }
5596
56- public function testSessionSetup ()
97+ public function test_SessionSetup ()
5798 {
5899 $ this ->cookieSetup ($ context )
59100 ->sessionCookie ('SessionId ' )
@@ -63,18 +104,13 @@ public function testSessionSetup()
63104 $ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
64105 }
65106
66- public function testName_ReturnsCookieName ()
107+ public function test_Name_ReturnsCookieName ()
67108 {
68109 $ this ->assertSame ('cookieName ' , $ this ->cookieSetup ($ context )->cookie ('cookieName ' )->name ());
69110 }
70111
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 )
78114 {
79115 $ cookie = $ this ->cookieSetup ($ context )
80116 ->directives ($ data )
@@ -83,7 +119,7 @@ public function testConstructorDirectivesSetting(string $expectedHeader, array $
83119 $ this ->assertEquals ([$ expectedHeader ], $ this ->responseHeader ($ context ));
84120 }
85121
86- public function testHeadersAreAdded ()
122+ public function test_HeadersAreAdded ()
87123 {
88124 $ this ->cookieSetup ($ context )
89125 ->sessionCookie ('cookie1 ' )
@@ -95,18 +131,18 @@ public function testHeadersAreAdded()
95131 $ this ->assertCount (2 , $ this ->responseHeader ($ context ));
96132 }
97133
98- public function testGivenBothExpiryDirectivesToSetupConstructor_FirstOneIsOverwritten ()
134+ public function test_GivenBothExpiryDirectivesToSetupConstructor_FirstOneIsOverwritten ()
99135 {
100136 $ this ->cookieSetup ($ context )
101- ->directives (['Expires ' => $ this -> fixedDate (3600 ), 'MaxAge ' => 100 ])
137+ ->directives (['Expires ' => FixedDateTime:: withOffset (3600 ), 'MaxAge ' => 100 ])
102138 ->cookie ('name ' )
103139 ->send ('value ' );
104140
105141 $ expected = ['name=value; Path=/; Expires=Tuesday, 01-May-2018 00:01:40 UTC; MaxAge=100 ' ];
106142 $ this ->assertSame ($ expected , $ this ->responseHeader ($ context ));
107143 }
108144
109- public function testSecureNamePrefix_ForcesSecureDirective ()
145+ public function test_SecureNamePrefix_ForcesSecureDirective ()
110146 {
111147 $ this ->cookieSetup ($ context )
112148 ->directives (['Domain ' => 'example.com ' , 'Path ' => '/test ' ])
@@ -117,7 +153,7 @@ public function testSecureNamePrefix_ForcesSecureDirective()
117153 $ this ->assertEquals ($ expected , $ this ->responseHeader ($ context ));
118154 }
119155
120- public function testHostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain ()
156+ public function test_HostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain ()
121157 {
122158 $ this ->cookieSetup ($ context )
123159 ->directives (['Domain ' => 'example.com ' , 'Path ' => '/test ' ])
@@ -128,30 +164,22 @@ public function testHostNamePrefix_ForceSecureRootPathDirectivesWithoutDomain()
128164 $ this ->assertEquals ($ expected , $ this ->responseHeader ($ context ));
129165 }
130166
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 )
137169 {
138170 $ this ->expectException (Exception \IllegalCharactersException::class);
139171 $ this ->cookieSetup ()->cookie ($ invalidName );
140172 }
141173
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 )
148176 {
149177 $ cookie = $ this ->cookieSetup ()->cookie ('testValue ' );
150178 $ this ->expectException (Exception \IllegalCharactersException::class);
151179 $ cookie ->send ($ invalidValue );
152180 }
153181
154- public function testGivenCookieWasSent_SendCookie_ThrowsException ()
182+ public function test_GivenCookieWasSent_SendCookie_ThrowsException ()
155183 {
156184 $ cookie = $ this ->cookieSetup ($ context )->cookie ('name ' );
157185
@@ -160,56 +188,9 @@ public function testGivenCookieWasSent_SendCookie_ThrowsException()
160188 $ cookie ->send ('value ' );
161189 }
162190
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
210192 {
211- $ context or $ context = new ResponseHeaders ();
212- return new CookieSetup ($ context );
193+ return new CookieSetup ($ context ??= new ResponseHeaders ());
213194 }
214195
215196 private function responseHeader (ResponseHeaders $ context ): array
0 commit comments