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
f416a366
Commit
f416a366
authored
Jun 10, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"FileHelperTest" has been advanced.
parent
539864f4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
126 additions
and
2 deletions
+126
-2
FileHelperTest.php
tests/unit/framework/helpers/FileHelperTest.php
+126
-2
No files found.
tests/unit/framework/helpers/FileHelperTest.php
View file @
f416a366
...
...
@@ -18,6 +18,9 @@ class FileHelperTest extends TestCase
{
$this
->
testFilePath
=
Yii
::
getAlias
(
'@yiiunit/runtime'
)
.
DIRECTORY_SEPARATOR
.
get_class
(
$this
);
$this
->
createDir
(
$this
->
testFilePath
);
if
(
!
file_exists
(
$this
->
testFilePath
))
{
$this
->
markTestIncomplete
(
'Unit tests runtime directory should have writable permissions!'
);
}
}
public
function
tearDown
()
...
...
@@ -43,8 +46,30 @@ class FileHelperTest extends TestCase
protected
function
removeDir
(
$dirName
)
{
if
(
!
empty
(
$dirName
)
&&
file_exists
(
$dirName
))
{
exec
(
"rm -rf
{
$dirName
}
"
);
if
(
$handle
=
opendir
(
$dirName
))
{
while
(
false
!==
(
$entry
=
readdir
(
$handle
)))
{
if
(
$entry
!=
'.'
&&
$entry
!=
'..'
)
{
if
(
is_dir
(
$dirName
.
DIRECTORY_SEPARATOR
.
$entry
)
===
true
)
{
$this
->
removeDir
(
$dirName
.
DIRECTORY_SEPARATOR
.
$entry
);
}
else
{
unlink
(
$dirName
.
DIRECTORY_SEPARATOR
.
$entry
);
}
}
}
closedir
(
$handle
);
rmdir
(
$dirName
);
}
}
}
/**
* Get file permission mode.
* @param string $file file name.
* @return string permission mode.
*/
protected
function
getMode
(
$file
)
{
return
substr
(
sprintf
(
'%o'
,
fileperms
(
$file
)),
-
4
);
}
/**
...
...
@@ -53,7 +78,8 @@ class FileHelperTest extends TestCase
* Arrays specifies directories, other values - files.
* @param string $basePath structure base file path.
*/
protected
function
createFileStructure
(
array
$items
,
$basePath
=
''
)
{
protected
function
createFileStructure
(
array
$items
,
$basePath
=
''
)
{
if
(
empty
(
$basePath
))
{
$basePath
=
$this
->
testFilePath
;
}
...
...
@@ -68,6 +94,18 @@ class FileHelperTest extends TestCase
}
}
/**
* Asserts that file has specific permission mode.
* @param integer $expectedMode expected file permission mode.
* @param string $fileName file name.
* @param string $message error message
*/
protected
function
assertFileMode
(
$expectedMode
,
$fileName
,
$message
=
''
)
{
$expectedMode
=
sprintf
(
'%o'
,
$expectedMode
);
$this
->
assertEquals
(
$expectedMode
,
$this
->
getMode
(
$fileName
),
$message
);
}
// Tests :
public
function
testCopyDirectory
()
...
...
@@ -95,6 +133,42 @@ class FileHelperTest extends TestCase
}
}
/**
* @depends testCopyDirectory
*/
public
function
testCopyDirectoryPermissions
()
{
if
(
substr
(
PHP_OS
,
0
,
3
)
==
'WIN'
)
{
$this
->
markTestSkipped
(
"Can't reliably test it on Windows because fileperms() always return 0777."
);
}
$srcDirName
=
'test_src_dir'
;
$subDirName
=
'test_sub_dir'
;
$fileName
=
'test_file.txt'
;
$this
->
createFileStructure
(
array
(
$srcDirName
=>
array
(
$subDirName
=>
array
(),
$fileName
=>
'test file content'
,
),
));
$basePath
=
$this
->
testFilePath
;
$srcDirName
=
$basePath
.
DIRECTORY_SEPARATOR
.
$srcDirName
;
$dstDirName
=
$basePath
.
DIRECTORY_SEPARATOR
.
'test_dst_dir'
;
$dirMode
=
0755
;
$fileMode
=
0755
;
$options
=
array
(
'dirMode'
=>
$dirMode
,
'fileMode'
=>
$fileMode
,
);
FileHelper
::
copyDirectory
(
$srcDirName
,
$dstDirName
,
$options
);
$this
->
assertFileMode
(
$dirMode
,
$dstDirName
,
'Destination directory has wrong mode!'
);
$this
->
assertFileMode
(
$dirMode
,
$dstDirName
.
DIRECTORY_SEPARATOR
.
$subDirName
,
'Copied sub directory has wrong mode!'
);
$this
->
assertFileMode
(
$fileMode
,
$dstDirName
.
DIRECTORY_SEPARATOR
.
$fileName
,
'Copied file has wrong mode!'
);
}
public
function
testRemoveDirectory
()
{
$dirName
=
'test_dir_for_remove'
;
...
...
@@ -143,4 +217,53 @@ class FileHelperTest extends TestCase
sort
(
$expectedFiles
);
$this
->
assertEquals
(
$expectedFiles
,
$foundFiles
);
}
/**
* @depends testFindFiles
*/
public
function
testFindFilesExclude
()
{
$dirName
=
'test_dir'
;
$fileName
=
'test_file.txt'
;
$excludeFileName
=
'exclude_file.txt'
;
$this
->
createFileStructure
(
array
(
$dirName
=>
array
(
$fileName
=>
'file content'
,
$excludeFileName
=>
'exclude file content'
,
),
));
$basePath
=
$this
->
testFilePath
;
$dirName
=
$basePath
.
DIRECTORY_SEPARATOR
.
$dirName
;
$options
=
array
(
'exclude'
=>
array
(
$excludeFileName
),
);
$foundFiles
=
FileHelper
::
findFiles
(
$dirName
,
$options
);
$this
->
assertEquals
(
array
(
$dirName
.
DIRECTORY_SEPARATOR
.
$fileName
),
$foundFiles
);
}
/**
* @depends testFindFiles
*/
public
function
testFindFilesFileType
()
{
$dirName
=
'test_dir'
;
$fileType
=
'dat'
;
$fileName
=
'test_file.'
.
$fileType
;
$excludeFileName
=
'exclude_file.txt'
;
$this
->
createFileStructure
(
array
(
$dirName
=>
array
(
$fileName
=>
'file content'
,
$excludeFileName
=>
'exclude file content'
,
),
));
$basePath
=
$this
->
testFilePath
;
$dirName
=
$basePath
.
DIRECTORY_SEPARATOR
.
$dirName
;
$options
=
array
(
'fileTypes'
=>
array
(
$fileType
),
);
$foundFiles
=
FileHelper
::
findFiles
(
$dirName
,
$options
);
$this
->
assertEquals
(
array
(
$dirName
.
DIRECTORY_SEPARATOR
.
$fileName
),
$foundFiles
);
}
}
\ No newline at end of file
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