From d68caa6077165425db2bbf774cb3e6215f12cf77 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 3 Jun 2014 12:48:04 -0400 Subject: [PATCH] Use the Time utility instead of \DateTime Doing this allows the mocked time objects to take effect. --- src/Controller/Component/CookieComponent.php | 6 +++--- .../Controller/Component/CookieComponentTest.php | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Controller/Component/CookieComponent.php b/src/Controller/Component/CookieComponent.php index b17cf5be86b..aed895c2dc1 100644 --- a/src/Controller/Component/CookieComponent.php +++ b/src/Controller/Component/CookieComponent.php @@ -16,7 +16,6 @@ use Cake\Controller\Component; use Cake\Controller\ComponentRegistry; -use Cake\Controller\Controller; use Cake\Core\Configure; use Cake\Error; use Cake\Event\Event; @@ -24,6 +23,7 @@ use Cake\Network\Response; use Cake\Utility\Hash; use Cake\Utility\Security; +use Cake\Utility\Time; /** * Cookie Component. @@ -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, @@ -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, diff --git a/tests/TestCase/Controller/Component/CookieComponentTest.php b/tests/TestCase/Controller/Component/CookieComponentTest.php index ac8135ae848..5555ae7195f 100644 --- a/tests/TestCase/Controller/Component/CookieComponentTest.php +++ b/tests/TestCase/Controller/Component/CookieComponentTest.php @@ -22,6 +22,7 @@ use Cake\Network\Response; use Cake\TestSuite\TestCase; use Cake\Utility\Security; +use Cake\Utility\Time; /** * CookieComponentTest class @@ -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( @@ -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, @@ -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, @@ -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); }