Skip to content

Commit 549e1ca

Browse files
committed
Refactor assertHeaderEmitted and assertHeaderNotEmitted
1 parent 7b7db5f commit 549e1ca

File tree

1 file changed

+20
-52
lines changed

1 file changed

+20
-52
lines changed

system/Test/CIUnitTestCase.php

Lines changed: 20 additions & 52 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->assertNotNull(
403+
$this->getHeaderEmitted($header, $ignoreCase, __METHOD__),
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->assertNull(
417+
$this->getHeaderEmitted($header, $ignoreCase, __METHOD__),
418+
"Found header for {$header}"
419+
);
449420
}
450421

451422
/**
@@ -520,23 +491,20 @@ protected function createApplication()
520491

521492
/**
522493
* Return first matching emitted header.
523-
*
524-
* @param string $header Identifier of the header of interest
525-
*
526-
* @return string|null The value of the header found, null if not found
527494
*/
528-
protected function getHeaderEmitted(string $header, bool $ignoreCase = false): ?string
495+
protected function getHeaderEmitted(string $header, bool $ignoreCase = false, string $method = __METHOD__): ?string
529496
{
530497
if (! function_exists('xdebug_get_headers')) {
531-
$this->markTestSkipped('XDebug not found.');
498+
$this->markTestSkipped($method . '() requires xdebug.');
532499
}
533500

534-
foreach (xdebug_get_headers() as $emitted) {
535-
$found = $ignoreCase ?
536-
(stripos($emitted, $header) === 0) :
537-
(strpos($emitted, $header) === 0);
501+
foreach (xdebug_get_headers() as $emittedHeader) {
502+
$found = $ignoreCase
503+
? (stripos($emittedHeader, $header) === 0)
504+
: (strpos($emittedHeader, $header) === 0);
505+
538506
if ($found) {
539-
return $emitted;
507+
return $emittedHeader;
540508
}
541509
}
542510

0 commit comments

Comments
 (0)