diff --git a/library/Zend/Test/PHPUnit/Controller/AbstractHttpControllerTestCase.php b/library/Zend/Test/PHPUnit/Controller/AbstractHttpControllerTestCase.php index 0e322f210b1..12cca473036 100644 --- a/library/Zend/Test/PHPUnit/Controller/AbstractHttpControllerTestCase.php +++ b/library/Zend/Test/PHPUnit/Controller/AbstractHttpControllerTestCase.php @@ -88,15 +88,30 @@ public function assertResponseHeaderContains($header, $match) $header )); } - if ($match != $responseHeader->getFieldValue()) { - throw new PHPUnit_Framework_ExpectationFailedException(sprintf( + + if (!$responseHeader instanceof \ArrayIterator) { + $responseHeader = array($responseHeader); + } + + $headerMatched = false; + + foreach ($responseHeader as $currentHeader) { + if ($match == $currentHeader->getFieldValue()) { + $headerMatched = true; + break; + } + } + + if (!$headerMatched) { + throw new \PHPUnit_Framework_ExpectationFailedException(sprintf( 'Failed asserting response header "%s" exists and contains "%s", actual content is "%s"', $header, $match, - $responseHeader->getFieldValue() + $currentHeader->getFieldValue() )); } - $this->assertEquals($match, $responseHeader->getFieldValue()); + + $this->assertEquals($match, $currentHeader->getFieldValue()); } /** @@ -114,14 +129,22 @@ public function assertNotResponseHeaderContains($header, $match) $header )); } - if ($match == $responseHeader->getFieldValue()) { - throw new PHPUnit_Framework_ExpectationFailedException(sprintf( - 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"', - $header, - $match - )); + + if (!$responseHeader instanceof \ArrayIterator) { + $responseHeader = array($responseHeader); + } + + foreach ($responseHeader as $currentHeader) { + if ($match == $currentHeader->getFieldValue()) { + throw new PHPUnit_Framework_ExpectationFailedException(sprintf( + 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"', + $header, + $match + )); + } } - $this->assertNotEquals($match, $responseHeader->getFieldValue()); + + $this->assertNotEquals($match, $currentHeader->getFieldValue()); } /** @@ -139,15 +162,31 @@ public function assertResponseHeaderRegex($header, $pattern) $header )); } - if (!preg_match($pattern, $responseHeader->getFieldValue())) { + + if (!$responseHeader instanceof \ArrayIterator) { + $responseHeader = array($responseHeader); + } + + $headerMatched = false; + + foreach ($responseHeader as $currentHeader) { + $headerMatched = (bool) preg_match($pattern, $currentHeader->getFieldValue()); + + if ($headerMatched) { + break; + } + } + + if (!$headerMatched) { throw new PHPUnit_Framework_ExpectationFailedException(sprintf( 'Failed asserting response header "%s" exists and matches regex "%s", actual content is "%s"', $header, $pattern, - $responseHeader->getFieldValue() + $currentHeader->getFieldValue() )); } - $this->assertTrue((bool) preg_match($pattern, $responseHeader->getFieldValue())); + + $this->assertTrue($headerMatched); } /** @@ -165,14 +204,26 @@ public function assertNotResponseHeaderRegex($header, $pattern) $header )); } - if (preg_match($pattern, $responseHeader->getFieldValue())) { - throw new PHPUnit_Framework_ExpectationFailedException(sprintf( - 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"', - $header, - $pattern - )); + + if (!$responseHeader instanceof \ArrayIterator) { + $responseHeader = array($responseHeader); } - $this->assertFalse((bool) preg_match($pattern, $responseHeader->getFieldValue())); + + $headerMatched = false; + + foreach ($responseHeader as $currentHeader) { + $headerMatched = (bool) preg_match($pattern, $currentHeader->getFieldValue()); + + if ($headerMatched) { + throw new PHPUnit_Framework_ExpectationFailedException(sprintf( + 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"', + $header, + $pattern + )); + } + } + + $this->assertFalse($headerMatched); } /** diff --git a/tests/ZendTest/Test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php b/tests/ZendTest/Test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php index f09506c8b11..67771ec6071 100644 --- a/tests/ZendTest/Test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php +++ b/tests/ZendTest/Test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php @@ -84,6 +84,12 @@ public function testAssertResponseHeaderContains() $this->assertResponseHeaderContains('Content-Type', 'text/json'); } + public function testAssertResponseHeaderContainsMultipleHeaderInterface() + { + $this->dispatch('/tests'); + $this->assertResponseHeaderContains('WWW-Authenticate', 'Basic realm="ZF2"'); + } + public function testAssertNotResponseHeaderContains() { $this->dispatch('/tests'); @@ -93,6 +99,12 @@ public function testAssertNotResponseHeaderContains() $this->assertNotResponseHeaderContains('Content-Type', 'text/html'); } + public function testAssertNotResponseHeaderContainsMultipleHeaderInterface() + { + $this->dispatch('/tests'); + $this->assertNotResponseHeaderContains('WWW-Authenticate', 'Basic realm="ZF3"'); + } + public function testAssertResponseHeaderRegex() { $this->dispatch('/tests'); @@ -105,6 +117,12 @@ public function testAssertResponseHeaderRegex() $this->assertResponseHeaderRegex('Content-Type', '#json#'); } + public function testAssertResponseHeaderRegexMultipleHeaderInterface() + { + $this->dispatch('/tests'); + $this->assertResponseHeaderRegex('WWW-Authenticate', '#"ZF2"$#'); + } + public function testAssertNotResponseHeaderRegex() { $this->dispatch('/tests'); @@ -114,6 +132,12 @@ public function testAssertNotResponseHeaderRegex() $this->assertNotResponseHeaderRegex('Content-Type', '#html$#'); } + public function testAssertNotResponseHeaderRegexMultipleHeaderInterface() + { + $this->dispatch('/tests'); + $this->assertNotResponseHeaderRegex('WWW-Authenticate', '#"ZF3"$#'); + } + public function testAssertRedirect() { $this->dispatch('/redirect'); diff --git a/tests/ZendTest/Test/_files/Baz/src/Baz/Controller/IndexController.php b/tests/ZendTest/Test/_files/Baz/src/Baz/Controller/IndexController.php index 696ebfbfcb8..72fc29ba9d6 100644 --- a/tests/ZendTest/Test/_files/Baz/src/Baz/Controller/IndexController.php +++ b/tests/ZendTest/Test/_files/Baz/src/Baz/Controller/IndexController.php @@ -15,7 +15,10 @@ class IndexController extends AbstractActionController { public function unittestsAction() { - $this->getResponse()->getHeaders()->addHeaderLine('Content-Type: text/html'); + $this->getResponse() + ->getHeaders() + ->addHeaderLine('Content-Type: text/html') + ->addHeaderLine('WWW-Authenticate: Basic realm="ZF2"'); $num_get = $this->getRequest()->getQuery()->get('num_get', 0); $num_post = $this->getRequest()->getPost()->get('num_post', 0);