From 2170d87488017f3eb719289fcf11af07ee223f46 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 4 Sep 2012 01:04:48 +0200 Subject: [PATCH 1/2] check() for CookieComponent and Configure (similar to `CakeSession::check()`) --- .../Controller/Component/CookieComponent.php | 14 +++++ lib/Cake/Core/Configure.php | 14 +++++ .../Component/CookieComponentTest.php | 54 +++++++++++++++++++ lib/Cake/Test/Case/Core/ConfigureTest.php | 54 +++++++++++++++++++ .../Case/Model/Datasource/CakeSessionTest.php | 2 +- 5 files changed, 137 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index 45551a8af..bdf344a5b 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -281,6 +281,20 @@ class CookieComponent extends Component { return $this->_values[$this->name][$key]; } +/** + * Returns true if given variable is set in cookie. + * + * @param string $var Variable name to check for + * @return boolean True if variable is there + */ + public function check($key = null) { + if (empty($key)) { + return false; + } + $result = $this->read($key); + return isset($result); + } + /** * Delete a cookie value * diff --git a/lib/Cake/Core/Configure.php b/lib/Cake/Core/Configure.php index 09615825c..f2101f795 100644 --- a/lib/Cake/Core/Configure.php +++ b/lib/Cake/Core/Configure.php @@ -170,6 +170,20 @@ class Configure { return Hash::get(self::$_values, $var); } +/** + * Returns true if given variable is set in Configure. + * + * @param string $var Variable name to check for + * @return boolean True if variable is there + */ + public static function check($var = null) { + if (empty($var)) { + return false; + } + $result = Hash::get(self::$_values, $var); + return isset($result); + } + /** * Used to delete a variable from Configure. * diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index 6ed65a673..7609b89ae 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -536,6 +536,60 @@ class CookieComponentTest extends CakeTestCase { $this->assertNull($this->Cookie->read('value')); } +/** + * testCheck method + * + * @return void + */ + public function testCheck() { + $this->Cookie->write('CookieComponentTestCase', 'value'); + $this->assertTrue($this->Cookie->check('CookieComponentTestCase')); + + $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase')); + } + +/** + * testCheckingSavedEmpty method + * + * @return void + */ + public function testCheckingSavedEmpty() { + $this->Cookie->write('CookieComponentTestCase', 0); + $this->assertTrue($this->Cookie->check('CookieComponentTestCase')); + + $this->Cookie->write('CookieComponentTestCase', '0'); + $this->assertTrue($this->Cookie->check('CookieComponentTestCase')); + + $this->Cookie->write('CookieComponentTestCase', false); + $this->assertTrue($this->Cookie->check('CookieComponentTestCase')); + + $this->Cookie->write('CookieComponentTestCase', null); + $this->assertFalse($this->Cookie->check('CookieComponentTestCase')); + } + +/** + * testCheckKeyWithSpaces method + * + * @return void + */ + public function testCheckKeyWithSpaces() { + $this->Cookie->write('CookieComponent Test', "test"); + $this->assertTrue($this->Cookie->check('CookieComponent Test')); + $this->Cookie->delete('CookieComponent Test'); + + $this->Cookie->write('CookieComponent Test.Test Case', "test"); + $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case')); + } + +/** + * testCheckEmpty + * + * @return void + */ + public function testCheckEmpty() { + $this->assertFalse($this->Cookie->check()); + } + /** * test that deleting a top level keys kills the child elements too. * diff --git a/lib/Cake/Test/Case/Core/ConfigureTest.php b/lib/Cake/Test/Case/Core/ConfigureTest.php index 7351f83f1..5dd1fe0b6 100644 --- a/lib/Cake/Test/Case/Core/ConfigureTest.php +++ b/lib/Cake/Test/Case/Core/ConfigureTest.php @@ -177,6 +177,60 @@ class ConfigureTest extends CakeTestCase { $this->assertTrue($result === null); } +/** + * testCheck method + * + * @return void + */ + public function testCheck() { + Configure::write('ConfigureTestCase', 'value'); + $this->assertTrue(Configure::check('ConfigureTestCase')); + + $this->assertFalse(Configure::check('NotExistingConfigureTestCase')); + } + +/** + * testCheckingSavedEmpty method + * + * @return void + */ + public function testCheckingSavedEmpty() { + $this->assertTrue(Configure::write('ConfigureTestCase', 0)); + $this->assertTrue(Configure::check('ConfigureTestCase')); + + $this->assertTrue(Configure::write('ConfigureTestCase', '0')); + $this->assertTrue(Configure::check('ConfigureTestCase')); + + $this->assertTrue(Configure::write('ConfigureTestCase', false)); + $this->assertTrue(Configure::check('ConfigureTestCase')); + + $this->assertTrue(Configure::write('ConfigureTestCase', null)); + $this->assertFalse(Configure::check('ConfigureTestCase')); + } + +/** + * testCheckKeyWithSpaces method + * + * @return void + */ + public function testCheckKeyWithSpaces() { + $this->assertTrue(Configure::write('Configure Test', "test")); + $this->assertTrue(Configure::check('Configure Test')); + Configure::delete('Configure Test'); + + $this->assertTrue(Configure::write('Configure Test.Test Case', "test")); + $this->assertTrue(Configure::check('Configure Test.Test Case')); + } + +/** + * testCheckEmpty + * + * @return void + */ + public function testCheckEmpty() { + $this->assertFalse(Configure::check()); + } + /** * testLoad method * diff --git a/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php b/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php index a2276972f..0c98febaa 100644 --- a/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php @@ -231,7 +231,7 @@ class CakeSessionTest extends CakeTestCase { TestCakeSession::write('SessionTestCase', 'value'); $this->assertTrue(TestCakeSession::check('SessionTestCase')); - $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false); + $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase')); } /** From 6d3e0a25b29eb16f5cbeac30481cc1dc2410008f Mon Sep 17 00:00:00 2001 From: euromark Date: Sat, 15 Sep 2012 02:33:05 +0200 Subject: [PATCH 2/2] save some memory usage (PHP < 5.4) in case of huge content and cut off the isset call --- lib/Cake/Controller/Component/CookieComponent.php | 3 +-- lib/Cake/Core/Configure.php | 3 +-- lib/Cake/Model/Datasource/CakeSession.php | 6 ++---- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index bdf344a5b..e105bc9ec 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -291,8 +291,7 @@ class CookieComponent extends Component { if (empty($key)) { return false; } - $result = $this->read($key); - return isset($result); + return $this->read($key) !== null; } /** diff --git a/lib/Cake/Core/Configure.php b/lib/Cake/Core/Configure.php index f2101f795..0d1b27478 100644 --- a/lib/Cake/Core/Configure.php +++ b/lib/Cake/Core/Configure.php @@ -180,8 +180,7 @@ class Configure { if (empty($var)) { return false; } - $result = Hash::get(self::$_values, $var); - return isset($result); + return Hash::get(self::$_values, $var) !== null; } /** diff --git a/lib/Cake/Model/Datasource/CakeSession.php b/lib/Cake/Model/Datasource/CakeSession.php index 6baab1d5b..ff13c0ca6 100644 --- a/lib/Cake/Model/Datasource/CakeSession.php +++ b/lib/Cake/Model/Datasource/CakeSession.php @@ -218,8 +218,7 @@ class CakeSession { if (empty($name)) { return false; } - $result = Hash::get($_SESSION, $name); - return isset($result); + return Hash::get($_SESSION, $name) !== null; } /** @@ -283,9 +282,8 @@ class CakeSession { protected static function _error($errorNumber) { if (!is_array(self::$error) || !array_key_exists($errorNumber, self::$error)) { return false; - } else { - return self::$error[$errorNumber]; } + return self::$error[$errorNumber]; } /**