Update tests for fsetFolder

This commit is contained in:
2026-06-25 16:26:15 +09:00
parent 7e01886ad8
commit d66fce374c
3 changed files with 61 additions and 37 deletions
@@ -15,6 +15,12 @@ if [ ! -f "${load_sql}" ]; then
echo 1;
exit 1;
fi;
# if we have no dropdb, createdb, psql, exit with 6
if ! command -v dropdb &> /dev/null || ! command -v createdb &> /dev/null || ! command -v psql &> /dev/null; then
echo 6;
exit 6;
fi;
db_user="${2}";
db_name="${3}";
db_host="${4}";
@@ -25,13 +31,13 @@ if [ -z "${db_user}" ] || [ -z "${db_name}" ] || [ -z "${db_host}" ]; then
fi;
# drop database, on error exit with 3
dropdb -U "${db_user}" -h "${db_host}" "${db_name}" 2>&1;
if ! $?; then
if [ ! $? ]; then
echo 3;
exit 3;
fi;
# create database, on error exit with 4
createdb -U "${db_user}" -O "${db_user}" -h "${db_host}" -E utf8 "${db_name}" 2>&1;
if ! $?; then
if [ ! $? ]; then
echo 4;
exit 4;
fi;
@@ -42,7 +48,7 @@ else
# load data (redirect ALL error to null), on error exit with 5
psql -U "${db_user}" -h "${db_host}" -f "${load_sql}" "${db_name}" 2>&1 1>/dev/null 2>/dev/null;
fi;
if ! $?; then
if [ ! $? ]; then
echo 5;
exit 5;
fi;
@@ -13,29 +13,6 @@ use PHPUnit\Framework\TestCase;
*/
final class CoreLibsDebugFileWriterTest extends TestCase
{
/**
* Undocumented function
*
* @return array
*/
public function fsetFolderProvider(): array
{
return [
'valid log folder name' => [
0 => '/tmp/',
1 => true,
],
'invalid log folder name' => [
0 => 'some name',
1 => false,
],
'not writeable log folder name' => [
0 => '/opt',
1 => false,
]
];
}
/**
* Undocumented function
*
@@ -85,18 +62,59 @@ final class CoreLibsDebugFileWriterTest extends TestCase
/**
* Undocumented function
*
* @dataProvider fsetFolderProvider
* @testdox fsetFolder $input will match $expected [$_dataName]
* @testdox fsetFolder test correct return code
*
* @param string $input
* @param boolean $expected
* @return void
*/
public function testFsetFolder(string $input, bool $expected): void
public function testFsetFolder(): void
{
// check the following
// - valid folder, writeable
// - valid folder, not writeable
// - invalid folder (eg file)
// - invalid folder name (eg contains spaces), must match ^[\w\-\/]+
// if we have no /tmp/ folder, we cannot test this, so skip the test
if (!is_dir('/tmp')) {
$this->markTestSkipped('No /tmp folder found, cannot test fsetFolder');
}
// TEST 3:
// create a file in /tmp/ to test invalid folder (eg file)
$test_file = '/tmp/somefile.txt';
if (!is_file($test_file)) {
touch($test_file);
}
$this->assertEquals(
$expected,
\CoreLibs\Debug\FileWriter::fsetFolder($input)
false,
\CoreLibs\Debug\FileWriter::fsetFolder($test_file)
);
// TEST 4:
// test invalid folder name (eg contains spaces), must match ^[\w\-\/]+
$this->assertEquals(
false,
\CoreLibs\Debug\FileWriter::fsetFolder('some name')
);
// TEST 1:
// create a folder in /tmp/ to test valid folder, writeable
$test_folder = '/tmp/somefolder';
if (!is_dir($test_folder)) {
mkdir($test_folder);
}
$this->assertEquals(
true,
\CoreLibs\Debug\FileWriter::fsetFolder($test_folder)
);
// TEST 2:
// create a folder in /tmp/ to test valid folder, not writeable
$test_folder = '/tmp/somefolder2';
if (!is_dir($test_folder)) {
mkdir($test_folder);
}
// remove all write permissions for the folder
chmod($test_folder, 0555);
$this->assertEquals(
false,
\CoreLibs\Debug\FileWriter::fsetFolder($test_folder)
);
}
+4 -4
View File
@@ -32,11 +32,11 @@ class FileWriter
if (!is_writeable($folder)) {
return false;
}
// if last is not / then add
if (substr($folder, -1, 1) != DIRECTORY_SEPARATOR) {
$folder .= DIRECTORY_SEPARATOR;
if (!is_dir($folder)) {
return false;
}
self::$debug_folder = $folder;
// if last is not / then add
self::$debug_folder = rtrim($folder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
return true;
}