mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Fleshed out testing for array, cookie and date (such as it is) helpers
This commit is contained in:
parent
1451e82a0d
commit
f8e5926deb
@ -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 = [
|
||||
|
@ -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'));
|
||||
}
|
||||
|
||||
}
|
||||
|
44
tests/system/Helpers/DateHelperTest.php
Normal file
44
tests/system/Helpers/DateHelperTest.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php namespace CodeIgniter\HTTP;
|
||||
|
||||
use Config\App;
|
||||
use CodeIgniter\Config\Services;
|
||||
use Tests\Support\HTTP\MockResponse;
|
||||
|
||||
final class DateHelperTest extends \CIUnitTestCase
|
||||
{
|
||||
|
||||
private $name;
|
||||
private $value;
|
||||
private $expire;
|
||||
private $response;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
helper('date');
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testNowDefault()
|
||||
{
|
||||
$time = new \DateTime();
|
||||
$this->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()));
|
||||
}
|
||||
|
||||
}
|
47
user_guide_src/source/helpers/date_helper.rst
Normal file
47
user_guide_src/source/helpers/date_helper.rst
Normal file
@ -0,0 +1,47 @@
|
||||
###########
|
||||
Date Helper
|
||||
###########
|
||||
|
||||
The Date Helper file contains functions that assist in working with
|
||||
dates.
|
||||
|
||||
.. contents::
|
||||
:local:
|
||||
|
||||
.. raw:: html
|
||||
|
||||
<div class="custom-index container"></div>
|
||||
|
||||
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.
|
Loading…
x
Reference in New Issue
Block a user