Auto create .env files

This commit is contained in:
2023-01-11 16:30:51 +09:00
parent 3668e6552c
commit dd2274f3b1
6 changed files with 101 additions and 5 deletions

View File

@@ -60,3 +60,9 @@ ESCAPE="String \" inside \" other "
DOUBLE="I will be used"
DOUBLE="This will be ignored"
```
## Development
Unit tests have to be run from base folder with
`vendor/bin/phpunit test/phpUnitTests/`

View File

@@ -1 +0,0 @@
phpUnitTests/dotenv/test.env

0
test/env/.gitignore vendored Normal file
View File

View File

@@ -13,6 +13,32 @@ use PHPUnit\Framework\TestCase;
*/
final class DotEnvTest extends TestCase
{
/**
* setup the .env files before test run
*
* @return void
*/
protected function setUp(): void
{
// create .env files
$file_content = __DIR__ . DIRECTORY_SEPARATOR
. 'dotenv' . DIRECTORY_SEPARATOR
. 'test.env';
$env_files = [
__DIR__ . DIRECTORY_SEPARATOR
. 'dotenv' . DIRECTORY_SEPARATOR
. '.env',
__DIR__ . DIRECTORY_SEPARATOR
. '.env',
];
// if not found, skip -> all will fail
if (is_file($file_content)) {
foreach ($env_files as $env_file) {
copy($file_content, $env_file);
}
}
}
/**
* Undocumented function
*

View File

@@ -1 +0,0 @@
test.env

View File

@@ -0,0 +1,49 @@
# enviroment file
SOMETHING=A
OTHER="B IS B"
Complex="A B \"D is F"
# COMMENT
HAS_SPACE= "ABC";
# COMMENT AT END
HAS_COMMENT_QUOTES_SPACE="Comment at end with quotes and space" # Comment QE
HAS_COMMENT_QUOTES_NO_SPACE="Comment at end with quotes no space"# Comment QES
HAS_COMMENT_NO_QUOTES_SPACE=Comment at end no quotes and space # Comment NQE
HAS_COMMENT_NO_QUOTES_NO_SPACE=Comment at end no quotes no space# Comment NQES
COMMENT_IN_TEXT_QUOTES="Foo bar # comment in here"
FAILURE = ABC
SIMPLEBOX= A B C
TITLE=1
FOO=1.2
SOME.TEST=Test Var
SOME.LIVE=Live Var
# VAR TESTS -
A_TEST1 = foo
A_TEST2 = ${TEST1:-bar} # TEST1 is set so the value of TEST2 = foo
A_TEST3 = ${TEST4:-bar} # TEST4 is not set so the value of TEST3 = bar
A_TEST5 = null
A_TEST6 = ${TEST5-bar} # TEST5 is set but empty so the value of TEST6 = null
A_TEST7 = ${TEST6:-bar} # TEST5 is set and empty so the value of TEST7 = bar
# VAR TESTS =
B_TEST1 = foo
B_TEST2 = ${TEST1:=bar} # TEST1 is set so the value of TEST2 = foo
B_TEST3 = ${TEST4:=bar} # TEST4 is not set so the value of TEST3 = bar and TEST4 = bar
B_TEST5 = null
B_TEST6 = ${TEST5=bar} # TEST5 is set but emtpy so the value of TEST6 = null
B_TEST7 = ${TEST6=bar} # TEST5 is set and empty so the value of TEST7 = bar and TEST5 = bar
# VAR TEST END
Test="A"
TEST="B"
TEST="D"
LINE="ABC
DEF"
OTHERLINE="ABC
AF\"ASFASDF
MORESHIT"
SUPERLINE=
"asfasfasf"
__FOO_BAR_1 = b
__FOOFOO = f
123123=number
EMPTY=
= flase
asfasdf

View File

@@ -6,10 +6,27 @@ $loader = require '../vendor/autoload.php';
$loader->addPsr4('gullevek\\', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src');
use gullevek\dotEnv\DotEnv;
print "BASE: " . __DIR__ . "<br>";
print "ORIG: <pre>" . file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '.env') . "</pre>";
// copy test file to .env file in env folder
$file_content = __DIR__ . DIRECTORY_SEPARATOR
. 'phpUnitTests' . DIRECTORY_SEPARATOR
. 'dotenv' . DIRECTORY_SEPARATOR
. 'test.env';
// env folder
$env_file = __DIR__ . DIRECTORY_SEPARATOR
. 'env' . DIRECTORY_SEPARATOR
. '.env';
if (!is_file($file_content)) {
die("Cannot read $file_content");
}
if (copy($file_content, $env_file) === false) {
die("Cannot copy $file_content to $env_file");
}
$status = DotEnv::readEnvFile(__DIR__);
print "BASE: " . __DIR__ . "<br>";
print "ENV: " . $env_file . "<br>";
print "ORIG: <pre>" . file_get_contents($env_file) . "</pre>";
$status = DotEnv::readEnvFile(__DIR__ . DIRECTORY_SEPARATOR . 'env');
print "STATUS: " . (string)$status . "<br>";
print "ENV: <pre>" . print_r($_ENV, true) . "</pre><br>";