@@ -68,6 +68,32 @@ private function createSessionCookieRequest(string $sessionName = null, $session
68
68
);
69
69
}
70
70
71
+ /**
72
+ * @param array $options Custom session options (without the "session" namespace)
73
+ * @return array Return the original (and overwritten) namespaced ini settings
74
+ */
75
+ private function applyCustomSessionOptions (array $ options )
76
+ {
77
+ $ ini = [];
78
+ foreach ($ options as $ key => $ value ) {
79
+ $ ini_key = "session. {$ key }" ;
80
+ $ ini [$ ini_key ] = ini_get ($ ini_key );
81
+ ini_set ($ ini_key , strval (is_bool ($ value ) ? intval ($ value ) : $ value ));
82
+ }
83
+
84
+ return $ ini ;
85
+ }
86
+
87
+ /**
88
+ * @param array $ini The original session namespaced ini settings
89
+ */
90
+ private function restoreOriginalSessionIniSettings (array $ ini )
91
+ {
92
+ foreach ($ ini as $ key => $ value ) {
93
+ ini_set ($ key , $ value );
94
+ }
95
+ }
96
+
71
97
public function testInitializeSessionFromRequestInitializesSessionWithGeneratedIdentifierIfNoSessionCookiePresent ()
72
98
{
73
99
$ this ->assertSame (PHP_SESSION_NONE , session_status ());
@@ -178,8 +204,9 @@ public function testPersistSessionIfSessionHasContents()
178
204
179
205
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsNocache ()
180
206
{
181
- $ ini = ini_get ('session.cache_limiter ' );
182
- ini_set ('session.cache_limiter ' , 'nocache ' );
207
+ $ ini = $ this ->applyCustomSessionOptions ([
208
+ 'cache_limiter ' => 'nocache ' ,
209
+ ]);
183
210
184
211
$ persistence = new PhpSessionPersistence ();
185
212
@@ -191,18 +218,18 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
191
218
$ this ->assertSame ($ response ->getHeaderLine ('Cache-Control ' ), 'no-store, no-cache, must-revalidate ' );
192
219
$ this ->assertSame ($ response ->getHeaderLine ('Pragma ' ), 'no-cache ' );
193
220
194
- ini_set ( ' session.cache_limiter ' , $ ini );
221
+ $ this -> restoreOriginalSessionIniSettings ( $ ini );
195
222
}
196
223
197
224
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsPublic ()
198
225
{
199
226
$ expire = 111 ;
200
227
$ maxAge = 60 * $ expire ;
201
228
202
- $ ini_limiter = ini_get ( ' session.cache_limiter ' );
203
- $ ini_expire = ini_get ( ' session.cache_expire ' );
204
- ini_set ( ' session.cache_limiter ' , ' public ' );
205
- ini_set ( ' session.cache_expire ' , $ expire );
229
+ $ ini = $ this -> applyCustomSessionOptions ([
230
+ ' cache_limiter ' => ' public ' ,
231
+ ' cache_expire ' => $ expire ,
232
+ ] );
206
233
207
234
$ persistence = new PhpSessionPersistence ();
208
235
@@ -221,19 +248,18 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
221
248
$ this ->assertLessThanOrEqual ($ expires , $ expiresMax );
222
249
$ this ->assertSame ($ response ->getHeaderLine ('Cache-Control ' ), $ control );
223
250
224
- ini_set ('session.cache_limiter ' , $ ini_limiter );
225
- ini_set ('session.cache_expire ' , $ ini_expire );
251
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
226
252
}
227
253
228
254
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsPrivate ()
229
255
{
230
256
$ expire = 222 ;
231
257
$ maxAge = 60 * $ expire ;
232
258
233
- $ ini_limiter = ini_get ( ' session.cache_limiter ' );
234
- $ ini_expire = ini_get ( ' session.cache_expire ' );
235
- ini_set ( ' session.cache_limiter ' , ' private ' );
236
- ini_set ( ' session.cache_expire ' , $ expire );
259
+ $ ini = $ this -> applyCustomSessionOptions ([
260
+ ' cache_limiter ' => ' private ' ,
261
+ ' cache_expire ' => $ expire ,
262
+ ] );
237
263
238
264
$ persistence = new PhpSessionPersistence ();
239
265
@@ -247,19 +273,18 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
247
273
$ this ->assertSame ($ response ->getHeaderLine ('Expires ' ), $ expires );
248
274
$ this ->assertSame ($ response ->getHeaderLine ('Cache-Control ' ), $ control );
249
275
250
- ini_set ('session.cache_limiter ' , $ ini_limiter );
251
- ini_set ('session.cache_expire ' , $ ini_expire );
276
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
252
277
}
253
278
254
279
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsPrivateNoExpire ()
255
280
{
256
281
$ expire = 333 ;
257
282
$ maxAge = 60 * $ expire ;
258
283
259
- $ ini_limiter = ini_get ( ' session.cache_limiter ' );
260
- $ ini_expire = ini_get ( ' session.cache_expire ' );
261
- ini_set ( ' session.cache_limiter ' , ' private_no_expire ' );
262
- ini_set ( ' session.cache_expire ' , $ expire );
284
+ $ ini = $ this -> applyCustomSessionOptions ([
285
+ ' cache_limiter ' => ' private_no_expire ' ,
286
+ ' cache_expire ' => $ expire ,
287
+ ] );
263
288
264
289
$ persistence = new PhpSessionPersistence ();
265
290
@@ -273,14 +298,14 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
273
298
$ this ->assertSame ('' , $ response ->getHeaderLine ('Expires ' ));
274
299
$ this ->assertSame ($ control , $ response ->getHeaderLine ('Cache-Control ' ));
275
300
276
- ini_set ('session.cache_limiter ' , $ ini_limiter );
277
- ini_set ('session.cache_expire ' , $ ini_expire );
301
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
278
302
}
279
303
280
304
public function testPersistSessionReturnsExpectedResponseWithoutAddedHeadersIfAlreadyHasAny ()
281
305
{
282
- $ ini = ini_get ('session.cache_limiter ' );
283
- ini_set ('session.cache_limiter ' , 'nocache ' );
306
+ $ ini = $ this ->applyCustomSessionOptions ([
307
+ 'cache_limiter ' => 'nocache ' ,
308
+ ]);
284
309
285
310
$ response = new Response ('php://memory ' , 200 , [
286
311
'Last-Modified ' => gmdate (PhpSessionPersistence::HTTP_DATE_FORMAT ),
@@ -296,14 +321,14 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedHeadersIfAl
296
321
$ this ->assertFalse ($ response ->hasHeader ('Expires ' ));
297
322
$ this ->assertFalse ($ response ->hasHeader ('Cache-Control ' ));
298
323
299
- ini_set ( ' session.cache_limiter ' , $ ini );
324
+ $ this -> restoreOriginalSessionIniSettings ( $ ini );
300
325
}
301
326
302
327
public function testPersistSessionInjectsExpectedLastModifiedHeaderIfScriptFilenameProvided ()
303
328
{
304
- // temporarily set session.cache_limiter to 'public'
305
- $ ini = ini_get ( ' session. cache_limiter ');
306
- ini_set ( ' session.cache_limiter ' , ' public ' );
329
+ $ ini = $ this -> applyCustomSessionOptions ([
330
+ ' cache_limiter ' => ' public ' ,
331
+ ] );
307
332
308
333
$ persistence = new PhpSessionPersistence ();
309
334
@@ -316,15 +341,14 @@ public function testPersistSessionInjectsExpectedLastModifiedHeaderIfScriptFilen
316
341
317
342
$ this ->assertSame ($ response ->getHeaderLine ('Last-Modified ' ), $ lastModified );
318
343
319
- // restore original ini setting
320
- ini_set ('session.cache_limiter ' , $ ini );
344
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
321
345
}
322
346
323
347
public function testPersistSessionInjectsExpectedLastModifiedHeaderWithClassFileMtimeIfNoScriptFilenameProvided ()
324
348
{
325
- // temporarily set session.cache_limiter to 'public'
326
- $ ini = ini_get ( ' session. cache_limiter ');
327
- ini_set ( ' session.cache_limiter ' , ' public ' );
349
+ $ ini = $ this -> applyCustomSessionOptions ([
350
+ ' cache_limiter ' => ' public ' ,
351
+ ] );
328
352
329
353
$ persistence = new PhpSessionPersistence ();
330
354
@@ -340,15 +364,14 @@ public function testPersistSessionInjectsExpectedLastModifiedHeaderWithClassFile
340
364
341
365
$ this ->assertSame ($ response ->getHeaderLine ('Last-Modified ' ), $ lastModified );
342
366
343
- // restore original ini setting
344
- ini_set ('session.cache_limiter ' , $ ini );
367
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
345
368
}
346
369
347
370
public function testPersistSessionDoesNotInjectLastModifiedHeaderIfUnableToDetermineFileMtime ()
348
371
{
349
- // temporarily set session.cache_limiter to 'public'
350
- $ ini = ini_get ( ' session. cache_limiter ');
351
- ini_set ( ' session.cache_limiter ' , ' public ' );
372
+ $ ini = $ this -> applyCustomSessionOptions ([
373
+ ' cache_limiter ' => ' public ' ,
374
+ ] );
352
375
353
376
$ persistence = new PhpSessionPersistence ();
354
377
@@ -359,15 +382,14 @@ public function testPersistSessionDoesNotInjectLastModifiedHeaderIfUnableToDeter
359
382
360
383
$ this ->assertFalse ($ response ->hasHeader ('Last-Modified ' ));
361
384
362
- // restore original ini setting
363
- ini_set ('session.cache_limiter ' , $ ini );
385
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
364
386
}
365
387
366
388
public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeadersIfEmptyCacheLimiter ()
367
389
{
368
- // temporarily set session.cache_limiter to ''
369
- $ ini = ini_get ( ' session. cache_limiter ');
370
- ini_set ( ' session.cache_limiter ' , '' );
390
+ $ ini = $ this -> applyCustomSessionOptions ([
391
+ ' cache_limiter ' => '' ,
392
+ ] );
371
393
372
394
$ persistence = new PhpSessionPersistence ();
373
395
@@ -379,15 +401,14 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeader
379
401
$ this ->assertFalse ($ response ->hasHeader ('Expires ' ));
380
402
$ this ->assertFalse ($ response ->hasHeader ('Cache-Control ' ));
381
403
382
- // restore original ini setting
383
- ini_set ('session.cache_limiter ' , $ ini );
404
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
384
405
}
385
406
386
407
public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeadersIfUnsupportedCacheLimiter ()
387
408
{
388
- // temporarily set session.cache_limiter to 'unsupported'
389
- $ ini = ini_get ( ' session. cache_limiter ');
390
- ini_set ( ' session.cache_limiter ' , ' unsupported ' );
409
+ $ ini = $ this -> applyCustomSessionOptions ([
410
+ ' cache_limiter ' => ' unsupported ' ,
411
+ ] );
391
412
392
413
$ persistence = new PhpSessionPersistence ();
393
414
@@ -399,7 +420,6 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeader
399
420
$ this ->assertFalse ($ response ->hasHeader ('Expires ' ));
400
421
$ this ->assertFalse ($ response ->hasHeader ('Cache-Control ' ));
401
422
402
- // restore original ini setting
403
- ini_set ('session.cache_limiter ' , $ ini );
423
+ $ this ->restoreOriginalSessionIniSettings ($ ini );
404
424
}
405
425
}
0 commit comments