From f8e5926deba54b944b6900567e12e04343b15dc6 Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Fri, 8 Jun 2018 21:09:24 -0700 Subject: [PATCH] Fleshed out testing for array, cookie and date (such as it is) helpers --- tests/system/Helpers/ArrayHelperTest.php | 16 +++++++ tests/system/Helpers/CookieHelperTest.php | 48 ++++++++++--------- tests/system/Helpers/DateHelperTest.php | 44 +++++++++++++++++ user_guide_src/source/helpers/date_helper.rst | 47 ++++++++++++++++++ 4 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 tests/system/Helpers/DateHelperTest.php create mode 100644 user_guide_src/source/helpers/date_helper.rst diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index 1db4d19f35..09b6ecd218 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -90,6 +90,22 @@ class ArrayHelperTest extends \CIUnitTestCase $this->assertEquals(23, dot_array_search('foo.*.baz', $data)); } + public function testArrayDotNestedNotFound() + { + $data = [ + 'foo' => [ + 'buzz' => [ + 'fizz' => 11 + ], + 'bar' => [ + 'baz' => 23 + ] + ] + ]; + + $this->assertNull(dot_array_search('foo.*.notthere', $data)); + } + public function testArrayDotIgnoresLastWildcard() { $data = [ diff --git a/tests/system/Helpers/CookieHelperTest.php b/tests/system/Helpers/CookieHelperTest.php index a361721cd7..e1ae2dfc02 100755 --- a/tests/system/Helpers/CookieHelperTest.php +++ b/tests/system/Helpers/CookieHelperTest.php @@ -4,7 +4,7 @@ use Config\App; use CodeIgniter\Config\Services; use Tests\Support\HTTP\MockResponse; -final class cookieHelperTest extends \CIUnitTestCase +final class CookieHelperTest extends \CIUnitTestCase { private $name; @@ -22,6 +22,8 @@ final class cookieHelperTest extends \CIUnitTestCase Services::injectMock('response', new MockResponse(new App())); $this->response = service('response'); + $this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent()); + Services::injectMock('request', $this->request); helper('cookie'); } @@ -30,14 +32,11 @@ final class cookieHelperTest extends \CIUnitTestCase public function testSetCookie() { - $this->response->setCookie($this->name, $this->value, $this->expire); - - //TODO: Find a way for set_cookie() to use the MockResponse object. - //set_cookie($this->name, $this->value, $this->expire); + set_cookie($this->name, $this->value, $this->expire); $this->assertTrue($this->response->hasCookie($this->name)); - $this->response->deleteCookie($this->name); + delete_cookie($this->name); } //-------------------------------------------------------------------- @@ -49,17 +48,16 @@ final class cookieHelperTest extends \CIUnitTestCase 'value' => $this->value, 'expire' => $this->expire ]; - //set_cookie($cookieAttr); - $this->response->setCookie($cookieAttr); - + set_cookie($cookieAttr); + $this->assertTrue($this->response->hasCookie($this->name, $this->value)); - $this->response->deleteCookie($this->name); + delete_cookie($this->name); } //-------------------------------------------------------------------- - public function testGetCookie() + public function testSetCookieSecured() { $pre = 'Hello, I try to'; $pst = 'your site'; @@ -68,29 +66,35 @@ final class cookieHelperTest extends \CIUnitTestCase $unsecured = 'unsecured'; $secured = 'secured'; - //set_cookie($unsecured, $unsec, $this->expire); - //set_cookie($secured, $sec, $this->expire); - $this->response->setCookie($unsecured, $unsec, $this->expire); - $this->response->setCookie($secured, $sec, $this->expire); + set_cookie($unsecured, $unsec, $this->expire); + set_cookie($secured, $sec, $this->expire); $this->assertTrue($this->response->hasCookie($unsecured, $unsec)); $this->assertTrue($this->response->hasCookie($secured, $sec)); - $this->response->deleteCookie($unsecured); - $this->response->deleteCookie($secured); + delete_cookie($unsecured); + delete_cookie($secured); } //-------------------------------------------------------------------- public function testDeleteCookie() { - //set_cookie($this->name, $this->value, $this->expire); - $this->response->setCookie($this->name, $this->value, $this->expire); + set_cookie($this->name, $this->value, $this->expire); + //$this->response->setCookie($this->name, $this->value, $this->expire); - $this->response->deleteCookie($this->name); + delete_cookie($this->name); - //$this->assertEquals(get_cookie($this->name), ''); - $this->assertTrue($this->response->hasCookie($this->name)); + $this->assertEmpty($this->response->getCookie($this->name)); + } + + //-------------------------------------------------------------------- + + public function testGetCookie() + { + $_COOKIE['TEST'] = 5; + + $this->assertEquals(5, get_cookie('TEST')); } } diff --git a/tests/system/Helpers/DateHelperTest.php b/tests/system/Helpers/DateHelperTest.php new file mode 100644 index 0000000000..b718ec2fd4 --- /dev/null +++ b/tests/system/Helpers/DateHelperTest.php @@ -0,0 +1,44 @@ +assertLessThan(1, abs(now() - time())); + } + + //-------------------------------------------------------------------- + + public function testNowSpecific() + { + // Chicago should be two hours ahead of Vancouver + $this->assertEquals(7200,now('America/Chicago')-now('America/Vancouver')); +// $zone = 'America/Vancouver'; +// $time = new \DateTime('now', new \DateTimeZone($zone)); +// echo now($zone) . '\n'; +// echo now() . '\n'; +// echo $time->getTimestamp() . '\n'; +// +// $this->assertLessThan(1, abs(now($zone) - $time->getTimestamp())); + } + +} diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst new file mode 100644 index 0000000000..e6d451f4fe --- /dev/null +++ b/user_guide_src/source/helpers/date_helper.rst @@ -0,0 +1,47 @@ +########### +Date Helper +########### + +The Date Helper file contains functions that assist in working with +dates. + +.. contents:: + :local: + +.. raw:: html + +
+ +Loading this Helper +=================== + +This helper is loaded using the following code:: + + helper('date'); + +Available Functions +=================== + +The following functions are available: + +.. php:function:: now([$timezone = NULL]) + + :param string $timezone: Timezone + :returns: UNIX timestamp + :rtype: int + + Returns the current time as a UNIX timestamp, referenced either to your server's + local time or any PHP supported timezone, based on the "time reference" setting + in your config file. If you do not intend to set your master time reference to + any other PHP supported timezone (which you'll typically do if you run a site + that lets each user set their own timezone settings) there is no benefit to using + this function over PHP's ``time()`` function. + :: + + echo now('Australia/Victoria'); + + If a timezone is not provided, it will return ``time()`` based on the + **time_reference** setting. + +Many functions previously found in the CodeIgniter 3 ``date_helper`` have been moved to the ``I18n`` +module in CodeIgniter 4.