From 549e1ca81c9623031e2fe037b6e3eb10e5368796 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 2 Nov 2022 01:07:02 +0800 Subject: [PATCH] Refactor assertHeaderEmitted and assertHeaderNotEmitted --- system/Test/CIUnitTestCase.php | 72 ++++++++++------------------------ 1 file changed, 20 insertions(+), 52 deletions(-) diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index e7a72db457f9..0fecac3ecca1 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -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}" + ); } /** @@ -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; } }