Skip to content

Commit 6d74307

Browse files
authored
Merge pull request #1086 from jim-parry/testing/helpers
Testing/helpers
2 parents d442db4 + 3d064c1 commit 6d74307

File tree

6 files changed

+535
-370
lines changed

6 files changed

+535
-370
lines changed

system/Helpers/filesystem_helper.php

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* CodeIgniter
45
*
@@ -35,7 +36,6 @@
3536
* @since Version 1.0.0
3637
* @filesource
3738
*/
38-
3939
/**
4040
* CodeIgniter Directory Helpers
4141
*
@@ -66,8 +66,10 @@
6666
*/
6767
function directory_map(string $source_dir, int $directory_depth = 0, bool $hidden = false): array
6868
{
69-
if ($fp = @opendir($source_dir))
69+
try
7070
{
71+
$fp = opendir($source_dir);
72+
7173
$filedata = [];
7274
$new_depth = $directory_depth - 1;
7375
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
@@ -95,8 +97,10 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
9597
closedir($fp);
9698
return $filedata;
9799
}
98-
99-
return [];
100+
catch (\Exception $fe)
101+
{
102+
return [];
103+
}
100104
}
101105

102106
}
@@ -120,25 +124,29 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
120124
*/
121125
function write_file(string $path, string $data, string $mode = 'wb'): bool
122126
{
123-
if ( ! $fp = @fopen($path, $mode))
127+
try
124128
{
125-
return false;
126-
}
129+
$fp = fopen($path, $mode);
127130

128-
flock($fp, LOCK_EX);
131+
flock($fp, LOCK_EX);
129132

130-
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
131-
{
132-
if (($result = fwrite($fp, substr($data, $written))) === false)
133+
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
133134
{
134-
break;
135+
if (($result = fwrite($fp, substr($data, $written))) === false)
136+
{
137+
break;
138+
}
135139
}
136-
}
137140

138-
flock($fp, LOCK_UN);
139-
fclose($fp);
141+
flock($fp, LOCK_UN);
142+
fclose($fp);
140143

141-
return is_int($result);
144+
return is_int($result);
145+
}
146+
catch (\Exception $fe)
147+
{
148+
return false;
149+
}
142150
}
143151

144152
}
@@ -168,29 +176,33 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
168176
// Trim the trailing slash
169177
$path = rtrim($path, '/\\');
170178

171-
if ( ! $current_dir = @opendir($path))
179+
try
172180
{
173-
return false;
174-
}
181+
$current_dir = opendir($path);
175182

176-
while (false !== ($filename = @readdir($current_dir)))
177-
{
178-
if ($filename !== '.' && $filename !== '..')
183+
while (false !== ($filename = @readdir($current_dir)))
179184
{
180-
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
181-
{
182-
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
183-
}
184-
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
185+
if ($filename !== '.' && $filename !== '..')
185186
{
186-
@unlink($path . DIRECTORY_SEPARATOR . $filename);
187+
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
188+
{
189+
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
190+
}
191+
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
192+
{
193+
@unlink($path . DIRECTORY_SEPARATOR . $filename);
194+
}
187195
}
188196
}
189-
}
190197

191-
closedir($current_dir);
198+
closedir($current_dir);
192199

193-
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
200+
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
201+
}
202+
catch (\Exception $fe)
203+
{
204+
return false;
205+
}
194206
}
195207

196208
}
@@ -216,8 +228,9 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
216228
{
217229
static $filedata = [];
218230

219-
if ($fp = @opendir($source_dir))
231+
try
220232
{
233+
$fp = opendir($source_dir);
221234
// reset the array and make sure $source_dir has a trailing slash on the initial call
222235
if ($recursion === false)
223236
{
@@ -240,8 +253,10 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
240253
closedir($fp);
241254
return $filedata;
242255
}
243-
244-
return [];
256+
catch (\Exception $fe)
257+
{
258+
return [];
259+
}
245260
}
246261

247262
}
@@ -270,34 +285,38 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
270285
static $filedata = [];
271286
$relative_path = $source_dir;
272287

273-
if ($fp = @opendir($source_dir))
288+
try
274289
{
275-
// reset the array and make sure $source_dir has a trailing slash on the initial call
276-
if ($recursion === false)
277-
{
278-
$filedata = [];
279-
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
280-
}
281-
282-
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
283-
while (false !== ($file = readdir($fp)))
284-
{
285-
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
290+
$fp = @opendir($source_dir); {
291+
// reset the array and make sure $source_dir has a trailing slash on the initial call
292+
if ($recursion === false)
286293
{
287-
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
294+
$filedata = [];
295+
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
288296
}
289-
elseif ($file[0] !== '.')
297+
298+
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
299+
while (false !== ($file = readdir($fp)))
290300
{
291-
$filedata[$file] = get_file_info($source_dir . $file);
292-
$filedata[$file]['relative_path'] = $relative_path;
301+
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
302+
{
303+
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
304+
}
305+
elseif ($file[0] !== '.')
306+
{
307+
$filedata[$file] = get_file_info($source_dir . $file);
308+
$filedata[$file]['relative_path'] = $relative_path;
309+
}
293310
}
294-
}
295311

296-
closedir($fp);
297-
return $filedata;
312+
closedir($fp);
313+
return $filedata;
314+
}
315+
}
316+
catch (\Exception $fe)
317+
{
318+
return [];
298319
}
299-
300-
return [];
301320
}
302321

303322
}
@@ -318,9 +337,9 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
318337
* @param string $file Path to file
319338
* @param mixed $returned_values Array or comma separated string of information returned
320339
*
321-
* @return array
340+
* @return array|null
322341
*/
323-
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']): array
342+
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date'])
324343
{
325344
if ( ! file_exists($file))
326345
{
@@ -334,8 +353,7 @@ function get_file_info(string $file, $returned_values = ['name', 'server_path',
334353

335354
foreach ($returned_values as $key)
336355
{
337-
switch ($key)
338-
{
356+
switch ($key) {
339357
case 'name':
340358
$fileinfo['name'] = basename($file);
341359
break;

tests/system/Helpers/ArrayHelperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ public function testArrayDotWildcardWithMultipleChoices()
9090
$this->assertEquals(23, dot_array_search('foo.*.baz', $data));
9191
}
9292

93+
public function testArrayDotNestedNotFound()
94+
{
95+
$data = [
96+
'foo' => [
97+
'buzz' => [
98+
'fizz' => 11
99+
],
100+
'bar' => [
101+
'baz' => 23
102+
]
103+
]
104+
];
105+
106+
$this->assertNull(dot_array_search('foo.*.notthere', $data));
107+
}
108+
93109
public function testArrayDotIgnoresLastWildcard()
94110
{
95111
$data = [

tests/system/Helpers/CookieHelperTest.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use CodeIgniter\Config\Services;
55
use Tests\Support\HTTP\MockResponse;
66

7-
final class cookieHelperTest extends \CIUnitTestCase
7+
final class CookieHelperTest extends \CIUnitTestCase
88
{
99

1010
private $name;
@@ -22,6 +22,8 @@ public function setUp()
2222

2323
Services::injectMock('response', new MockResponse(new App()));
2424
$this->response = service('response');
25+
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
26+
Services::injectMock('request', $this->request);
2527

2628
helper('cookie');
2729
}
@@ -30,14 +32,11 @@ public function setUp()
3032

3133
public function testSetCookie()
3234
{
33-
$this->response->setCookie($this->name, $this->value, $this->expire);
34-
35-
//TODO: Find a way for set_cookie() to use the MockResponse object.
36-
//set_cookie($this->name, $this->value, $this->expire);
35+
set_cookie($this->name, $this->value, $this->expire);
3736

3837
$this->assertTrue($this->response->hasCookie($this->name));
3938

40-
$this->response->deleteCookie($this->name);
39+
delete_cookie($this->name);
4140
}
4241

4342
//--------------------------------------------------------------------
@@ -49,17 +48,16 @@ public function testSetCookieByArrayParameters()
4948
'value' => $this->value,
5049
'expire' => $this->expire
5150
];
52-
//set_cookie($cookieAttr);
53-
$this->response->setCookie($cookieAttr);
54-
51+
set_cookie($cookieAttr);
52+
5553
$this->assertTrue($this->response->hasCookie($this->name, $this->value));
5654

57-
$this->response->deleteCookie($this->name);
55+
delete_cookie($this->name);
5856
}
5957

6058
//--------------------------------------------------------------------
6159

62-
public function testGetCookie()
60+
public function testSetCookieSecured()
6361
{
6462
$pre = 'Hello, I try to';
6563
$pst = 'your site';
@@ -68,29 +66,35 @@ public function testGetCookie()
6866
$unsecured = 'unsecured';
6967
$secured = 'secured';
7068

71-
//set_cookie($unsecured, $unsec, $this->expire);
72-
//set_cookie($secured, $sec, $this->expire);
73-
$this->response->setCookie($unsecured, $unsec, $this->expire);
74-
$this->response->setCookie($secured, $sec, $this->expire);
69+
set_cookie($unsecured, $unsec, $this->expire);
70+
set_cookie($secured, $sec, $this->expire);
7571

7672
$this->assertTrue($this->response->hasCookie($unsecured, $unsec));
7773
$this->assertTrue($this->response->hasCookie($secured, $sec));
7874

79-
$this->response->deleteCookie($unsecured);
80-
$this->response->deleteCookie($secured);
75+
delete_cookie($unsecured);
76+
delete_cookie($secured);
8177
}
8278

8379
//--------------------------------------------------------------------
8480

8581
public function testDeleteCookie()
8682
{
87-
//set_cookie($this->name, $this->value, $this->expire);
88-
$this->response->setCookie($this->name, $this->value, $this->expire);
83+
set_cookie($this->name, $this->value, $this->expire);
84+
//$this->response->setCookie($this->name, $this->value, $this->expire);
8985

90-
$this->response->deleteCookie($this->name);
86+
delete_cookie($this->name);
9187

92-
//$this->assertEquals(get_cookie($this->name), '');
93-
$this->assertTrue($this->response->hasCookie($this->name));
88+
$this->assertEmpty($this->response->getCookie($this->name));
89+
}
90+
91+
//--------------------------------------------------------------------
92+
93+
public function testGetCookie()
94+
{
95+
$_COOKIE['TEST'] = 5;
96+
97+
$this->assertEquals(5, get_cookie('TEST'));
9498
}
9599

96100
}

0 commit comments

Comments
 (0)