Skip to content

Commit e98ebb8

Browse files
committed
Added env parser tester, fixtures and load function test
1 parent 3cc2150 commit e98ebb8

File tree

6 files changed

+198
-6
lines changed

6 files changed

+198
-6
lines changed

Tests/EnvparserTest.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,68 @@
1010
*/
1111
class EnvparserTest extends \PHPUnit_Framework_TestCase
1212
{
13-
13+
public $tmpPathEnv;
14+
public $instance;
15+
16+
const DS = DIRECTORY_SEPARATOR;
17+
18+
protected function setUp()
19+
{
20+
$this->tmpPathEnv = sys_get_temp_dir().self::DS.'EvnmanagerTestEnv.env';
21+
22+
if (!touch($this->tmpPathEnv)) {
23+
throw new \RuntimeException(sprintf('A test failed. Could not write a file to "%s" path', $this->tmpPathEnv));
24+
}
25+
26+
$tmpData = <<<EOF
27+
FOO=bar
28+
EOF;
29+
30+
file_put_contents($this->tmpPathEnv, $tmpData);
31+
}
32+
33+
public function testInstance()
34+
{
35+
$this->instance = new Envparser;
36+
$instanceClass = $this->instance instanceof Envparser;
37+
38+
$this->assertEquals($instanceClass, true);
39+
}
40+
41+
private function getFileTest($envFile)
42+
{
43+
return __DIR__.self::DS.'fixtures'.self::DS.$envFile;
44+
}
45+
46+
public function testRead()
47+
{
48+
$envfile = $this->getFileTest('.env');
49+
50+
$this->assertTrue(is_readable($envfile));
51+
}
52+
53+
public function testEnvLoadsFile()
54+
{
55+
$envfile = $this->getFileTest('.env');
56+
57+
$envparser = new Envparser($envfile);
58+
$envparserLoader = $envparser->load();
59+
60+
$this->assertTrue($envparserLoader);
61+
}
62+
63+
public function testEnvVar()
64+
{
65+
$envfile = $this->getFileTest('.env');
66+
67+
$envparser = new Envparser($envfile);
68+
$envparserLoader = $envparser->load();
69+
70+
$envparser->run();
71+
72+
$envVar = $envparser->getValue('FOO');
73+
74+
$this->assertSame($envVar, 'bar');
75+
$this->assertSame($envVar, getenv('FOO'));
76+
}
1477
}

Tests/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
error_reporting(E_ALL | E_STRICT);
1010

1111
/**
12-
* Autloader
12+
* Autoloader
1313
*/
14-
require __DIR__.'../../vendor/autoload.php';
14+
require __DIR__.'../../../../../vendor/autoload.php';

Tests/fixtures/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=bar

docs/README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `Environment manager` parse, populates dot environment files to super global $_ENV files, HTTP request headers.
44

5-
It provides to edit environment file.
5+
It provides for editing environment file and manipulate them.
66

77
## Installation
88

@@ -12,12 +12,60 @@ It provides to edit environment file.
1212

1313
## Prerequisities
1414

15-
PHP version requirements: _PHP >5.3_
15+
PHP version requirements: _PHP >7.0_
1616

1717
Use `use Codervio\Environment\EnvParser` declaration for parsing dot env files.
1818

1919
Use `use Codervio\Environment\EnvEditor` declaration for edit and manage dot env file.
2020

21+
## Usage
22+
23+
Example of fetching env variables and loading into global super variables like `$_ENV`, using function `getenv()` or directly using instance:
24+
25+
If on a file example.env contains a data:
26+
27+
```text
28+
FOO=bar
29+
```
30+
31+
After loading instance it can be fetching a variable:
32+
33+
```php
34+
use Codervio\Envmanager\Envparser;
35+
36+
$envparser = new Envparser;
37+
$envparser->load();
38+
$envparser->run();
39+
40+
$result = $parser->getValue('FOO');
41+
var_dump($result);
42+
```
43+
44+
Returns a result will automatically detect type of getting env variables:
45+
46+
```php
47+
(string) 'bar'
48+
```
49+
50+
Or get a result using env variables globally:
51+
```php
52+
echo apache_getenv('FOO')
53+
echo getenv('FOO')
54+
echo $_ENV('FOO')
55+
```
56+
57+
## Returning a result
58+
59+
A result returns in following orders:
60+
- using apache_getenv() if apache service is configured internally to use only Apache environment
61+
- using getenv() that most Unix systems supports
62+
- using PHP native super globals $_ENV function
63+
64+
It will automatically parse to PHP env functions and super globals so you can access via:
65+
- superglobals functions $_ENV
66+
- superglobals $_SERVER variable
67+
- using getenv() if is enabled by system
68+
- using apache_getenv() if is enabled by Apache service
2169

2270
## Changelog
2371

@@ -27,11 +75,13 @@ Status of core:
2775
| ------------- |:-------------------- |
2876
| `1.0` | Release version |
2977

30-
PHP version above `5.3`.
78+
PHP version above `7.0`.
3179
Quality assurance: Unit tests provided
3280

3381
## Table of Contents
3482

3583
### EnvParser
3684

85+
* [`load()`](load.md) - Load an environment .env file or folder with .env files
86+
3787
### EnvEditor

docs/load.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# load
2+
3+
Load an environment .env file or folder with .env files
4+
5+
## Description
6+
7+
```php
8+
load($processEnvironment = true, $override = false)
9+
```
10+
11+
Load and env file or files inside folders.
12+
ProcessEnvironment parses a variable and populate into ENV system variables.
13+
If a variable exists on system from .env file it can override value.
14+
15+
By default it will load and automatically detect encoding type.
16+
17+
## Parameters
18+
19+
__processEnvironment__
20+
: Parse into system env variable from a file
21+
: Default: `true`
22+
23+
__override__
24+
: Replace a value into environment system variable
25+
: Default: `false`
26+
27+
## Return values
28+
29+
__bool__
30+
: Returns `TRUE` if loaded a file or `FALSE` if is not loaded a file
31+
32+
__Exception__
33+
1. Exception on `mb_string` encoding if not defined default mb_string values
34+
35+
## Examples
36+
37+
Example #1 Load a file
38+
```php
39+
use Codervio\Envmanager\Envparser;
40+
41+
$envparser = new Envparser('.env');
42+
$envparser->load();
43+
```
44+
45+
Example #2 Load a file from directory
46+
```php
47+
use Codervio\Envmanager\Envparser;
48+
49+
$envparser = new Envparser(__DIR__);
50+
$envparser->load();
51+
```
52+
53+
Example #3 Load a file and parse into ENV variable, don't override a variable into ENV system variable
54+
```php
55+
use Codervio\Envmanager\Envparser;
56+
57+
$envparser = new Envparser(__DIR__);
58+
$envparser->load(true, false);
59+
```
60+
61+
Example #4 Don't parse into global ENV variable, don't override a variable into ENV system variable
62+
```php
63+
use Codervio\Envmanager\Envparser;
64+
65+
$envparser = new Envparser(__DIR__);
66+
$envparser->load(false);
67+
```
68+
69+
## Notes
70+
71+
> This will not run ENV parser. It will only load a files. To run use `run()` function after `load()` a function that loaded a file or files.
72+
73+
## See also
74+
75+
_No documents._

src/Envparser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ public function load($processEnvironment = true, $override = false)
7272

7373
if ($this->loader->getLoadStatus()) {
7474
$this->context = $this->parseEncoding($this->loader->run());
75+
return true;
7576
}
77+
78+
return false;
7679
}
7780

7881
private function getParsed() : ParserCollector

0 commit comments

Comments
 (0)