Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 775598a

Browse files
committed
add and use helper methods to improve test cases readability
1 parent 2fec15d commit 775598a

File tree

1 file changed

+69
-49
lines changed

1 file changed

+69
-49
lines changed

test/PhpSessionPersistenceTest.php

+69-49
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ private function createSessionCookieRequest(string $sessionName = null, $session
6868
);
6969
}
7070

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+
7197
public function testInitializeSessionFromRequestInitializesSessionWithGeneratedIdentifierIfNoSessionCookiePresent()
7298
{
7399
$this->assertSame(PHP_SESSION_NONE, session_status());
@@ -178,8 +204,9 @@ public function testPersistSessionIfSessionHasContents()
178204

179205
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsNocache()
180206
{
181-
$ini = ini_get('session.cache_limiter');
182-
ini_set('session.cache_limiter', 'nocache');
207+
$ini = $this->applyCustomSessionOptions([
208+
'cache_limiter' => 'nocache',
209+
]);
183210

184211
$persistence = new PhpSessionPersistence();
185212

@@ -191,18 +218,18 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
191218
$this->assertSame($response->getHeaderLine('Cache-Control'), 'no-store, no-cache, must-revalidate');
192219
$this->assertSame($response->getHeaderLine('Pragma'), 'no-cache');
193220

194-
ini_set('session.cache_limiter', $ini);
221+
$this->restoreOriginalSessionIniSettings($ini);
195222
}
196223

197224
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsPublic()
198225
{
199226
$expire = 111;
200227
$maxAge = 60 * $expire;
201228

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+
]);
206233

207234
$persistence = new PhpSessionPersistence();
208235

@@ -221,19 +248,18 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
221248
$this->assertLessThanOrEqual($expires, $expiresMax);
222249
$this->assertSame($response->getHeaderLine('Cache-Control'), $control);
223250

224-
ini_set('session.cache_limiter', $ini_limiter);
225-
ini_set('session.cache_expire', $ini_expire);
251+
$this->restoreOriginalSessionIniSettings($ini);
226252
}
227253

228254
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsPrivate()
229255
{
230256
$expire = 222;
231257
$maxAge = 60 * $expire;
232258

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+
]);
237263

238264
$persistence = new PhpSessionPersistence();
239265

@@ -247,19 +273,18 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
247273
$this->assertSame($response->getHeaderLine('Expires'), $expires);
248274
$this->assertSame($response->getHeaderLine('Cache-Control'), $control);
249275

250-
ini_set('session.cache_limiter', $ini_limiter);
251-
ini_set('session.cache_expire', $ini_expire);
276+
$this->restoreOriginalSessionIniSettings($ini);
252277
}
253278

254279
public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCacheLimiterIsPrivateNoExpire()
255280
{
256281
$expire = 333;
257282
$maxAge = 60 * $expire;
258283

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+
]);
263288

264289
$persistence = new PhpSessionPersistence();
265290

@@ -273,14 +298,14 @@ public function testPersistSessionReturnsExpectedResponseWithCacheHeadersIfCache
273298
$this->assertSame('', $response->getHeaderLine('Expires'));
274299
$this->assertSame($control, $response->getHeaderLine('Cache-Control'));
275300

276-
ini_set('session.cache_limiter', $ini_limiter);
277-
ini_set('session.cache_expire', $ini_expire);
301+
$this->restoreOriginalSessionIniSettings($ini);
278302
}
279303

280304
public function testPersistSessionReturnsExpectedResponseWithoutAddedHeadersIfAlreadyHasAny()
281305
{
282-
$ini = ini_get('session.cache_limiter');
283-
ini_set('session.cache_limiter', 'nocache');
306+
$ini = $this->applyCustomSessionOptions([
307+
'cache_limiter' => 'nocache',
308+
]);
284309

285310
$response = new Response('php://memory', 200, [
286311
'Last-Modified' => gmdate(PhpSessionPersistence::HTTP_DATE_FORMAT),
@@ -296,14 +321,14 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedHeadersIfAl
296321
$this->assertFalse($response->hasHeader('Expires'));
297322
$this->assertFalse($response->hasHeader('Cache-Control'));
298323

299-
ini_set('session.cache_limiter', $ini);
324+
$this->restoreOriginalSessionIniSettings($ini);
300325
}
301326

302327
public function testPersistSessionInjectsExpectedLastModifiedHeaderIfScriptFilenameProvided()
303328
{
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+
]);
307332

308333
$persistence = new PhpSessionPersistence();
309334

@@ -316,15 +341,14 @@ public function testPersistSessionInjectsExpectedLastModifiedHeaderIfScriptFilen
316341

317342
$this->assertSame($response->getHeaderLine('Last-Modified'), $lastModified);
318343

319-
// restore original ini setting
320-
ini_set('session.cache_limiter', $ini);
344+
$this->restoreOriginalSessionIniSettings($ini);
321345
}
322346

323347
public function testPersistSessionInjectsExpectedLastModifiedHeaderWithClassFileMtimeIfNoScriptFilenameProvided()
324348
{
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+
]);
328352

329353
$persistence = new PhpSessionPersistence();
330354

@@ -340,15 +364,14 @@ public function testPersistSessionInjectsExpectedLastModifiedHeaderWithClassFile
340364

341365
$this->assertSame($response->getHeaderLine('Last-Modified'), $lastModified);
342366

343-
// restore original ini setting
344-
ini_set('session.cache_limiter', $ini);
367+
$this->restoreOriginalSessionIniSettings($ini);
345368
}
346369

347370
public function testPersistSessionDoesNotInjectLastModifiedHeaderIfUnableToDetermineFileMtime()
348371
{
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+
]);
352375

353376
$persistence = new PhpSessionPersistence();
354377

@@ -359,15 +382,14 @@ public function testPersistSessionDoesNotInjectLastModifiedHeaderIfUnableToDeter
359382

360383
$this->assertFalse($response->hasHeader('Last-Modified'));
361384

362-
// restore original ini setting
363-
ini_set('session.cache_limiter', $ini);
385+
$this->restoreOriginalSessionIniSettings($ini);
364386
}
365387

366388
public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeadersIfEmptyCacheLimiter()
367389
{
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+
]);
371393

372394
$persistence = new PhpSessionPersistence();
373395

@@ -379,15 +401,14 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeader
379401
$this->assertFalse($response->hasHeader('Expires'));
380402
$this->assertFalse($response->hasHeader('Cache-Control'));
381403

382-
// restore original ini setting
383-
ini_set('session.cache_limiter', $ini);
404+
$this->restoreOriginalSessionIniSettings($ini);
384405
}
385406

386407
public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeadersIfUnsupportedCacheLimiter()
387408
{
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+
]);
391412

392413
$persistence = new PhpSessionPersistence();
393414

@@ -399,7 +420,6 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeader
399420
$this->assertFalse($response->hasHeader('Expires'));
400421
$this->assertFalse($response->hasHeader('Cache-Control'));
401422

402-
// restore original ini setting
403-
ini_set('session.cache_limiter', $ini);
423+
$this->restoreOriginalSessionIniSettings($ini);
404424
}
405425
}

0 commit comments

Comments
 (0)