Moved to tabs for indent and add phpcs.xml file, update phpunit to 12 and update tests
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ vendor
|
|||||||
composer.lock
|
composer.lock
|
||||||
**/.env
|
**/.env
|
||||||
**/.target
|
**/.target
|
||||||
|
.phpunit.cache
|
||||||
|
|||||||
@@ -26,10 +26,10 @@
|
|||||||
"exclude": ["/test/", "/test/*", "/phpstan.neon", "/psalm.xml", "/.phan/", "/.vscode/", "/phpunit.xml"]
|
"exclude": ["/test/", "/test/*", "/phpstan.neon", "/psalm.xml", "/.phan/", "/.vscode/", "/phpunit.xml"]
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9",
|
|
||||||
"phan/phan": "^5.4",
|
"phan/phan": "^5.4",
|
||||||
"phpstan/phpstan": "^2.0",
|
|
||||||
"phpstan/phpdoc-parser": "^2.0",
|
"phpstan/phpdoc-parser": "^2.0",
|
||||||
"phpstan/phpstan-deprecation-rules": "^2.0"
|
"phpstan/phpstan-deprecation-rules": "^2.0",
|
||||||
|
"phpstan/phpstan": "2.1.x-dev",
|
||||||
|
"phpunit/phpunit": "^12"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
phpcs.xml
Normal file
18
phpcs.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<ruleset name="MyStandard">
|
||||||
|
<description>PSR12 override rules (strict, standard). Switch spaces indent to tab.</description>
|
||||||
|
<arg name="tab-width" value="4"/>
|
||||||
|
<rule ref="PSR1"/>
|
||||||
|
<rule ref="PSR12">
|
||||||
|
<!-- turn off white space check for tab -->
|
||||||
|
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
|
||||||
|
</rule>
|
||||||
|
<!-- no space indent, must be tab, 4 is tab iwdth -->
|
||||||
|
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
|
||||||
|
<rule ref="Generic.WhiteSpace.ScopeIndent">
|
||||||
|
<properties>
|
||||||
|
<property name="indent" value="4"/>
|
||||||
|
<property name="tabIndent" value="true"/>
|
||||||
|
</properties>
|
||||||
|
</rule>
|
||||||
|
</ruleset>
|
||||||
16
phpunit.xml
16
phpunit.xml
@@ -1,10 +1,8 @@
|
|||||||
<phpunit
|
<?xml version="1.0"?>
|
||||||
colors="true"
|
<phpunit colors="true" cacheDirectory=".phpunit.cache">
|
||||||
verbose="false"
|
<testsuites>
|
||||||
>
|
<testsuite name="unit">
|
||||||
<testsuites>
|
<directory>test/phpUnitTests/</directory>
|
||||||
<testsuite name="unit">
|
</testsuite>
|
||||||
<directory>test/phpUnitTests/</directory>
|
</testsuites>
|
||||||
</testsuite>
|
|
||||||
</testsuites>
|
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
216
src/DotEnv.php
216
src/DotEnv.php
@@ -6,115 +6,115 @@ namespace gullevek\dotEnv;
|
|||||||
|
|
||||||
class DotEnv
|
class DotEnv
|
||||||
{
|
{
|
||||||
/** @var string constant comment char, set to # */
|
/** @var string constant comment char, set to # */
|
||||||
private const COMMENT_CHAR = '#';
|
private const COMMENT_CHAR = '#';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parses .env file
|
* parses .env file
|
||||||
*
|
*
|
||||||
* Rules for .env file
|
* Rules for .env file
|
||||||
* variable is any alphanumeric string followed by = on the same line
|
* variable is any alphanumeric string followed by = on the same line
|
||||||
* content starts with the first non space part
|
* content starts with the first non space part
|
||||||
* strings can be contained in "
|
* strings can be contained in "
|
||||||
* strings MUST be contained in " if they are multiline
|
* strings MUST be contained in " if they are multiline
|
||||||
* if string starts with " it will match until another " is found
|
* if string starts with " it will match until another " is found
|
||||||
* anything AFTER " is ignored
|
* anything AFTER " is ignored
|
||||||
* if there are two variables with the same name only the first is used
|
* if there are two variables with the same name only the first is used
|
||||||
* variables are case sensitive
|
* variables are case sensitive
|
||||||
*
|
*
|
||||||
* [] Grouping Block Name as prefix until next or end if set,
|
* [] Grouping Block Name as prefix until next or end if set,
|
||||||
* space replaced by _, all other var rules apply
|
* space replaced by _, all other var rules apply
|
||||||
*
|
*
|
||||||
* @param string $path Folder to file, default is __DIR__
|
* @param string $path Folder to file, default is __DIR__
|
||||||
* @param string $env_file What file to load, default is .env
|
* @param string $env_file What file to load, default is .env
|
||||||
* @return int -1 other error
|
* @return int -1 other error
|
||||||
* 0 for success full load
|
* 0 for success full load
|
||||||
* 1 for file loadable, no data or data already loaded
|
* 1 for file loadable, no data or data already loaded
|
||||||
* 2 for file not readable or open failed
|
* 2 for file not readable or open failed
|
||||||
* 3 for file not found
|
* 3 for file not found
|
||||||
*/
|
*/
|
||||||
public static function readEnvFile(
|
public static function readEnvFile(
|
||||||
string $path = __DIR__,
|
string $path = __DIR__,
|
||||||
string $env_file = '.env'
|
string $env_file = '.env'
|
||||||
): int {
|
): int {
|
||||||
// default -1;
|
// default -1;
|
||||||
$status = -1;
|
$status = -1;
|
||||||
$env_file_target = $path . DIRECTORY_SEPARATOR . $env_file;
|
$env_file_target = $path . DIRECTORY_SEPARATOR . $env_file;
|
||||||
// this is not a file -> abort
|
// this is not a file -> abort
|
||||||
if (!is_file($env_file_target)) {
|
if (!is_file($env_file_target)) {
|
||||||
$status = 3;
|
$status = 3;
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
// cannot open file -> abort
|
// cannot open file -> abort
|
||||||
if (!is_readable($env_file_target)) {
|
if (!is_readable($env_file_target)) {
|
||||||
$status = 2;
|
$status = 2;
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
// open file
|
// open file
|
||||||
if (($fp = fopen($env_file_target, 'r')) === false) {
|
if (($fp = fopen($env_file_target, 'r')) === false) {
|
||||||
$status = 2;
|
$status = 2;
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
// set to readable but not yet any data loaded
|
// set to readable but not yet any data loaded
|
||||||
$status = 1;
|
$status = 1;
|
||||||
$block = false;
|
$block = false;
|
||||||
$var = '';
|
$var = '';
|
||||||
$prefix_name = '';
|
$prefix_name = '';
|
||||||
while (($line = fgets($fp)) !== false) {
|
while (($line = fgets($fp)) !== false) {
|
||||||
// [] block must be a single line, or it will be ignored
|
// [] block must be a single line, or it will be ignored
|
||||||
if (preg_match("/^\s*\[([\w_.\s]+)\]/", $line, $matches)) {
|
if (preg_match("/^\s*\[([\w_.\s]+)\]/", $line, $matches)) {
|
||||||
$prefix_name = preg_replace("/\s+/", "_", $matches[1]) . ".";
|
$prefix_name = preg_replace("/\s+/", "_", $matches[1]) . ".";
|
||||||
} elseif (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
|
} elseif (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
|
||||||
// main match for variable = value part
|
// main match for variable = value part
|
||||||
$var = $prefix_name . $matches[1];
|
$var = $prefix_name . $matches[1];
|
||||||
$value = $matches[2];
|
$value = $matches[2];
|
||||||
$quotes = $matches[3];
|
$quotes = $matches[3];
|
||||||
// write only if env is not set yet, and write only the first time
|
// write only if env is not set yet, and write only the first time
|
||||||
if (empty($_ENV[$var])) {
|
if (empty($_ENV[$var])) {
|
||||||
if (!empty($quotes)) {
|
if (!empty($quotes)) {
|
||||||
// match greedy for first to last so we move any " if there are
|
// match greedy for first to last so we move any " if there are
|
||||||
if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) {
|
if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) {
|
||||||
$value = $matches[1];
|
$value = $matches[1];
|
||||||
} else {
|
} else {
|
||||||
// this is a multi line
|
// this is a multi line
|
||||||
$block = true;
|
$block = true;
|
||||||
// first " in string remove
|
// first " in string remove
|
||||||
// add removed new line back because this is a multi line
|
// add removed new line back because this is a multi line
|
||||||
$value = ltrim($value, '"') . PHP_EOL;
|
$value = ltrim($value, '"') . PHP_EOL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// strip any quotes at end for unquoted single line
|
// strip any quotes at end for unquoted single line
|
||||||
// an right hand spaces are removed too
|
// an right hand spaces are removed too
|
||||||
$value = false !== ($pos = strpos($value, self::COMMENT_CHAR)) ?
|
$value = false !== ($pos = strpos($value, self::COMMENT_CHAR)) ?
|
||||||
rtrim(substr($value, 0, $pos)) : $value;
|
rtrim(substr($value, 0, $pos)) : $value;
|
||||||
}
|
}
|
||||||
// if block is set, we strip line of slashes
|
// if block is set, we strip line of slashes
|
||||||
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
|
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
|
||||||
// set successful load
|
// set successful load
|
||||||
$status = 0;
|
$status = 0;
|
||||||
}
|
}
|
||||||
} elseif ($block === true) {
|
} elseif ($block === true) {
|
||||||
// read line until there is a unescaped "
|
// read line until there is a unescaped "
|
||||||
// this also strips everything after the last "
|
// this also strips everything after the last "
|
||||||
if (preg_match("/(.*[^\\\])\"/", $line, $matches)) {
|
if (preg_match("/(.*[^\\\])\"/", $line, $matches)) {
|
||||||
$block = false;
|
$block = false;
|
||||||
// strip ending " and EVERYTHING that follows after that
|
// strip ending " and EVERYTHING that follows after that
|
||||||
$line = $matches[1];
|
$line = $matches[1];
|
||||||
}
|
}
|
||||||
// just be sure it is init before we fill
|
// just be sure it is init before we fill
|
||||||
if (!isset($_ENV[$var])) {
|
if (!isset($_ENV[$var])) {
|
||||||
$_ENV[$var] = '';
|
$_ENV[$var] = '';
|
||||||
} elseif (!is_string($_ENV[$var])) {
|
} elseif (!is_string($_ENV[$var])) {
|
||||||
// if this is not string, skip
|
// if this is not string, skip
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// strip line of slashes
|
// strip line of slashes
|
||||||
$_ENV[$var] .= stripslashes($line);
|
$_ENV[$var] .= stripslashes($line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -5,209 +5,214 @@ declare(strict_types=1);
|
|||||||
namespace tests;
|
namespace tests;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
|
use PHPUnit\Framework\Attributes\TestDox;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversMethod;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class for DotEnv
|
* Test class for DotEnv
|
||||||
* @coversDefaultClass \gullevek\DotEnv
|
|
||||||
* @testdox \gullevek\DotEnv method tests
|
|
||||||
*/
|
*/
|
||||||
|
#[TestDox("\gullevek\DotEnv method tests")]
|
||||||
|
#[CoversClass(\gullevek\dotEnv\DotEnv::class)]
|
||||||
|
#[CoversMethod(\gullevek\dotEnv\DotEnv::class, 'readEnvFile')]
|
||||||
final class DotEnvTest extends TestCase
|
final class DotEnvTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* setup the .env files before test run
|
* setup the .env files before test run
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function setUpBeforeClass(): void
|
public static function setUpBeforeClass(): void
|
||||||
{
|
{
|
||||||
// create .env files
|
// create .env files
|
||||||
$file_content = __DIR__ . DIRECTORY_SEPARATOR
|
$file_content = __DIR__ . DIRECTORY_SEPARATOR
|
||||||
. 'dotenv' . DIRECTORY_SEPARATOR
|
. 'dotenv' . DIRECTORY_SEPARATOR
|
||||||
. 'test.env';
|
. 'test.env';
|
||||||
// copy to all folder levels
|
// copy to all folder levels
|
||||||
$env_files = [
|
$env_files = [
|
||||||
__DIR__ . DIRECTORY_SEPARATOR
|
__DIR__ . DIRECTORY_SEPARATOR
|
||||||
. 'dotenv' . DIRECTORY_SEPARATOR
|
. 'dotenv' . DIRECTORY_SEPARATOR
|
||||||
. '.env',
|
. '.env',
|
||||||
__DIR__ . DIRECTORY_SEPARATOR
|
__DIR__ . DIRECTORY_SEPARATOR
|
||||||
. '.env',
|
. '.env',
|
||||||
__DIR__ . DIRECTORY_SEPARATOR
|
__DIR__ . DIRECTORY_SEPARATOR
|
||||||
. '..' . DIRECTORY_SEPARATOR
|
. '..' . DIRECTORY_SEPARATOR
|
||||||
. '.env',
|
. '.env',
|
||||||
];
|
];
|
||||||
// if not found, skip -> all will fail
|
// if not found, skip -> all will fail
|
||||||
if (is_file($file_content)) {
|
if (is_file($file_content)) {
|
||||||
foreach ($env_files as $env_file) {
|
foreach ($env_files as $env_file) {
|
||||||
copy($file_content, $env_file);
|
copy($file_content, $env_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function envFileProvider(): array
|
public static function envFileProvider(): array
|
||||||
{
|
{
|
||||||
$dot_env_content = [
|
$dot_env_content = [
|
||||||
'SOMETHING' => 'A',
|
'SOMETHING' => 'A',
|
||||||
'OTHER' => 'B IS B',
|
'OTHER' => 'B IS B',
|
||||||
'Complex' => 'A B \"D is F',
|
'Complex' => 'A B \"D is F',
|
||||||
'HAS_SPACE' => 'ABC',
|
'HAS_SPACE' => 'ABC',
|
||||||
'HAS_COMMENT_QUOTES_SPACE' => 'Comment at end with quotes and space',
|
'HAS_COMMENT_QUOTES_SPACE' => 'Comment at end with quotes and space',
|
||||||
'HAS_COMMENT_QUOTES_NO_SPACE' => 'Comment at end with quotes no space',
|
'HAS_COMMENT_QUOTES_NO_SPACE' => 'Comment at end with quotes no space',
|
||||||
'HAS_COMMENT_NO_QUOTES_SPACE' => 'Comment at end no quotes and space',
|
'HAS_COMMENT_NO_QUOTES_SPACE' => 'Comment at end no quotes and space',
|
||||||
'HAS_COMMENT_NO_QUOTES_NO_SPACE' => 'Comment at end no quotes no space',
|
'HAS_COMMENT_NO_QUOTES_NO_SPACE' => 'Comment at end no quotes no space',
|
||||||
'COMMENT_IN_TEXT_QUOTES' => 'Foo bar # comment in here',
|
'COMMENT_IN_TEXT_QUOTES' => 'Foo bar # comment in here',
|
||||||
'HAS_EQUAL_NO_QUITES' => 'Is This = Valid',
|
'HAS_EQUAL_NO_QUITES' => 'Is This = Valid',
|
||||||
'HAS_EQUAL_QUITES' => 'Is This = Valid',
|
'HAS_EQUAL_QUITES' => 'Is This = Valid',
|
||||||
'FAILURE' => 'ABC',
|
'FAILURE' => 'ABC',
|
||||||
'SIMPLEBOX' => 'A B C',
|
'SIMPLEBOX' => 'A B C',
|
||||||
'TITLE' => '1',
|
'TITLE' => '1',
|
||||||
'FOO' => '1.2',
|
'FOO' => '1.2',
|
||||||
'SOME.TEST' => 'Test Var',
|
'SOME.TEST' => 'Test Var',
|
||||||
'SOME.LIVE' => 'Live Var',
|
'SOME.LIVE' => 'Live Var',
|
||||||
'A_TEST1' => 'foo',
|
'A_TEST1' => 'foo',
|
||||||
'A_TEST2' => '${TEST1:-bar}',
|
'A_TEST2' => '${TEST1:-bar}',
|
||||||
'A_TEST3' => '${TEST4:-bar}',
|
'A_TEST3' => '${TEST4:-bar}',
|
||||||
'A_TEST5' => 'null',
|
'A_TEST5' => 'null',
|
||||||
'A_TEST6' => '${TEST5-bar}',
|
'A_TEST6' => '${TEST5-bar}',
|
||||||
'A_TEST7' => '${TEST6:-bar}',
|
'A_TEST7' => '${TEST6:-bar}',
|
||||||
'B_TEST1' => 'foo',
|
'B_TEST1' => 'foo',
|
||||||
'B_TEST2' => '${TEST1:=bar}',
|
'B_TEST2' => '${TEST1:=bar}',
|
||||||
'B_TEST3' => '${TEST4:=bar}',
|
'B_TEST3' => '${TEST4:=bar}',
|
||||||
'B_TEST5' => 'null',
|
'B_TEST5' => 'null',
|
||||||
'B_TEST6' => '${TEST5=bar}',
|
'B_TEST6' => '${TEST5=bar}',
|
||||||
'B_TEST7' => '${TEST6=bar}',
|
'B_TEST7' => '${TEST6=bar}',
|
||||||
'Test' => 'A',
|
'Test' => 'A',
|
||||||
'TEST' => 'B',
|
'TEST' => 'B',
|
||||||
'LINE' => "ABC\nDEF",
|
'LINE' => "ABC\nDEF",
|
||||||
'OTHERLINE' => "ABC\nAF\"ASFASDF\nMORESHIT",
|
'OTHERLINE' => "ABC\nAF\"ASFASDF\nMORESHIT",
|
||||||
'SUPERLINE' => '',
|
'SUPERLINE' => '',
|
||||||
'__FOO_BAR_1' => 'b',
|
'__FOO_BAR_1' => 'b',
|
||||||
'__FOOFOO' => 'f ',
|
'__FOOFOO' => 'f ',
|
||||||
123123 => 'number',
|
123123 => 'number',
|
||||||
'EMPTY' => '',
|
'EMPTY' => '',
|
||||||
'Var_Test.TEST' => 'Block 1 D',
|
'Var_Test.TEST' => 'Block 1 D',
|
||||||
'OtherSet.TEST' => 'Block 2 D',
|
'OtherSet.TEST' => 'Block 2 D',
|
||||||
];
|
];
|
||||||
// 0: folder relative to test folder, if unset __DIR__
|
// 0: folder relative to test folder, if unset __DIR__
|
||||||
// 1: file, if unset .env
|
// 1: file, if unset .env
|
||||||
// 2: status to be returned
|
// 2: status to be returned
|
||||||
// 3: _ENV file content to be set
|
// 3: _ENV file content to be set
|
||||||
// 4: override chmod as octect in string
|
// 4: override chmod as octect in string
|
||||||
return [
|
return [
|
||||||
'default' => [
|
'default' => [
|
||||||
'folder' => null,
|
'folder' => null,
|
||||||
'file' => null,
|
'file' => null,
|
||||||
'status' => 3,
|
'expected_status' => 3,
|
||||||
'content' => [],
|
'expected_env' => [],
|
||||||
'chmod' => null,
|
'chmod' => null,
|
||||||
],
|
],
|
||||||
'cannot open file' => [
|
'cannot open file' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
'file' => 'cannot_read.env',
|
'file' => 'cannot_read.env',
|
||||||
'status' => 2,
|
'expected_status' => 2,
|
||||||
'content' => [],
|
'expected_env' => [],
|
||||||
// 0000
|
// 0000
|
||||||
'chmod' => '100000',
|
'chmod' => '100000',
|
||||||
],
|
],
|
||||||
'empty file' => [
|
'empty file' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
'file' => 'empty.env',
|
'file' => 'empty.env',
|
||||||
'status' => 1,
|
'expected_status' => 1,
|
||||||
'content' => [],
|
'expected_env' => [],
|
||||||
// 0664
|
// 0664
|
||||||
'chmod' => '100664',
|
'chmod' => '100664',
|
||||||
],
|
],
|
||||||
'override all' => [
|
'override all' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
'file' => 'test.env',
|
'file' => 'test.env',
|
||||||
'status' => 0,
|
'expected_status' => 0,
|
||||||
'content' => $dot_env_content,
|
'expected_env' => $dot_env_content,
|
||||||
// 0664
|
// 0664
|
||||||
'chmod' => '100664',
|
'chmod' => '100664',
|
||||||
],
|
],
|
||||||
'override directory' => [
|
'override directory' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
'file' => null,
|
'file' => null,
|
||||||
'status' => 0,
|
'expected_status' => 0,
|
||||||
'content' => $dot_env_content,
|
'expected_env' => $dot_env_content,
|
||||||
'chmod' => null,
|
'chmod' => null,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test read .env file
|
* test read .env file
|
||||||
*
|
*
|
||||||
* @covers ::readEnvFile
|
* @param string|null $folder
|
||||||
* @dataProvider envFileProvider
|
* @param string|null $file
|
||||||
* @testdox Read _ENV file from $folder / $file with expected status: $expected_status [$_dataName]
|
* @param int $expected_status
|
||||||
*
|
* @param array $expected_env
|
||||||
* @param string|null $folder
|
* @param string|null $chmod
|
||||||
* @param string|null $file
|
* @return void
|
||||||
* @param int $expected_status
|
*/
|
||||||
* @param array $expected_env
|
#[Test]
|
||||||
* @param string|null $chmod
|
#[TestDox('Read _ENV file from $folder / $file with expected status: $expected_status [$_dataName]')]
|
||||||
* @return void
|
#[DataProvider('envFileProvider')]
|
||||||
*/
|
public function testReadEnvFile(
|
||||||
public function testReadEnvFile(
|
?string $folder,
|
||||||
?string $folder,
|
?string $file,
|
||||||
?string $file,
|
int $expected_status,
|
||||||
int $expected_status,
|
array $expected_env,
|
||||||
array $expected_env,
|
?string $chmod
|
||||||
?string $chmod
|
): void {
|
||||||
): void {
|
// skip if chmod is set to 10000 (000 no rights) if we are root
|
||||||
// skip if chmod is set to 10000 (000 no rights) if we are root
|
// as root there is no stop reading a file
|
||||||
// as root there is no stop reading a file
|
if (
|
||||||
if (
|
!empty($chmod) &&
|
||||||
!empty($chmod) &&
|
$chmod == '100000' &&
|
||||||
$chmod == '100000' &&
|
getmyuid() == 0
|
||||||
getmyuid() == 0
|
) {
|
||||||
) {
|
$this->markTestSkipped(
|
||||||
$this->markTestSkipped(
|
"Skip cannot read file test because run user is root"
|
||||||
"Skip cannot read file test because run user is root"
|
);
|
||||||
);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
// reset $_ENV for clean compare
|
||||||
// reset $_ENV for clean compare
|
$_ENV = [];
|
||||||
$_ENV = [];
|
// previous file perm
|
||||||
// previous file perm
|
$old_chmod = null;
|
||||||
$old_chmod = null;
|
// if we have change permission for file
|
||||||
// if we have change permission for file
|
if (
|
||||||
if (
|
is_file($folder . DIRECTORY_SEPARATOR . $file) &&
|
||||||
is_file($folder . DIRECTORY_SEPARATOR . $file) &&
|
!empty($chmod)
|
||||||
!empty($chmod)
|
) {
|
||||||
) {
|
// get the old permissions
|
||||||
// get the old permissions
|
$old_chmod = fileperms($folder . DIRECTORY_SEPARATOR . $file);
|
||||||
$old_chmod = fileperms($folder . DIRECTORY_SEPARATOR . $file);
|
chmod($folder . DIRECTORY_SEPARATOR . $file, octdec($chmod));
|
||||||
chmod($folder . DIRECTORY_SEPARATOR . $file, octdec($chmod));
|
}
|
||||||
}
|
if ($folder !== null && $file !== null) {
|
||||||
if ($folder !== null && $file !== null) {
|
$status = \gullevek\dotEnv\DotEnv::readEnvFile($folder, $file);
|
||||||
$status = \gullevek\dotEnv\DotEnv::readEnvFile($folder, $file);
|
} elseif ($folder !== null) {
|
||||||
} elseif ($folder !== null) {
|
$status = \gullevek\dotEnv\DotEnv::readEnvFile($folder);
|
||||||
$status = \gullevek\dotEnv\DotEnv::readEnvFile($folder);
|
} else {
|
||||||
} else {
|
$status = \gullevek\dotEnv\DotEnv::readEnvFile();
|
||||||
$status = \gullevek\dotEnv\DotEnv::readEnvFile();
|
}
|
||||||
}
|
$this->assertEquals(
|
||||||
$this->assertEquals(
|
$expected_status,
|
||||||
$status,
|
$status,
|
||||||
$expected_status,
|
'Assert returned status equal'
|
||||||
'Assert returned status equal'
|
);
|
||||||
);
|
// now assert read data
|
||||||
// now assert read data
|
$this->assertEquals(
|
||||||
$this->assertEquals(
|
$expected_env,
|
||||||
$_ENV,
|
$_ENV,
|
||||||
$expected_env,
|
'Assert _ENV correct'
|
||||||
'Assert _ENV correct'
|
);
|
||||||
);
|
// if we have file and chmod unset
|
||||||
// if we have file and chmod unset
|
if ($old_chmod !== null) {
|
||||||
if ($old_chmod !== null) {
|
chmod($folder . DIRECTORY_SEPARATOR . $file, $old_chmod);
|
||||||
chmod($folder . DIRECTORY_SEPARATOR . $file, $old_chmod);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -8,18 +8,18 @@ use gullevek\dotEnv\DotEnv;
|
|||||||
|
|
||||||
// copy test file to .env file in env folder
|
// copy test file to .env file in env folder
|
||||||
$file_content = __DIR__ . DIRECTORY_SEPARATOR
|
$file_content = __DIR__ . DIRECTORY_SEPARATOR
|
||||||
. 'phpUnitTests' . DIRECTORY_SEPARATOR
|
. 'phpUnitTests' . DIRECTORY_SEPARATOR
|
||||||
. 'dotenv' . DIRECTORY_SEPARATOR
|
. 'dotenv' . DIRECTORY_SEPARATOR
|
||||||
. 'test.env';
|
. 'test.env';
|
||||||
// env folder
|
// env folder
|
||||||
$env_file = __DIR__ . DIRECTORY_SEPARATOR
|
$env_file = __DIR__ . DIRECTORY_SEPARATOR
|
||||||
. 'env' . DIRECTORY_SEPARATOR
|
. 'env' . DIRECTORY_SEPARATOR
|
||||||
. '.env';
|
. '.env';
|
||||||
if (!is_file($file_content)) {
|
if (!is_file($file_content)) {
|
||||||
die("Cannot read $file_content");
|
die("Cannot read $file_content");
|
||||||
}
|
}
|
||||||
if (copy($file_content, $env_file) === false) {
|
if (copy($file_content, $env_file) === false) {
|
||||||
die("Cannot copy $file_content to $env_file");
|
die("Cannot copy $file_content to $env_file");
|
||||||
}
|
}
|
||||||
|
|
||||||
print "BASE: " . __DIR__ . "<br>";
|
print "BASE: " . __DIR__ . "<br>";
|
||||||
|
|||||||
Reference in New Issue
Block a user