Skip to content

Commit

Permalink
Add UnitTest
Browse files Browse the repository at this point in the history
Add DotEnvTest
Update composer.json
  • Loading branch information
alireaza committed Jan 4, 2022
1 parent 8c44ff1 commit 5cd712f
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 4 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alireaza/dot-env-resolver-variables",
"version": "1.0.0",
"version": "1.1.0",
"type": "library",
"license": "MIT",
"description": "This is Variables Resolver package for DotEnv.",
Expand All @@ -22,7 +22,7 @@
],
"require": {
"php": "^8.0.0",
"alireaza/dot-env": "1.0.0"
"alireaza/dot-env": "1.1.0"
},
"require-dev": {
"phpunit/phpunit": "9.5.9"
Expand Down
268 changes: 268 additions & 0 deletions tests/Unit/DotEnvTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
<?php

declare(strict_types=1);

namespace AliReaza\Tests\DotEnv\Resolver\Unit;

use AliReaza\DotEnv\DotEnv;
use PHPUnit\Framework\TestCase;
use Throwable;

class DotEnvTest extends TestCase
{
public function test_When_create_new_DotEnv_without_arguments_Expect_env_property_must_array_and_empty()
{
$env = new DotEnv();

$array = $env->toArray();

$this->assertTrue(is_array($array) && empty($array));
}

public function test_When_create_new_DotEnv_and_load_file_Expect_env_property_must_array_of_file_variables()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO=BAR');

$env = new DotEnv();

$env->load($file);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'BAR');
}

public function test_When_create_new_DotEnv_with_file_argument_Expect_env_property_must_array_of_file_variables()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO=BAR');

$env = new DotEnv($file);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'BAR');
}

public function test_When_create_new_DotEnv_with_file_and_resolvers_arguments_Expect_env_property_must_array_of_file_variables_that_must_be_converted_to_lowercase_by_resolver()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO=BAR');

$env = new DotEnv($file, [
new class {
public function __invoke(string $data)
{
return strtolower($data);
}
}
]);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'bar');
}

public function test_When_create_new_DotEnv_with_empty_file_Expect_env_property_must_array_and_empty()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, '');

$env = new DotEnv($file);

unlink($file);

rmdir($tmpdir);

$array = $env->toArray();

$this->assertTrue(is_array($array) && empty($array));
}

public function test_When_create_new_DotEnv_with_missing_equal_for_variable_in_file_Expect_throw_exception()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO');

$this->expectExceptionMessage('Missing = in the environment variable declaration: FOO');

try {
new DotEnv($file);
} catch (Throwable $exception) {
unlink($file);

rmdir($tmpdir);

throw $exception;
}

unlink($file);

rmdir($tmpdir);
}

public function test_When_create_new_DotEnv_with_whitespace_character_after_variable_in_file_Expect_throw_exception()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO =BAR');

$this->expectExceptionMessage('Whitespace characters are not supported after the variable name: FOO');

try {
new DotEnv($file);
} catch (Throwable $exception) {
unlink($file);

rmdir($tmpdir);

throw $exception;
}

unlink($file);

rmdir($tmpdir);
}

public function test_When_create_new_DotEnv_with_export_before_variable_in_file_Expect_env_property_must_array_of_file_variables()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'export FOO=BAR');

$env = new DotEnv($file);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'BAR');
}

public function test_When_create_new_DotEnv_with_whitespace_character_inside_variable_in_file_Expect_throw_exception()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO BAR=BAZ');

$this->expectExceptionMessage('Invalid character in variable name: FOO BAR');

try {
new DotEnv($file);
} catch (Throwable $exception) {
unlink($file);

rmdir($tmpdir);

throw $exception;
}

unlink($file);

rmdir($tmpdir);
}

public function test_When_create_new_DotEnv_with_whitespace_character_before_value_in_file_Expect_throw_exception()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO= BAR');

$this->expectExceptionMessage('Whitespace characters are not supported before the value: BAR');

try {
new DotEnv($file);
} catch (Throwable $exception) {
unlink($file);

rmdir($tmpdir);

throw $exception;
}

unlink($file);

rmdir($tmpdir);
}

public function test_When_create_new_DotEnv_with_whitespace_character_inside_value_in_file_Expect_env_property_must_array_of_file_variables()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO=BAR BAZ');

$env = new DotEnv($file);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'BAR BAZ');
}

public function test_When_create_new_DotEnv_with_value_with_single_quotes_in_file_Expect_env_property_must_array_of_file_variables_without_quotes()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, "FOO='BAR BAZ'");

$env = new DotEnv($file);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'BAR BAZ');
}

public function test_When_create_new_DotEnv_with_value_with_double_quotes_in_file_Expect_env_property_must_array_of_file_variables_without_quotes()
{
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO="BAR BAZ"');

$env = new DotEnv($file);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('FOO') && $env->get('FOO') === 'BAR BAZ');
}
}
20 changes: 18 additions & 2 deletions tests/Unit/VariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@

namespace AliReaza\Tests\DotEnv\Resolver\Unit;

use AliReaza\DotEnv\DotEnv;
use AliReaza\DotEnv\Resolver\Variables;
use PHPUnit\Framework\TestCase;

class VariablesTest extends TestCase
{
public function test_Coming_Soon()
public function test_When_use_Variables_Resolver_Expect_env_property_must_array_of_file_variables_and_all_variables_inside_values_have_value()
{
$this->markTestIncomplete('This test has not been implemented yet.');
mkdir($tmpdir = sys_get_temp_dir() . '/dotenv');

$file = tempnam($tmpdir, 'alireaza-');

file_put_contents($file, 'FOO=BAR' . "\n" . 'BAZ=${FOO}');

$env = new DotEnv($file, [
new Variables()
]);

unlink($file);

rmdir($tmpdir);

$this->assertTrue(is_array($env->toArray()) && $env->has('BAZ') && $env->get('BAZ') === 'BAR');
}
}

0 comments on commit 5cd712f

Please sign in to comment.