Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor assertHeaderEmitted and assertHeaderNotEmitted #6806

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 20 additions & 52 deletions system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,60 +392,31 @@ public function assertEventTriggered(string $eventName): bool
}

/**
* Hooks into xdebug's headers capture, looking for a specific header
* emitted
* Hooks into xdebug's headers capture, looking for presence of
* a specific header emitted.
*
* @param string $header The leading portion of the header we are looking for
*
* @throws Exception
*/
public function assertHeaderEmitted(string $header, bool $ignoreCase = false): void
{
$found = false;

if (! function_exists('xdebug_get_headers')) {
$this->markTestSkipped('XDebug not found.');
}

foreach (xdebug_get_headers() as $emitted) {
$found = $ignoreCase ?
(stripos($emitted, $header) === 0) :
(strpos($emitted, $header) === 0);
if ($found) {
break;
}
}

$this->assertTrue($found, "Didn't find header for {$header}");
$this->assertNotNull(
$this->getHeaderEmitted($header, $ignoreCase, __METHOD__),
"Didn't find header for {$header}"
);
}

/**
* Hooks into xdebug's headers capture, looking for a specific header
* emitted
* Hooks into xdebug's headers capture, looking for absence of
* a specific header emitted.
*
* @param string $header The leading portion of the header we don't want to find
*
* @throws Exception
*/
public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false): void
{
$found = false;

if (! function_exists('xdebug_get_headers')) {
$this->markTestSkipped('XDebug not found.');
}

foreach (xdebug_get_headers() as $emitted) {
$found = $ignoreCase ?
(stripos($emitted, $header) === 0) :
(strpos($emitted, $header) === 0);
if ($found) {
break;
}
}

$success = ! $found;
$this->assertTrue($success, "Found header for {$header}");
$this->assertNull(
$this->getHeaderEmitted($header, $ignoreCase, __METHOD__),
"Found header for {$header}"
);
}

/**
Expand Down Expand Up @@ -520,23 +491,20 @@ protected function createApplication()

/**
* Return first matching emitted header.
*
* @param string $header Identifier of the header of interest
*
* @return string|null The value of the header found, null if not found
*/
protected function getHeaderEmitted(string $header, bool $ignoreCase = false): ?string
protected function getHeaderEmitted(string $header, bool $ignoreCase = false, string $method = __METHOD__): ?string
{
if (! function_exists('xdebug_get_headers')) {
$this->markTestSkipped('XDebug not found.');
$this->markTestSkipped($method . '() requires xdebug.');
}

foreach (xdebug_get_headers() as $emitted) {
$found = $ignoreCase ?
(stripos($emitted, $header) === 0) :
(strpos($emitted, $header) === 0);
foreach (xdebug_get_headers() as $emittedHeader) {
$found = $ignoreCase
? (stripos($emittedHeader, $header) === 0)
: (strpos($emittedHeader, $header) === 0);

if ($found) {
return $emitted;
return $emittedHeader;
}
}

Expand Down