Skip to content

Commit 916fffa

Browse files
committed
Refactor assertHeaderEmitted and assertHeaderNotEmitted
1 parent 8274996 commit 916fffa

File tree

1 file changed

+19
-47
lines changed

1 file changed

+19
-47
lines changed

system/Test/CIUnitTestCase.php

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -392,60 +392,31 @@ public function assertEventTriggered(string $eventName): bool
392392
}
393393

394394
/**
395-
* Hooks into xdebug's headers capture, looking for a specific header
396-
* emitted
395+
* Hooks into xdebug's headers capture, looking for presence of
396+
* a specific header emitted.
397397
*
398398
* @param string $header The leading portion of the header we are looking for
399-
*
400-
* @throws Exception
401399
*/
402400
public function assertHeaderEmitted(string $header, bool $ignoreCase = false): void
403401
{
404-
$found = false;
405-
406-
if (! function_exists('xdebug_get_headers')) {
407-
$this->markTestSkipped('XDebug not found.');
408-
}
409-
410-
foreach (xdebug_get_headers() as $emitted) {
411-
$found = $ignoreCase ?
412-
(stripos($emitted, $header) === 0) :
413-
(strpos($emitted, $header) === 0);
414-
if ($found) {
415-
break;
416-
}
417-
}
418-
419-
$this->assertTrue($found, "Didn't find header for {$header}");
402+
$this->assertTrue(
403+
$this->getHeaderEmitted($header, $ignoreCase) !== null,
404+
"Didn't find header for {$header}"
405+
);
420406
}
421407

422408
/**
423-
* Hooks into xdebug's headers capture, looking for a specific header
424-
* emitted
409+
* Hooks into xdebug's headers capture, looking for absence of
410+
* a specific header emitted.
425411
*
426412
* @param string $header The leading portion of the header we don't want to find
427-
*
428-
* @throws Exception
429413
*/
430414
public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false): void
431415
{
432-
$found = false;
433-
434-
if (! function_exists('xdebug_get_headers')) {
435-
$this->markTestSkipped('XDebug not found.');
436-
}
437-
438-
foreach (xdebug_get_headers() as $emitted) {
439-
$found = $ignoreCase ?
440-
(stripos($emitted, $header) === 0) :
441-
(strpos($emitted, $header) === 0);
442-
if ($found) {
443-
break;
444-
}
445-
}
446-
447-
$success = ! $found;
448-
$this->assertTrue($success, "Found header for {$header}");
416+
$this->assertTrue(
417+
$this->getHeaderEmitted($header, $ignoreCase) === null,
418+
"Found header for {$header}"
419+
);
449420
}
450421

451422
/**
@@ -528,15 +499,16 @@ protected function createApplication()
528499
protected function getHeaderEmitted(string $header, bool $ignoreCase = false): ?string
529500
{
530501
if (! function_exists('xdebug_get_headers')) {
531-
$this->markTestSkipped('XDebug not found.');
502+
$this->markTestSkipped('xdebug not found.');
532503
}
533504

534-
foreach (xdebug_get_headers() as $emitted) {
535-
$found = $ignoreCase ?
536-
(stripos($emitted, $header) === 0) :
537-
(strpos($emitted, $header) === 0);
505+
foreach (xdebug_get_headers() as $emittedHeader) {
506+
$found = $ignoreCase
507+
? (stripos($emittedHeader, $header) === 0)
508+
: (strpos($emittedHeader, $header) === 0);
509+
538510
if ($found) {
539-
return $emitted;
511+
return $emittedHeader;
540512
}
541513
}
542514

0 commit comments

Comments
 (0)