Skip to content

Commit

Permalink
Add Request::cookie()
Browse files Browse the repository at this point in the history
Provides a simple notice error free way to access cookie data.
Also allows test cases to read cookies without modifying the super
global.
  • Loading branch information
markstory committed Jul 4, 2012
1 parent 836fa83 commit f7522b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 13 additions & 4 deletions lib/Cake/Network/Request.php
@@ -1,9 +1,5 @@
<?php
/**
* Cake Request
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand Down Expand Up @@ -850,6 +846,19 @@ public function input($callback = null) {
return $input;
}

/**
* Read cookie data from the request's cookie data.
*
* @param string $key The key you want to read.
* @return null|string Either the cookie value, or null if the value doesn't exist.
*/
public function cookie($key) {
if (isset($this->cookies[$key])) {
return $this->cookies[$key];
}
return null;
}

/**
* Read data from php://input, mocked in tests.
*
Expand Down
24 changes: 19 additions & 5 deletions lib/Cake/Test/TestCase/Network/RequestTest.php
@@ -1,9 +1,5 @@
<?php
/**
* Request Test case file.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -16,8 +12,8 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace Cake\Test\TestSuite\Network;

use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Routing\Dispatcher;
Expand Down Expand Up @@ -1843,6 +1839,24 @@ public function testIsRequested() {
$this->assertFalse($request->isRequested());
}

/**
* Test the cookie() method.
*
* @return void
*/
public function testReadCookie() {
$request = new Request(array(
'cookies' => array(
'testing' => 'A value in the cookie'
)
));
$result = $request->cookie('testing');
$this->assertEquals('A value in the cookie', $result);

$result = $request->cookie('not there');
$this->assertNull($result);
}

/**
* loadEnvironment method
*
Expand Down

0 comments on commit f7522b7

Please sign in to comment.