Update core git config files, vendor installs for unit testing

Update .gitattributes with more files to ignore
Add .gitignore for php unit test cache
Install dotenv file parser for unit testing
Update current test pages with dotenv file parser (replaces function
call)

Documentation PHPdoc update for checkMe method call
This commit is contained in:
2022-06-09 09:25:08 +09:00
parent 1e836e9a2b
commit a51f4df53e
27 changed files with 672 additions and 96 deletions

3
.gitattributes vendored
View File

@@ -1,5 +1,6 @@
test/ export-ignore test/ export-ignore
.gitattributes export-ignore
phpstan.neon export-ignore phpstan.neon export-ignore
phpunit.xml export-ignore
psalm.xml export-ignore psalm.xml export-ignore
.phan/ export-ignore .phan/ export-ignore
.* export-ignore

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.phpunit.result.cache

View File

@@ -25,6 +25,7 @@
"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" "phpunit/phpunit": "^9",
"gullevek/dotenv": "dev-master"
} }
} }

58
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "575af90859cb670cec82630ae7426112", "content-hash": "412ca7fe7d62707c222169f4a7deeaec",
"packages": [], "packages": [],
"packages-dev": [ "packages-dev": [
{ {
@@ -77,6 +77,58 @@
], ],
"time": "2022-03-03T08:28:38+00:00" "time": "2022-03-03T08:28:38+00:00"
}, },
{
"name": "gullevek/dotenv",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/gullevek/dotEnv.git",
"reference": "2c7931b7a57f992f9010fc7d9c58af8951f1dc24"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/gullevek/dotEnv/zipball/2c7931b7a57f992f9010fc7d9c58af8951f1dc24",
"reference": "2c7931b7a57f992f9010fc7d9c58af8951f1dc24",
"shasum": ""
},
"require": {
"php": ">=7.4.0"
},
"require-dev": {
"phpunit/phpunit": "^9"
},
"default-branch": true,
"type": "library",
"autoload": {
"psr-4": {
"gullevek\\dotEnv\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Clemens Schwaighofer",
"email": "gullevek@gullevek.org",
"homepage": "http://gullevek.org"
}
],
"description": "Simple .env file processing and storing in _ENV array",
"homepage": "https://github.com/gullevek/dotEnv",
"keywords": [
".env",
"_ENV",
"dotenv",
"environment variables"
],
"support": {
"issues": "https://github.com/gullevek/dotEnv/issues",
"source": "https://github.com/gullevek/dotEnv/tree/v2.0.1"
},
"time": "2022-06-08T05:12:35+00:00"
},
{ {
"name": "myclabs/deep-copy", "name": "myclabs/deep-copy",
"version": "1.x-dev", "version": "1.x-dev",
@@ -2037,7 +2089,9 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "dev", "minimum-stability": "dev",
"stability-flags": [], "stability-flags": {
"gullevek/dotenv": 20
},
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {

View File

@@ -7,7 +7,7 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
> >
<projectFiles> <projectFiles>
<directory name="src" /> <directory name="." />
<ignoreFiles> <ignoreFiles>
<directory name="vendor" /> <directory name="vendor" />
<directory name="test" /> <directory name="test" />

View File

@@ -137,6 +137,10 @@ final class AmazonIncentives
// ********************************************************************* // *********************************************************************
/** /**
* Prints out ENV, CONFIG and KEY data
* This is for debug only, this will print out secrets.
* Use with care
*
* @return array<mixed> * @return array<mixed>
*/ */
public function checkMe(): array public function checkMe(): array

View File

@@ -57,13 +57,12 @@ $loader = require '../vendor/autoload.php';
// need to add this or it will not load here // need to add this or it will not load here
$loader->addPsr4('gullevek\\', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src'); $loader->addPsr4('gullevek\\', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src');
// print "LOADER: <pre>" . print_r($loader, true) . "</pre>"; // print "LOADER: <pre>" . print_r($loader, true) . "</pre>";
// env file loader (simple)
require 'read_env_file.php';
use gullevek\AmazonIncentives\AmazonIncentives; use gullevek\AmazonIncentives\AmazonIncentives;
use gullevek\dotEnv\DotEnv;
// load env data with dotenv // load env data with dotenv
__readEnvFile(__DIR__); DotEnv::readEnvFile(__DIR__);
print "<h1>Amazon Gift Card Incentives</h1><br>"; print "<h1>Amazon Gift Card Incentives</h1><br>";
@@ -102,8 +101,9 @@ $mock_wait = 2;
if ($run_info_test === true) { if ($run_info_test === true) {
$aws = new AmazonIncentives(); $aws = new AmazonIncentives();
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>"; $aws_check_me = $aws->checkMe();
fwrite($fp, writeLog($aws->checkMe())); print "checkMe: <pre>" . print_r($aws_check_me, true) . "</pre>";
fwrite($fp, writeLog($aws_check_me));
print "<hr>"; print "<hr>";
} }

View File

@@ -0,0 +1,23 @@
<?php
// just print out env data nd connect data
// checkMe from AmazonIntentives call is requal to
// run_info_test === true in aws_gift_card_tests.php
$loader = require '../vendor/autoload.php';
// need to add this or it will not load here
$loader->addPsr4('gullevek\\', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src');
// print "LOADER: <pre>" . print_r($loader, true) . "</pre>";
use gullevek\AmazonIncentives\AmazonIncentives;
use gullevek\dotEnv\DotEnv;
// load env data with dotenv
DotEnv::readEnvFile(__DIR__);
print "_ENV: <pre>" . print_r($_ENV, true) . "</pre>";
$aws = new AmazonIncentives();
print "checkMe: <pre>" . print_r($aws->checkMe(), true) . "</pre>";
// __END__

View File

@@ -1,84 +0,0 @@
<?php
/**
* parses .env file
*
* Rules for .env file
* variable is any alphanumeric string followed by = on the same line
* content starts with the first non space part
* strings can be contained in "
* strings MUST be contained in " if they are multiline
* if string starts with " it will match until another " is found
* anything AFTER " is ignored
* if there are two variables with the same name only the first is used
* variables are case sensitive
*
* @param string $path Folder to file, default is __DIR__
* @param string $env_file What file to load, default is .env
* @return int -1 other error
* 0 for success full load
* 1 for file loadable, but no data inside
* 2 for file not readable
* 3 for file not found
*/
function __readEnvFile(string $path = __DIR__, string $env_file = '.env'): int
{
// default -1;
$status = -1;
$env_file_target = $path . DIRECTORY_SEPARATOR . $env_file;
// this is not a file -> abort
if (!is_file($env_file_target)) {
$status = 3;
return $status;
}
// cannot open file -> abort
if (($fp = fopen($env_file_target, 'r')) === false) {
$status = 2;
return $status;
}
// set to readable but not yet any data loaded
$status = 1;
$block = false;
$var = '';
while ($line = fgets($fp)) {
// main match for variable = value part
if (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
$var = $matches[1];
$value = $matches[2];
$quotes = $matches[3];
// wirte only if env is not set yet, and write only the first time
if (empty($_ENV[$var])) {
if (!empty($quotes)) {
// match greedy for first to last so we move any " if there are
if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) {
$value = $matches[1];
} else {
// this is a multi line
$block = true;
// first " in string remove
// add removed new line back because this is a multi line
$value = ltrim($value, '"') . PHP_EOL;
}
}
// if block is set, we strip line of slashes
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
// set successful load
$status = 0;
}
} elseif ($block === true) {
// read line until there is a unescaped "
// this also strips everything after the last "
if (preg_match("/(.*[^\\\])\"/", $line, $matches)) {
$block = false;
// strip ending " and EVERYTHING that follows after that
$line = $matches[1];
}
// strip line of slashes
$_ENV[$var] .= stripslashes($line);
}
}
fclose($fp);
return $status;
}
// __END__

View File

@@ -7,6 +7,7 @@ $baseDir = dirname($vendorDir);
return array( return array(
'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/reflection-docblock/src', $vendorDir . '/phpdocumentor/type-resolver/src'), 'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/reflection-docblock/src', $vendorDir . '/phpdocumentor/type-resolver/src'),
'gullevek\\dotEnv\\' => array($vendorDir . '/gullevek/dotenv/src'),
'gullevek\\AmazonIncentives\\' => array($baseDir . '/src'), 'gullevek\\AmazonIncentives\\' => array($baseDir . '/src'),
'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'), 'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'),
'Prophecy\\' => array($vendorDir . '/phpspec/prophecy/src/Prophecy'), 'Prophecy\\' => array($vendorDir . '/phpspec/prophecy/src/Prophecy'),

View File

@@ -18,6 +18,7 @@ class ComposerStaticInit0c8f6bec90a6d60040a922f19a1f0e64
), ),
'g' => 'g' =>
array ( array (
'gullevek\\dotEnv\\' => 16,
'gullevek\\AmazonIncentives\\' => 26, 'gullevek\\AmazonIncentives\\' => 26,
), ),
'W' => 'W' =>
@@ -43,6 +44,10 @@ class ComposerStaticInit0c8f6bec90a6d60040a922f19a1f0e64
1 => __DIR__ . '/..' . '/phpdocumentor/reflection-docblock/src', 1 => __DIR__ . '/..' . '/phpdocumentor/reflection-docblock/src',
2 => __DIR__ . '/..' . '/phpdocumentor/type-resolver/src', 2 => __DIR__ . '/..' . '/phpdocumentor/type-resolver/src',
), ),
'gullevek\\dotEnv\\' =>
array (
0 => __DIR__ . '/..' . '/gullevek/dotenv/src',
),
'gullevek\\AmazonIncentives\\' => 'gullevek\\AmazonIncentives\\' =>
array ( array (
0 => __DIR__ . '/../..' . '/src', 0 => __DIR__ . '/../..' . '/src',

View File

@@ -73,6 +73,61 @@
], ],
"install-path": "../doctrine/instantiator" "install-path": "../doctrine/instantiator"
}, },
{
"name": "gullevek/dotenv",
"version": "dev-master",
"version_normalized": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/gullevek/dotEnv.git",
"reference": "2c7931b7a57f992f9010fc7d9c58af8951f1dc24"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/gullevek/dotEnv/zipball/2c7931b7a57f992f9010fc7d9c58af8951f1dc24",
"reference": "2c7931b7a57f992f9010fc7d9c58af8951f1dc24",
"shasum": ""
},
"require": {
"php": ">=7.4.0"
},
"require-dev": {
"phpunit/phpunit": "^9"
},
"time": "2022-06-08T05:12:35+00:00",
"default-branch": true,
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"gullevek\\dotEnv\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Clemens Schwaighofer",
"email": "gullevek@gullevek.org",
"homepage": "http://gullevek.org"
}
],
"description": "Simple .env file processing and storing in _ENV array",
"homepage": "https://github.com/gullevek/dotEnv",
"keywords": [
".env",
"_ENV",
"dotenv",
"environment variables"
],
"support": {
"issues": "https://github.com/gullevek/dotEnv/issues",
"source": "https://github.com/gullevek/dotEnv/tree/v2.0.1"
},
"install-path": "../gullevek/dotenv"
},
{ {
"name": "myclabs/deep-copy", "name": "myclabs/deep-copy",
"version": "1.x-dev", "version": "1.x-dev",
@@ -2130,6 +2185,7 @@
"dev": true, "dev": true,
"dev-package-names": [ "dev-package-names": [
"doctrine/instantiator", "doctrine/instantiator",
"gullevek/dotenv",
"myclabs/deep-copy", "myclabs/deep-copy",
"nikic/php-parser", "nikic/php-parser",
"phar-io/manifest", "phar-io/manifest",

View File

@@ -5,7 +5,7 @@
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'reference' => '5b99da616b048dd9f0d9bd0672992d27b882a36c', 'reference' => '1e836e9a2bd6e41c4555cf917fe645b453cb5b79',
'name' => 'gullevek/amazon-incentives', 'name' => 'gullevek/amazon-incentives',
'dev' => true, 'dev' => true,
), ),
@@ -25,9 +25,20 @@
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'reference' => '5b99da616b048dd9f0d9bd0672992d27b882a36c', 'reference' => '1e836e9a2bd6e41c4555cf917fe645b453cb5b79',
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'gullevek/dotenv' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'type' => 'library',
'install_path' => __DIR__ . '/../gullevek/dotenv',
'aliases' => array(
0 => '9999999-dev',
),
'reference' => '2c7931b7a57f992f9010fc7d9c58af8951f1dc24',
'dev_requirement' => true,
),
'myclabs/deep-copy' => array( 'myclabs/deep-copy' => array(
'pretty_version' => '1.x-dev', 'pretty_version' => '1.x-dev',
'version' => '1.9999999.9999999.9999999-dev', 'version' => '1.9999999.9999999.9999999-dev',

3
vendor/gullevek/dotenv/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
vendor
.phpunit.result.cache
composer.lock

97
vendor/gullevek/dotenv/.phan/config.php vendored Normal file
View File

@@ -0,0 +1,97 @@
<?php
/**
* This configuration will be read and overlaid on top of the
* default configuration. Command line arguments will be applied
* after this file is read.
*
* @see src/Phan/Config.php
* See Config for all configurable options.
*
* A Note About Paths
* ==================
*
* Files referenced from this file should be defined as
*
* ```
* Config::projectPath('relative_path/to/file')
* ```
*
* where the relative path is relative to the root of the
* project which is defined as either the working directory
* of the phan executable or a path passed in via the CLI
* '-d' flag.
*/
// use Phan\Config;
return [
// If true, missing properties will be created when
// they are first seen. If false, we'll report an
// error message.
"allow_missing_properties" => false,
// Allow null to be cast as any type and for any
// type to be cast to null.
"null_casts_as_any_type" => false,
// Backwards Compatibility Checking
'backward_compatibility_checks' => true,
// Run a quick version of checks that takes less
// time
"quick_mode" => false,
// Only emit critical issues to start with
// (0 is low severity, 5 is normal severity, 10 is critical)
"minimum_severity" => 10,
// default false for include path check
"enable_include_path_checks" => true,
"include_paths" => [
],
'ignore_undeclared_variables_in_global_scope' => true,
"file_list" => [
],
// A list of directories that should be parsed for class and
// method information. After excluding the directories
// defined in exclude_analysis_directory_list, the remaining
// files will be statically analyzed for errors.
//
// Thus, both first-party and third-party code being used by
// your application should be included in this list.
'directory_list' => [
// Change this to include the folders you wish to analyze
// (and the folders of their dependencies)
'.'
// 'www',
// To speed up analysis, we recommend going back later and
// limiting this to only the vendor/ subdirectories your
// project depends on.
// `phan --init` will generate a list of folders for you
],
// A list of directories holding code that we want
// to parse, but not analyze
"exclude_analysis_directory_list" => [
'vendor',
'test'
],
'exclude_file_list' => [
],
// what not to show as problem
'suppress_issue_types' => [
// 'PhanUndeclaredMethod',
'PhanEmptyFile',
],
// Override to hardcode existence and types of (non-builtin) globals in the global scope.
// Class names should be prefixed with `\`.
//
// (E.g. `['_FOO' => '\FooClass', 'page' => '\PageClass', 'userId' => 'int']`)
'globals_type_map' => [],
];

50
vendor/gullevek/dotenv/Readme.md vendored Normal file
View File

@@ -0,0 +1,50 @@
# dotenv: readEnvFile()
A simple implementation of <https://github.com/vlucas/phpdotenv>
This is not a functional replacement, but a very simple implementation of the basic functions.
It is recommended to create a `.env.example` example file that is checked into the
repository. The `.env` should *NEVER* be checked into anything
## How to install
`comoser require gullevek/dotEnv`
## How it works
Put the function where it is needed or put it in a file and load it.
if not parameter is given it will use `__DIR__` as base path.
Second parameter is file name override. Default is `.env`
Data is loaded into _ENV only.
If there is already an entry in _ENV then it will not be overwritten.
## .env file example
A valid entry has to start with an alphanumeric string, underscores are allowed and
then have an equal sign (=). After the equal sign the data block starts. Data can be
quoted with double quotes (") and if this is done can stretch over multiple lines.
The openeing double quote must be on the same lign as the requal sign (=). If double
quoted (") charcters are used it will read each line until another double quote (")
character is found. Everything after that is ignored.
Any spaces before the variable or before and after the equal sign (=) are ignored.
Line is read until `PHP_EOL`. So any trailing spaces are read too.
Any line that is not valid is ignored.
```ini
# this line is ignored
SOMETHING=A
OTHER="A B C"
MULTI_LINE="1 2 3
4 5 6
7 8 9" ; and this is ignored
ESCAPE="String \" inside \" other "
DOUBLE="I will be used"
DOUBLE="This will be ignored"
```

30
vendor/gullevek/dotenv/composer.json vendored Normal file
View File

@@ -0,0 +1,30 @@
{
"name": "gullevek/dotenv",
"description": "Simple .env file processing and storing in _ENV array",
"keywords": [".env", "dotenv", "_ENV", "environment variables"],
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"gullevek\\dotEnv\\": "src/"
}
},
"authors": [
{
"name": "Clemens Schwaighofer",
"email": "gullevek@gullevek.org",
"homepage": "http://gullevek.org"
}
],
"homepage": "https://github.com/gullevek/dotEnv",
"minimum-stability": "dev",
"require": {
"php": ">=7.4.0"
},
"archive": {
"exclude": ["/test/", "/test/*", "/phpstan.neon", "/psalm.xml", "/.phan/", "/.vscode/", "/phpunit.xml"]
},
"require-dev": {
"phpunit/phpunit": "^9"
}
}

10
vendor/gullevek/dotenv/phpstan.neon vendored Normal file
View File

@@ -0,0 +1,10 @@
# PHP Stan Config
parameters:
tmpDir: /tmp/phpstan-codeblocks-dotenv
level: max
paths:
- %currentWorkingDirectory%
excludePaths:
- vendor
- test

5
vendor/gullevek/dotenv/phpunit.xml vendored Normal file
View File

@@ -0,0 +1,5 @@
<phpunit
colors="true"
verbose="true"
>
</phpunit>

16
vendor/gullevek/dotenv/psalm.xml vendored Normal file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="8"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="test" />
</ignoreFiles>
</projectFiles>
</psalm>

99
vendor/gullevek/dotenv/src/DotEnv.php vendored Normal file
View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace gullevek\dotEnv;
class DotEnv
{
/**
* parses .env file
*
* Rules for .env file
* variable is any alphanumeric string followed by = on the same line
* content starts with the first non space part
* strings can be contained in "
* strings MUST be contained in " if they are multiline
* if string starts with " it will match until another " is found
* anything AFTER " is ignored
* if there are two variables with the same name only the first is used
* variables are case sensitive
*
* @param string $path Folder to file, default is __DIR__
* @param string $env_file What file to load, default is .env
* @return int -1 other error
* 0 for success full load
* 1 for file loadable, but no data inside
* 2 for file not readable or open failed
* 3 for file not found
*/
public static function readEnvFile(
string $path = __DIR__,
string $env_file = '.env'
): int {
// default -1;
$status = -1;
$env_file_target = $path . DIRECTORY_SEPARATOR . $env_file;
// this is not a file -> abort
if (!is_file($env_file_target)) {
$status = 3;
return $status;
}
// cannot open file -> abort
if (!is_readable($env_file_target)) {
$status = 2;
return $status;
}
// open file
if (($fp = fopen($env_file_target, 'r')) === false) {
$status = 2;
return $status;
}
// set to readable but not yet any data loaded
$status = 1;
$block = false;
$var = '';
while ($line = fgets($fp)) {
// main match for variable = value part
if (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
$var = $matches[1];
$value = $matches[2];
$quotes = $matches[3];
// write only if env is not set yet, and write only the first time
if (empty($_ENV[$var])) {
if (!empty($quotes)) {
// match greedy for first to last so we move any " if there are
if (preg_match('/^"(.*[^\\\])"/U', $value, $matches)) {
$value = $matches[1];
} else {
// this is a multi line
$block = true;
// first " in string remove
// add removed new line back because this is a multi line
$value = ltrim($value, '"') . PHP_EOL;
}
}
// if block is set, we strip line of slashes
$_ENV[$var] = $block === true ? stripslashes($value) : $value;
// set successful load
$status = 0;
}
} elseif ($block === true) {
// read line until there is a unescaped "
// this also strips everything after the last "
if (preg_match("/(.*[^\\\])\"/", $line, $matches)) {
$block = false;
// strip ending " and EVERYTHING that follows after that
$line = $matches[1];
}
// strip line of slashes
$_ENV[$var] .= stripslashes($line);
}
}
fclose($fp);
return $status;
}
}
// __END__

View File

@@ -0,0 +1,4 @@
# example enviroment file
SOMETHING=
OTHER=
Complex=

View File

@@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
/**
* Test class for ACL\Login
* @coversDefaultClass \gullevek\DotEnv
* @testdox \gullevek\DotEnv method tests
*/
final class DotEnvTest extends TestCase
{
/**
* Undocumented function
*
* @return array
*/
public function envFileProvider(): array
{
$dot_env_content = [
'SOMETHING' => 'A',
'OTHER' => 'B IS B',
'Complex' => 'A B \"D is F',
'HAS_SPACE' => 'ABC',
'FAILURE' => 'ABC',
'SIMPLEBOX' => 'A B C',
'TITLE' => '1',
'FOO' => '1.2',
'SOME.TEST' => 'Test Var',
'SOME.LIVE' => 'Live Var',
'Test' => 'A',
'TEST' => 'B',
'LINE' => "ABC\nDEF",
'OTHERLINE' => "ABC\nAF\"ASFASDF\nMORESHIT",
'SUPERLINE' => '',
'__FOO_BAR_1' => 'b',
'__FOOFOO' => 'f ',
123123 => 'number',
'EMPTY' => '',
];
// 0: folder relative to test folder, if unset __DIR__
// 1: file, if unset .env
// 2: status to be returned
// 3: _ENV file content to be set
// 4: override chmod as octect in string
return [
'default' => [
'folder' => null,
'file' => null,
'status' => 3,
'content' => [],
'chmod' => null,
],
'cannot open file' => [
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
'file' => 'cannot_read.env',
'status' => 2,
'content' => [],
'chmod' => '000',
],
'empty file' => [
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
'file' => 'empty.env',
'status' => 1,
'content' => [],
'chmod' => null,
],
'override all' => [
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
'file' => 'test.env',
'status' => 0,
'content' => $dot_env_content,
'chmod' => null,
],
'override directory' => [
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
'file' => null,
'status' => 0,
'content' => $dot_env_content,
'chmod' => null,
],
];
}
/**
* test read .env file
*
* @covers ::readEnvFile
* @dataProvider envFileProvider
* @testdox Read _ENV file from $folder / $file with expected status: $expected_status [$_dataName]
*
* @param string|null $folder
* @param string|null $file
* @param int $expected_status
* @param array $expected_env
* @param string|null $chmod
* @return void
*/
public function testReadEnvFile(
?string $folder,
?string $file,
int $expected_status,
array $expected_env,
?string $chmod
): void {
// if we have file + chmod set
$old_chmod = null;
if (
is_file($folder . DIRECTORY_SEPARATOR . $file) &&
!empty($chmod)
) {
// get the old permissions
$old_chmod = fileperms($folder . DIRECTORY_SEPARATOR . $file);
chmod($folder . DIRECTORY_SEPARATOR . $file, octdec($chmod));
}
if ($folder !== null && $file !== null) {
$status = \gullevek\dotEnv\DotEnv::readEnvFile($folder, $file);
} elseif ($folder !== null) {
$status = \gullevek\dotEnv\DotEnv::readEnvFile($folder);
} else {
$status = \gullevek\dotEnv\DotEnv::readEnvFile();
}
$this->assertEquals(
$status,
$expected_status,
'Assert returned status equal'
);
// now assert read data
$this->assertEquals(
$_ENV,
$expected_env,
'Assert _ENV correct'
);
// if we have file and chmod unset
if ($old_chmod !== null) {
chmod($folder . DIRECTORY_SEPARATOR . $file, $old_chmod);
}
}
}
// __END__

View File

@@ -0,0 +1,28 @@
# enviroment file
SOMETHING=A
OTHER="B IS B"
Complex="A B \"D is F"
# COMMENT
HAS_SPACE= "ABC";
FAILURE = ABC
SIMPLEBOX= A B C
TITLE=1
FOO=1.2
SOME.TEST=Test Var
SOME.LIVE=Live Var
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

@@ -0,0 +1,17 @@
<?php
// composer auto loader
$loader = require '../vendor/autoload.php';
// need to add this or it will not load here
$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>";
$status = DotEnv::readEnvFile(__DIR__);
print "STATUS: " . (string)$status . "<br>";
print "ENV: <pre>" . print_r($_ENV, true) . "</pre><br>";
// __END__