diff --git a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php index a05c4438d7f..c26d8225808 100644 --- a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php +++ b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php @@ -256,4 +256,86 @@ public function testAssertTextEquals() { $two = "\nOne\nTwo"; $this->assertTextEquals($one, $two); } + +/** + * test assertTextStartsWith() + * + * @return void + */ + public function testAssertTextStartsWith() { + $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!"; + $stringClean = "some\nstring\nwith\ndifferent\nline endings!"; + + $this->assertStringStartsWith("some\nstring", $stringDirty); + $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty); + $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty); + + $this->assertTextStartsWith("some\nstring\nwith", $stringDirty); + $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty); + } + +/** + * test assertTextStartsNotWith() + * + * @return void + */ + public function testAssertTextStartsNotWith() { + $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!"; + $stringClean = "some\nstring\nwith\ndifferent\nline endings!"; + + $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty); + } + +/** + * test assertTextEndsWith() + * + * @return void + */ + public function testAssertTextEndsWith() { + $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!"; + $stringClean = "some\nstring\nwith\ndifferent\nline endings!"; + + $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty); + $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty); + } + +/** + * test assertTextEndsNotWith() + * + * @return void + */ + public function testAssertTextEndsNotWith() { + $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!"; + $stringClean = "some\nstring\nwith\ndifferent\nline endings!"; + + $this->assertStringEndsNotWith("different\nline endings", $stringDirty); + $this->assertTextEndsNotWith("different\rline endings", $stringDirty); + } + +/** + * test assertTextContains() + * + * @return void + */ + public function testAssertTextContains() { + $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!"; + $stringClean = "some\nstring\nwith\ndifferent\nline endings!"; + + $this->assertContains("different", $stringDirty); + $this->assertNotContains("different\rline", $stringDirty); + + $this->assertTextContains("different\rline", $stringDirty); + } + +/** + * test assertTextNotContains() + * + * @return void + */ + public function testAssertTextNotContains() { + $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!"; + $stringClean = "some\nstring\nwith\ndifferent\nline endings!"; + + $this->assertTextNotContains("different\rlines", $stringDirty); + } } diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index 8bafade81fa..f1f21eb6c4e 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -199,6 +199,7 @@ public function loadFixtures() { * @param string $expected The expected value. * @param string $result The actual value. * @param message The message to use for failure. + * @return boolean */ public function assertTextNotEquals($expected, $result, $message = '') { $expected = str_replace(array("\r\n", "\r"), "\n", $expected); @@ -213,6 +214,7 @@ public function assertTextNotEquals($expected, $result, $message = '') { * @param string $expected The expected value. * @param string $result The actual value. * @param message The message to use for failure. + * @return boolean */ public function assertTextEquals($expected, $result, $message = '') { $expected = str_replace(array("\r\n", "\r"), "\n", $expected); @@ -220,6 +222,98 @@ public function assertTextEquals($expected, $result, $message = '') { return $this->assertEquals($expected, $result, $message); } +/** + * Asserts that a string starts with a given prefix, ignoring differences in newlines. + * Helpful for doing cross platform tests of blocks of text. + * + * @param string $prefix + * @param string $string + * @param string $message + * @return boolean + */ + public function assertTextStartsWith($prefix, $string, $message = '') { + $prefix = str_replace(array("\r\n", "\r"), "\n", $prefix); + $string = str_replace(array("\r\n", "\r"), "\n", $string); + return $this->assertStringStartsWith($prefix, $string, $message); + } + +/** + * Asserts that a string starts not with a given prefix, ignoring differences in newlines. + * Helpful for doing cross platform tests of blocks of text. + * + * @param string $prefix + * @param string $string + * @param string $message + * @return boolean + */ + public function assertTextStartsNotWith($prefix, $string, $message = '') { + $prefix = str_replace(array("\r\n", "\r"), "\n", $prefix); + $string = str_replace(array("\r\n", "\r"), "\n", $string); + return $this->assertStringStartsNotWith($prefix, $string, $message); + } + +/** + * Asserts that a string ends with a given prefix, ignoring differences in newlines. + * Helpful for doing cross platform tests of blocks of text. + * + * @param string $suffix + * @param string $string + * @param string $message + * @return boolean + */ + public function assertTextEndsWith($suffix, $string, $message = '') { + $suffix = str_replace(array("\r\n", "\r"), "\n", $suffix); + $string = str_replace(array("\r\n", "\r"), "\n", $string); + return $this->assertStringEndsWith($suffix, $string, $message); + } + +/** + * Asserts that a string ends not with a given prefix, ignoring differences in newlines. + * Helpful for doing cross platform tests of blocks of text. + * + * @param string $suffix + * @param string $string + * @param string $message + * @return boolean + */ + public function assertTextEndsNotWith($suffix, $string, $message = '') { + $suffix = str_replace(array("\r\n", "\r"), "\n", $suffix); + $string = str_replace(array("\r\n", "\r"), "\n", $string); + return $this->assertStringEndsNotWith($suffix, $string, $message); + } + +/** + * Assert that a string contains another string, ignoring differences in newlines. + * Helpful for doing cross platform tests of blocks of text. + * + * @param string $needle + * @param string $haystack + * @param string $message + * @param boolean $ignoreCase + * @return boolean + */ + public function assertTextContains($needle, $haystack, $message = '', $ignoreCase = false) { + $needle = str_replace(array("\r\n", "\r"), "\n", $needle); + $haystack = str_replace(array("\r\n", "\r"), "\n", $haystack); + return $this->assertContains($needle, $haystack, $message, $ignoreCase); + } + +/** + * Assert that a text doesn't contain another text, ignoring differences in newlines. + * Helpful for doing cross platform tests of blocks of text. + * + * @param string $needle + * @param string $haystack + * @param string $message + * @param boolean $ignoreCase + * @return boolean + */ + public function assertTextNotContains($needle, $haystack, $message = '', $ignoreCase = false) { + $needle = str_replace(array("\r\n", "\r"), "\n", $needle); + $haystack = str_replace(array("\r\n", "\r"), "\n", $haystack); + return $this->assertNotContains($needle, $haystack, $message, $ignoreCase); + } + /** * Takes an array $expected and generates a regex from it to match the provided $string. * Samples for $expected: