Skip to content

Commit

Permalink
Use the Time utility instead of \DateTime
Browse files Browse the repository at this point in the history
Doing this allows the mocked time objects to take effect.
  • Loading branch information
markstory committed Jun 4, 2014
1 parent 79f20ca commit d68caa6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -16,14 +16,14 @@

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Error;
use Cake\Event\Event;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Utility\Hash;
use Cake\Utility\Security;
use Cake\Utility\Time;

/**
* Cookie Component.
Expand Down Expand Up @@ -301,7 +301,7 @@ public function delete($key) {
*/
protected function _write($name, $value) {
$config = $this->configKey($name);
$expires = new \DateTime($config['expires']);
$expires = new Time($config['expires']);

$this->_response->cookie(array(
'name' => $name,
Expand All @@ -325,7 +325,7 @@ protected function _write($name, $value) {
*/
protected function _delete($name) {
$config = $this->configKey($name);
$expires = new \DateTime('now');
$expires = new Time('now');

$this->_response->cookie(array(
'name' => $name,
Expand Down
10 changes: 6 additions & 4 deletions tests/TestCase/Controller/Component/CookieComponentTest.php
Expand Up @@ -22,6 +22,7 @@
use Cake\Network\Response;
use Cake\TestSuite\TestCase;
use Cake\Utility\Security;
use Cake\Utility\Time;

/**
* CookieComponentTest class
Expand Down Expand Up @@ -278,7 +279,7 @@ public function testWriteWithFalseyValue() {
public function testWriteFarFuture() {
$this->Cookie->configKey('Testing', 'expires', '+90 years');
$this->Cookie->write('Testing', 'value');
$future = new \DateTime('now');
$future = new Time('now');
$future->modify('+90 years');

$expected = array(
Expand Down Expand Up @@ -310,7 +311,7 @@ public function testWriteHttpOnly() {
$expected = array(
'name' => 'Testing',
'value' => 'value',
'expire' => time() + 10,
'expire' => (new Time('+10 seconds'))->format('U'),
'path' => '/',
'domain' => '',
'secure' => false,
Expand Down Expand Up @@ -361,7 +362,7 @@ public function testDeleteHttpOnly() {
$expected = array(
'name' => 'Testing',
'value' => '',
'expire' => time() - 42000,
'expire' => (new Time('now'))->format('U') - 42000,
'path' => '/',
'domain' => '',
'secure' => false,
Expand All @@ -387,7 +388,8 @@ public function testWriteArrayValues() {
);
$result = $this->Controller->response->cookie('Testing');

$this->assertWithinMargin($result['expire'], time() + 10, 1);
$time = new Time('now');
$this->assertWithinMargin($result['expire'], $time->format('U') + 10, 1);
unset($result['expire']);
$this->assertEquals($expected, $result);
}
Expand Down

0 comments on commit d68caa6

Please sign in to comment.