Browse Source

Merge pull request #9847 from Zuluru/master

Add functions to test response body against regular expressions
Mark Story 9 years ago
parent
commit
83641b9b63

+ 30 - 0
src/TestSuite/IntegrationTestCase.php

@@ -858,6 +858,36 @@ abstract class IntegrationTestCase extends TestCase
     }
 
     /**
+     * Asserts that the response body matches a given regular expression.
+     *
+     * @param string $pattern The pattern to compare against.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertResponseRegExp($pattern, $message = '')
+    {
+        if (!$this->_response) {
+            $this->fail('No response set, cannot assert content. ' . $message);
+        }
+        $this->assertRegExp($pattern, (string)$this->_response->body(), $message);
+    }
+
+    /**
+     * Asserts that the response body does not match a given regular expression.
+     *
+     * @param string $pattern The pattern to compare against.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertResponseNotRegExp($pattern, $message = '')
+    {
+        if (!$this->_response) {
+            $this->fail('No response set, cannot assert content. ' . $message);
+        }
+        $this->assertNotRegExp($pattern, (string)$this->_response->body(), $message);
+    }
+
+    /**
      * Assert response content is not empty.
      *
      * @param string $message The failure message that will be appended to the generated message.

+ 26 - 0
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -747,6 +747,32 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     }
 
     /**
+     * Test the content regexp assertion.
+     *
+     * @return void
+     */
+    public function testAssertResponseRegExp()
+    {
+        $this->_response = new Response();
+        $this->_response->body('Some content');
+
+        $this->assertResponseRegExp('/cont/');
+    }
+
+    /**
+     * Test the negated content regexp assertion.
+     *
+     * @return void
+     */
+    public function testAssertResponseNotRegExp()
+    {
+        $this->_response = new Response();
+        $this->_response->body('Some content');
+
+        $this->assertResponseNotRegExp('/cant/');
+    }
+
+    /**
      * Test that works in tandem with testEventManagerReset2 to
      * test the EventManager reset.
      *