Merge pull request #1086 from jim-parry/testing/helpers

Testing/helpers
This commit is contained in:
Lonnie Ezell 2018-06-26 15:41:45 -05:00 committed by GitHub
commit 6d74307d1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 497 additions and 332 deletions

View File

@ -1,4 +1,5 @@
<?php
/**
* CodeIgniter
*
@ -35,7 +36,6 @@
* @since Version 1.0.0
* @filesource
*/
/**
* CodeIgniter Directory Helpers
*
@ -66,8 +66,10 @@ if ( ! function_exists('directory_map'))
*/
function directory_map(string $source_dir, int $directory_depth = 0, bool $hidden = false): array
{
if ($fp = @opendir($source_dir))
try
{
$fp = opendir($source_dir);
$filedata = [];
$new_depth = $directory_depth - 1;
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
@ -95,8 +97,10 @@ if ( ! function_exists('directory_map'))
closedir($fp);
return $filedata;
}
return [];
catch (\Exception $fe)
{
return [];
}
}
}
@ -120,25 +124,29 @@ if ( ! function_exists('write_file'))
*/
function write_file(string $path, string $data, string $mode = 'wb'): bool
{
if ( ! $fp = @fopen($path, $mode))
try
{
$fp = fopen($path, $mode);
flock($fp, LOCK_EX);
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($data, $written))) === false)
{
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
return is_int($result);
}
catch (\Exception $fe)
{
return false;
}
flock($fp, LOCK_EX);
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($data, $written))) === false)
{
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
return is_int($result);
}
}
@ -168,29 +176,33 @@ if ( ! function_exists('delete_files'))
// Trim the trailing slash
$path = rtrim($path, '/\\');
if ( ! $current_dir = @opendir($path))
try
{
$current_dir = opendir($path);
while (false !== ($filename = @readdir($current_dir)))
{
if ($filename !== '.' && $filename !== '..')
{
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
{
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
}
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
{
@unlink($path . DIRECTORY_SEPARATOR . $filename);
}
}
}
closedir($current_dir);
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
}
catch (\Exception $fe)
{
return false;
}
while (false !== ($filename = @readdir($current_dir)))
{
if ($filename !== '.' && $filename !== '..')
{
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
{
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
}
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
{
@unlink($path . DIRECTORY_SEPARATOR . $filename);
}
}
}
closedir($current_dir);
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
}
}
@ -216,8 +228,9 @@ if ( ! function_exists('get_filenames'))
{
static $filedata = [];
if ($fp = @opendir($source_dir))
try
{
$fp = opendir($source_dir);
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
@ -240,8 +253,10 @@ if ( ! function_exists('get_filenames'))
closedir($fp);
return $filedata;
}
return [];
catch (\Exception $fe)
{
return [];
}
}
}
@ -270,34 +285,38 @@ if ( ! function_exists('get_dir_file_info'))
static $filedata = [];
$relative_path = $source_dir;
if ($fp = @opendir($source_dir))
try
{
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
$filedata = [];
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
while (false !== ($file = readdir($fp)))
{
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
$fp = @opendir($source_dir); {
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
$filedata = [];
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
elseif ($file[0] !== '.')
{
$filedata[$file] = get_file_info($source_dir . $file);
$filedata[$file]['relative_path'] = $relative_path;
}
}
closedir($fp);
return $filedata;
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
while (false !== ($file = readdir($fp)))
{
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
{
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
}
elseif ($file[0] !== '.')
{
$filedata[$file] = get_file_info($source_dir . $file);
$filedata[$file]['relative_path'] = $relative_path;
}
}
closedir($fp);
return $filedata;
}
}
catch (\Exception $fe)
{
return [];
}
return [];
}
}
@ -318,9 +337,9 @@ if ( ! function_exists('get_file_info'))
* @param string $file Path to file
* @param mixed $returned_values Array or comma separated string of information returned
*
* @return array
* @return array|null
*/
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']): array
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date'])
{
if ( ! file_exists($file))
{
@ -334,8 +353,7 @@ if ( ! function_exists('get_file_info'))
foreach ($returned_values as $key)
{
switch ($key)
{
switch ($key) {
case 'name':
$fileinfo['name'] = basename($file);
break;

View File

@ -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 = [

View File

@ -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'));
}
}

View File

@ -0,0 +1,37 @@
<?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())); // close enough
}
//--------------------------------------------------------------------
public function testNowSpecific()
{
// Chicago should be two hours ahead of Vancouver
$this->assertEquals(7200,now('America/Chicago')-now('America/Vancouver'));
}
}

View File

@ -1,298 +1,341 @@
<?php namespace CodeIgniter\Helpers;
<?php
namespace CodeIgniter\Helpers;
use org\bovigo\vfs\vfsStream;
class FilesystemHelperTest extends \CIUnitTestCase
{
public function testDirectoryMapDefaults()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
public function setUp()
{
parent::setUp();
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
$this->structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
}
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile'
];
//--------------------------------------------------------------------
$root = vfsStream::setup('root', null, $structure);
$this->assertTrue($root->hasChild('foo'));
public function testDirectoryMapDefaults()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
$this->assertEquals($expected, directory_map(vfsStream::url('root')));
}
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile'
];
//--------------------------------------------------------------------
$root = vfsStream::setup('root', null, $this->structure);
$this->assertTrue($root->hasChild('foo'));
public function testDirectoryMapShowsHiddenFiles()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
$this->assertEquals($expected, directory_map(vfsStream::url('root')));
}
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
public function testDirectoryMapShowsHiddenFiles()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile',
'.hidden'
];
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile',
'.hidden'
];
$root = vfsStream::setup('root', null, $structure);
$this->assertTrue($root->hasChild('foo'));
$root = vfsStream::setup('root', null, $this->structure);
$this->assertTrue($root->hasChild('foo'));
$this->assertEquals($expected, directory_map(vfsStream::url('root'), false, true));
}
$this->assertEquals($expected, directory_map(vfsStream::url('root'), false, true));
}
//--------------------------------------------------------------------
public function testDirectoryMapLimitsRecursion()
{
$this->assertTrue(function_exists('directory_map'));
public function testDirectoryMapLimitsRecursion()
{
$this->assertTrue(function_exists('directory_map'));
$expected = [
'foo' . DIRECTORY_SEPARATOR,
'boo' . DIRECTORY_SEPARATOR,
'AnEmptyFolder' . DIRECTORY_SEPARATOR,
'simpleFile',
'.hidden'
];
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
$root = vfsStream::setup('root', null, $this->structure);
$this->assertTrue($root->hasChild('foo'));
$expected = [
'foo' . DIRECTORY_SEPARATOR,
'boo' . DIRECTORY_SEPARATOR,
'AnEmptyFolder' . DIRECTORY_SEPARATOR,
'simpleFile',
'.hidden'
];
$this->assertEquals($expected, directory_map(vfsStream::url('root'), 1, true));
}
$root = vfsStream::setup('root', null, $structure);
$this->assertTrue($root->hasChild('foo'));
public function testDirectoryMapHandlesNotfound()
{
$this->assertEquals([], directory_map(SUPPORTPATH . 'Files/shaker/'));
}
$this->assertEquals($expected, directory_map(vfsStream::url('root'), 1, true));
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
public function testWriteFileSuccess()
{
$vfs = vfsStream::setup('root');
public function testWriteFileSuccess()
{
$vfs = vfsStream::setup('root');
$this->assertTrue(write_file(vfsStream::url('root/test.php'), 'Simple'));
$this->assertFileExists($vfs->getChild('test.php')->url());
}
$this->assertTrue(write_file(vfsStream::url('root/test.php'), 'Simple'));
$this->assertFileExists($vfs->getChild('test.php')->url());
}
public function testWriteFileFailure()
{
$vfs = vfsStream::setup('root');
//--------------------------------------------------------------------
$this->assertFalse(write_file(vfsStream::url('apple#test.php'), 'Simple'));
}
public function testDeleteFilesDefaultsToOneLevelDeep()
{
$this->assertTrue(function_exists('delete_files'));
//--------------------------------------------------------------------
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
public function testDeleteFilesDefaultsToOneLevelDeep()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $structure);
$vfs = vfsStream::setup('root', null, $this->structure);
delete_files(vfsStream::url('root'));
delete_files(vfsStream::url('root'));
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertTrue($vfs->hasChild('foo'));
$this->assertTrue($vfs->hasChild('boo'));
$this->assertTrue($vfs->hasChild('AnEmptyFolder'));
}
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertTrue($vfs->hasChild('foo'));
$this->assertTrue($vfs->hasChild('boo'));
$this->assertTrue($vfs->hasChild('AnEmptyFolder'));
}
//--------------------------------------------------------------------
public function testDeleteFilesHandlesRecursion()
{
$this->assertTrue(function_exists('delete_files'));
public function testDeleteFilesHandlesRecursion()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $this->structure);
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
delete_files(vfsStream::url('root'), true);
$vfs = vfsStream::setup('root', null, $structure);
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
}
delete_files(vfsStream::url('root'), true);
public function testDeleteFilesLeavesHTFiles()
{
$structure = array_merge($this->structure, [
'.htaccess' => 'Deny All',
'index.html' => 'foo',
'index.php' => 'blah'
]);
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
}
$vfs = vfsStream::setup('root', null, $structure);
//--------------------------------------------------------------------
delete_files(vfsStream::url('root'), true, true);
public function testDeleteFilesLeavesHTFiles()
{
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon',
'.htaccess' => 'Deny All',
'index.html' => 'foo',
'index.php' => 'blah'
];
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
$this->assertTrue($vfs->hasChild('.htaccess'));
$this->assertTrue($vfs->hasChild('index.html'));
$this->assertTrue($vfs->hasChild('index.php'));
}
$vfs = vfsStream::setup('root', null, $structure);
public function testDeleteFilesFailure()
{
$this->assertFalse(delete_files(SUPPORTPATH . 'Files/shaker/'));
}
delete_files(vfsStream::url('root'), true, true);
//--------------------------------------------------------------------
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
$this->assertTrue($vfs->hasChild('.htaccess'));
$this->assertTrue($vfs->hasChild('index.html'));
$this->assertTrue($vfs->hasChild('index.php'));
}
public function testGetFilenames()
{
$this->assertTrue(function_exists('delete_files'));
//--------------------------------------------------------------------
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
'foo',
'boo',
'AnEmptyFolder',
'simpleFile'
];
public function testGetFilenames()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $this->structure);
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
$this->assertEquals($expected, get_filenames($vfs->url(), false));
}
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
'foo',
'boo',
'AnEmptyFolder',
'simpleFile'
];
public function testGetFilenamesWithSource()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $structure);
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
DIRECTORY_SEPARATOR . 'foo',
DIRECTORY_SEPARATOR . 'boo',
DIRECTORY_SEPARATOR . 'AnEmptyFolder',
DIRECTORY_SEPARATOR . 'simpleFile'
];
$this->assertEquals($expected, get_filenames($vfs->url(), false));
}
$vfs = vfsStream::setup('root', null, $this->structure);
//--------------------------------------------------------------------
$this->assertEquals($expected, get_filenames($vfs->url(), true));
}
public function testGetFilenamesWithSource()
{
$this->assertTrue(function_exists('delete_files'));
public function testGetFilenamesFailure()
{
$this->assertEquals([], get_filenames(SUPPORTPATH . 'Files/shaker/'));
}
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
//--------------------------------------------------------------------
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
DIRECTORY_SEPARATOR . 'foo',
DIRECTORY_SEPARATOR . 'boo',
DIRECTORY_SEPARATOR . 'AnEmptyFolder',
DIRECTORY_SEPARATOR . 'simpleFile'
];
public function testGetDirFileInfo()
{
$expected = [
'banana.php' => [
'name' => 'banana.php',
'server_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker/banana.php',
'size' => 193,
'date' => 1529305930,
'relative_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker',
]
];
$vfs = vfsStream::setup('root', null, $structure);
$this->assertEquals($expected, get_filenames($vfs->url(), true));
}
$this->assertEquals($expected, get_dir_file_info(SUPPORTPATH . 'Files/baker'));
}
//--------------------------------------------------------------------
public function testGetDirFileInfoNested()
{
$expected = ['banana.php', 'prune_ripe.php', 'fig_3.php', 'apple.php'];
$results = get_dir_file_info(SUPPORTPATH . 'Files', false);
$this->assertEmpty(array_diff($expected, array_keys($results)));
}
public function testGetDirFileInfoFailure()
{
$expected = [];
$this->assertEquals($expected, get_dir_file_info(SUPPORTPATH . 'Files#baker'));
}
//--------------------------------------------------------------------
public function testGetFileInfo()
{
$expected = [
'name' => 'banana.php',
'server_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker/banana.php',
'size' => 193,
'date' => 1529305930,
];
$this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/baker/banana.php'));
}
public function testGetFileInfoCustom()
{
$expected = [
'readable' => true,
'writable' => true,
'executable' => false,
];
$this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/baker/banana.php', 'readable,writable,executable'));
}
public function testGetFileInfoPerms()
{
$expected = 0664;
$stuff = get_file_info(SUPPORTPATH . 'Files/baker/banana.php', 'fileperms');
$this->assertEquals($expected, $stuff['fileperms'] & 0777);
}
public function testGetFileNotThereInfo()
{
$expected = null;
$this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/icer'));
}
//--------------------------------------------------------------------
public function testOctalPermissions()
{
$this->assertEquals('777', octal_permissions(0777));
$this->assertEquals('655', octal_permissions(0655));
$this->assertEquals('123', octal_permissions(0123));
}
public function testSymbolicPermissions()
{
$expected = [
0777 => 'urwxrwxrwx',
0655 => 'urw-r-xr-x',
0123 => 'u--x-w--wx',
010655 => 'prw-r-xr-x',
020655 => 'crw-r-xr-x',
040655 => 'drw-r-xr-x',
060655 => 'brw-r-xr-x',
0100655 => '-rw-r-xr-x',
0120655 => 'lrw-r-xr-x',
0140655 => 'srw-r-xr-x',
];
foreach ($expected as $perm => $value)
$this->assertEquals($value, symbolic_permissions($perm));
}
//--------------------------------------------------------------------
public function testRealPathURL()
{
$this->expectException(\InvalidArgumentException::class);
set_realpath('http://somewhere.com/overtherainbow');
}
public function testRealPathInvalid()
{
$this->expectException(\InvalidArgumentException::class);
set_realpath(SUPPORTPATH . 'root/../', true);
}
public function testRealPathResolved()
{
$this->assertEquals(SUPPORTPATH . 'Helpers/', set_realpath(SUPPORTPATH . 'Files/../Helpers', true));
}
}

View 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.