Skip to content

Commit

Permalink
check() for CookieComponent and Configure (similar to `CakeSession::c…
Browse files Browse the repository at this point in the history
…heck()`)
  • Loading branch information
euromark committed Sep 3, 2012
1 parent eb33929 commit 2170d87
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -281,6 +281,20 @@ public function read($key = null) {
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
*
Expand Down
14 changes: 14 additions & 0 deletions lib/Cake/Core/Configure.php
Expand Up @@ -170,6 +170,20 @@ public static function read($var = null) {
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.
*
Expand Down
54 changes: 54 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
Expand Up @@ -536,6 +536,60 @@ public function testNoErrorOnNonArrayData() {
$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.
*
Expand Down
54 changes: 54 additions & 0 deletions lib/Cake/Test/Case/Core/ConfigureTest.php
Expand Up @@ -177,6 +177,60 @@ public function testDelete() {
$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
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php
Expand Up @@ -231,7 +231,7 @@ public function testCheck() {
TestCakeSession::write('SessionTestCase', 'value');
$this->assertTrue(TestCakeSession::check('SessionTestCase'));

$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false);
$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
}

/**
Expand Down

0 comments on commit 2170d87

Please sign in to comment.