From 7383298410836ee05566863aac785b31d1d6de3d Mon Sep 17 00:00:00 2001 From: euromark Date: Mon, 1 Oct 2012 13:42:14 +0200 Subject: [PATCH] making messages() and errors() reset themselves by default --- lib/Cake/Test/Case/Utility/FolderTest.php | 46 +++++++++++++++++++++++ lib/Cake/Utility/Folder.php | 20 +++++++--- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/FolderTest.php b/lib/Cake/Test/Case/Utility/FolderTest.php index 65739bf84b4..d3eddbf2ad2 100644 --- a/lib/Cake/Test/Case/Utility/FolderTest.php +++ b/lib/Cake/Test/Case/Utility/FolderTest.php @@ -766,6 +766,52 @@ public function testDirSize() { $Folder->delete($Folder->pwd() . 'config_non_existent'); } +/** + * test that errors and messages can be resetted + * + * @return void + */ + public function testReset() { + $path = TMP . 'folder_delete_test'; + mkdir($path); + $folder = $path . DS . 'sub'; + mkdir($folder); + $file = $folder . DS . 'file'; + touch($file); + $handle = fopen($file, 'a'); + + $Folder = new Folder($folder); + $return = $Folder->delete(); + $this->assertFalse($return); + + $messages = $Folder->messages(); + $errors = $Folder->errors(); + $expected = array( + $folder . DS . 'file NOT removed', + $folder . ' NOT removed', + ); + sort($expected); + sort($errors); + $this->assertEmpty($messages); + $this->assertEquals($expected, $errors); + + fclose($handle); + + $return = $Folder->delete(); + $this->assertTrue($return); + + $messages = $Folder->messages(); + $errors = $Folder->errors(); + $expected = array( + $folder . DS . 'file removed', + $folder . ' removed', + ); + sort($expected); + sort($messages); + $this->assertEmpty($errors); + $this->assertEquals($expected, $messages); + } + /** * testDelete method * diff --git a/lib/Cake/Utility/Folder.php b/lib/Cake/Utility/Folder.php index 3bacbcd92b1..5f19fdde934 100644 --- a/lib/Cake/Utility/Folder.php +++ b/lib/Cake/Utility/Folder.php @@ -23,7 +23,7 @@ class Folder { /** * Default scheme for Folder::copy - * Recursively merges subfolders with the same name + * Recursively merges subfolders with the same name * * @constant MERGE */ @@ -740,21 +740,31 @@ public function move($options) { /** * get messages from latest method * + * @param boolean $reset Reset message stack after reading * @return array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::messages */ - public function messages() { - return $this->_messages; + public function messages($reset = true) { + $messages = $this->_messages; + if ($reset) { + $this->_messages = array(); + } + return $messages; } /** * get error from latest method * + * @param boolean $reset Reset error stack after reading * @return array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::errors */ - public function errors() { - return $this->_errors; + public function errors($reset = true) { + $errors = $this->_errors; + if ($reset) { + $this->_errors = array(); + } + return $errors; } /**