Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b2b10d16
Commit
b2b10d16
authored
Jan 26, 2014
by
John Was
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more unit tests for helpers/FileHelper::findFiles using new wildcard exceptions matching
parent
6a84bfff
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
16 deletions
+52
-16
BaseFileHelper.php
framework/helpers/BaseFileHelper.php
+1
-0
FileHelperTest.php
tests/unit/framework/helpers/FileHelperTest.php
+51
-16
No files found.
framework/helpers/BaseFileHelper.php
View file @
b2b10d16
...
@@ -259,6 +259,7 @@ class BaseFileHelper
...
@@ -259,6 +259,7 @@ class BaseFileHelper
if
(
!
is_dir
(
$dir
))
{
if
(
!
is_dir
(
$dir
))
{
throw
new
InvalidParamException
(
'The dir argument must be a directory.'
);
throw
new
InvalidParamException
(
'The dir argument must be a directory.'
);
}
}
$dir
=
rtrim
(
$dir
,
DIRECTORY_SEPARATOR
);
if
(
!
isset
(
$options
[
'basePath'
]))
{
if
(
!
isset
(
$options
[
'basePath'
]))
{
$options
[
'basePath'
]
=
realpath
(
$dir
);
$options
[
'basePath'
]
=
realpath
(
$dir
);
// this should also be done only once
// this should also be done only once
...
...
tests/unit/framework/helpers/FileHelperTest.php
View file @
b2b10d16
...
@@ -253,23 +253,58 @@ class FileHelperTest extends TestCase
...
@@ -253,23 +253,58 @@ class FileHelperTest extends TestCase
*/
*/
public
function
testFindFilesExclude
()
public
function
testFindFilesExclude
()
{
{
$dirName
=
'test_dir'
;
$basePath
=
$this
->
testFilePath
.
DIRECTORY_SEPARATOR
;
$fileName
=
'test_file.txt'
;
$dirs
=
array
(
''
,
'one'
,
'one'
.
DIRECTORY_SEPARATOR
.
'two'
,
'three'
);
$excludeFileName
=
'exclude_file.txt'
;
$files
=
array_fill_keys
(
array_map
(
function
(
$n
){
return
"a.
$n
"
;},
range
(
1
,
8
)),
'file contents'
);
$this
->
createFileStructure
([
$dirName
=>
[
$tree
=
$files
;
$fileName
=>
'file content'
,
$root
=
$files
;
$excludeFileName
=>
'exclude file content'
,
$flat
=
array
();
],
foreach
(
$dirs
as
$dir
)
{
]);
foreach
(
$files
as
$fileName
=>
$contents
)
{
$basePath
=
$this
->
testFilePath
;
$flat
[]
=
rtrim
(
$basePath
.
$dir
,
DIRECTORY_SEPARATOR
)
.
DIRECTORY_SEPARATOR
.
$fileName
;
$dirName
=
$basePath
.
DIRECTORY_SEPARATOR
.
$dirName
;
}
if
(
$dir
===
''
)
continue
;
$parts
=
explode
(
DIRECTORY_SEPARATOR
,
$dir
);
$last
=
array_pop
(
$parts
);
$parent
=
array_pop
(
$parts
);
$tree
[
$last
]
=
$files
;
if
(
$parent
!==
null
)
{
$tree
[
$parent
][
$last
]
=
&
$tree
[
$last
];
}
else
{
$root
[
$last
]
=
&
$tree
[
$last
];
}
}
$this
->
createFileStructure
(
$root
);
$options
=
[
// range
'except'
=>
[
$excludeFileName
],
$foundFiles
=
FileHelper
::
findFiles
(
$basePath
,
[
'except'
=>
[
'a.[2-8]'
]]);
];
sort
(
$foundFiles
);
$foundFiles
=
FileHelper
::
findFiles
(
$dirName
,
$options
);
$expect
=
array_values
(
array_filter
(
$flat
,
function
(
$p
){
return
substr
(
$p
,
-
3
)
===
'a.1'
;}));
$this
->
assertEquals
([
$dirName
.
DIRECTORY_SEPARATOR
.
$fileName
],
$foundFiles
);
$this
->
assertEquals
(
$expect
,
$foundFiles
);
// suffix
$foundFiles
=
FileHelper
::
findFiles
(
$basePath
,
[
'except'
=>
[
'*.1'
]]);
sort
(
$foundFiles
);
$expect
=
array_values
(
array_filter
(
$flat
,
function
(
$p
){
return
substr
(
$p
,
-
3
)
!==
'a.1'
;}));
$this
->
assertEquals
(
$expect
,
$foundFiles
);
// dir
$foundFiles
=
FileHelper
::
findFiles
(
$basePath
,
[
'except'
=>
[
'/one'
]]);
sort
(
$foundFiles
);
$expect
=
array_values
(
array_filter
(
$flat
,
function
(
$p
){
return
strpos
(
$p
,
DIRECTORY_SEPARATOR
.
'one'
)
===
false
;}));
$this
->
assertEquals
(
$expect
,
$foundFiles
);
// dir contents
$foundFiles
=
FileHelper
::
findFiles
(
$basePath
,
[
'except'
=>
[
'?*/a.1'
]]);
sort
(
$foundFiles
);
$expect
=
array_values
(
array_filter
(
$flat
,
function
(
$p
){
return
substr
(
$p
,
-
11
,
10
)
===
'one'
.
DIRECTORY_SEPARATOR
.
'two'
.
DIRECTORY_SEPARATOR
.
'a.'
||
(
substr
(
$p
,
-
8
)
!==
DIRECTORY_SEPARATOR
.
'one'
.
DIRECTORY_SEPARATOR
.
'a.1'
&&
substr
(
$p
,
-
10
)
!==
DIRECTORY_SEPARATOR
.
'three'
.
DIRECTORY_SEPARATOR
.
'a.1'
);
}));
$this
->
assertEquals
(
$expect
,
$foundFiles
);
}
}
public
function
testCreateDirectory
()
public
function
testCreateDirectory
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment