Skip to content

Commit

Permalink
Merge pull request #10 from SolumDeSignum/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Faks authored Sep 1, 2024
2 parents 1efd74b + 68e970b commit be708a8
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ExampleIntegration implements PackageEnvLoaderContract
/**
* @var bool
*/
private bool $packageEnvLoaded;
private array $packageEnvLoaded;

/**
* ExampleIntegration constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/PackageEnvLoaderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface PackageEnvLoaderContract
{
public function packageEnvRootPath(array $paths): string|array;
public function packageEnvRootPath(array $paths): array;
}
11 changes: 11 additions & 0 deletions src/Enums/EnvironmentVariableEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace SolumDeSignum\PackageEnvLoader\Enums;

enum EnvironmentVariableEnum: string
{
case FIRST = '.env.first.test';
case SECOND = '.env.second.test';
}
38 changes: 27 additions & 11 deletions src/Traits/PackageEnvLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,44 @@
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidFileException;
use Illuminate\Support\Facades\Log;
use LogicException;
use SolumDeSignum\PackageEnvLoader\Contracts\PackageEnvLoaderContract;

trait PackageEnvLoader
{
public function createPackageDotenv(array $environmentFiles = ['.env']): bool
public function createPackageDotenv(array $environmentFiles = ['.env']): array
{
$responseLoaded = true;
if (!($this instanceof PackageEnvLoaderContract)) {
throw new LogicException(
'Class must implement PackageEnvLoaderContract to use method createPackageDotenv.'
);
}

$responsePackage = [];

foreach ($environmentFiles as $file) {
foreach ($environmentFiles as $environmentFile) {
try {
Dotenv::createImmutable(
$response = Dotenv::createImmutable(
$this->packageEnvRootPath(),
$file
)
->safeLoad();
$environmentFile
)->safeLoad();

Log::info("Loaded environment file: $file");
Log::info("Loaded environment file: $environmentFile");
} catch (InvalidFileException $e) {
Log::error("Failed to load environment file $file: " . $e->getMessage());
$responseLoaded = false;
Log::error("Failed to load environment file $environmentFile: " . $e->getMessage());
$response = false;
}

$responsePackage[] = $response;
}

$flattenedResponses = [];
foreach ($responsePackage as $response) {
if (is_array($response)) {
$flattenedResponses = array_merge($flattenedResponses, $response);
}
}

return $responseLoaded;
return $flattenedResponses;
}
}
6 changes: 3 additions & 3 deletions stubs/ExampleIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ExampleIntegration implements PackageEnvLoaderContract
{
use PackageEnvLoader;

private bool $packageEnv;
private array $packageEnv;

/**
* ExampleIntegration constructor.
Expand All @@ -24,9 +24,9 @@ public function __construct()
*
* @param array $paths
*
* @return string|array
* @return array
*/
public function packageEnvRootPath(array $paths = [__DIR__ . '/..']): string|array
public function packageEnvRootPath(array $paths = [__DIR__ . '/..']): array
{
return $paths;
}
Expand Down
110 changes: 74 additions & 36 deletions tests/Feature/PackageEnvLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,136 @@

use Orchestra\Testbench\TestCase;
use SolumDeSignum\PackageEnvLoader\Contracts\PackageEnvLoaderContract;
use SolumDeSignum\PackageEnvLoader\Enums\EnvironmentVariableEnum;
use SolumDeSignum\PackageEnvLoader\Traits\PackageEnvLoader;

class PackageEnvLoaderTest extends TestCase implements PackageEnvLoaderContract
{
use PackageEnvLoader;

/**
* Define the root path for the environment files.
*
* @param array $paths An array of path segments.
* @return string|array
*/
public function packageEnvRootPath(array $paths = [__DIR__ . '/..']): string|array
private array $env = [
'first' => EnvironmentVariableEnum::FIRST->value,
'second' => EnvironmentVariableEnum::SECOND->value
];

protected function setUp(): void
{
// Handle paths as array or string
return $paths;
parent::setUp();
$this->forgetEnvironmentVariables();
}

protected function tearDown(): void
{
$this->forgetEnvironmentVariables();
parent::tearDown();
}

/**
* @test
*/
public function firstEnv(): void
public function firstEnvExist(): void
{
// Adjusting to handle an array of paths
$paths = $this->packageEnvRootPath();
foreach ((array)$paths as $path) {
$this->assertFileExists($path . '/.env.first.test');
foreach ($this->packageEnvRootPath() as $path) {
$this->assertFileExists($path . '/' . $this->env['first']);
}
}

/**
* @test
*/
public function secondEnv(): void
public function secondEnvExist(): void
{
// Adjusting to handle an array of paths
$paths = $this->packageEnvRootPath();
foreach ((array)$paths as $path) {
$this->assertFileExists($path . '/.env.second.test');
foreach ($this->packageEnvRootPath() as $path) {
$this->assertFileExists($path . '/' . $this->env['second']);
}
}

/**
* @test
*/
public function packageName(): void
public function firstEnvIsLoaded(): void
{
$responsePackageDotenv = $this->createPackageDotenv(['.env.first.test']);
$responsePackageDotenv = $this->createPackageDotenv([$this->env['first']]);
$this->assertIsArray($responsePackageDotenv);
$this->assertArrayHasKey('PACKAGE_NAME_FIRST', $responsePackageDotenv);
$this->assertSame('PACKAGE_ENV_LOADER', env('PACKAGE_NAME_FIRST'));
}

/**
* @test
*/
public function packageDotenv(): void
public function secondEnvIsLoaded(): void
{
$responsePackageDotenv = $this->createPackageDotenv(['.env.first.test']);
$responsePackageDotenv = $this->createPackageDotenv([$this->env['second']]);
$this->assertIsArray($responsePackageDotenv);
$this->assertArrayHasKey('PACKAGE_NAME_SECOND', $responsePackageDotenv);
$this->assertSame('PACKAGE_ENV_LOADER_SECOND', env('PACKAGE_NAME_SECOND'));
}

/**
* @test
*/
public function everything(): void
public function firstEnvExistAndIsLoaded(): void
{
$paths = $this->packageEnvRootPath();
foreach ((array)$paths as $path) {
$this->assertFileExists($path . '/.env.first.test');
foreach ($this->packageEnvRootPath() as $path) {
$this->assertFileExists($path . '/' . $this->env['first']);
}

$responsePackageDotenv = $this->createPackageDotenv(['.env.first.test']);
$responsePackageDotenv = $this->createPackageDotenv([$this->env['first']]);
$this->assertIsArray($responsePackageDotenv);
$this->assertArrayHasKey('PACKAGE_NAME_FIRST', $responsePackageDotenv);
$this->assertSame('PACKAGE_ENV_LOADER', env('PACKAGE_NAME_FIRST'));
}

/**
* @test
*/
public function secondEnvExistAndIsLoaded(): void
{
foreach ($this->packageEnvRootPath() as $path) {
$this->assertFileExists($path . '/' . $this->env['second']);
}

$responsePackageDotenv = $this->createPackageDotenv([$this->env['second']]);
$this->assertIsArray($responsePackageDotenv);
$this->assertArrayHasKey('PACKAGE_NAME_SECOND', $responsePackageDotenv);
$this->assertSame('PACKAGE_ENV_LOADER_SECOND', env('PACKAGE_NAME_SECOND'));
}

/**
* @test
*/
public function multipleEverything(): void
{
$paths = $this->packageEnvRootPath();
foreach ((array)$paths as $path) {
$this->assertFileExists($path . '/.env.first.test');
$this->assertFileExists($path . '/.env.second.test');
foreach ($this->packageEnvRootPath() as $path) {
$this->assertFileExists($path . '/' . $this->env['first']);
$this->assertFileExists($path . '/' . $this->env['second']);
}

$responsePackageDotenvFirst = $this->createPackageDotenv(['.env.first.test']);
$this->assertIsArray($responsePackageDotenvFirst);
$responsePackageDotenv = $this->createPackageDotenv([
$this->env['first'],
$this->env['second']
]);

$this->assertIsArray($responsePackageDotenv);

$this->assertArrayHasKey('PACKAGE_NAME_FIRST', $responsePackageDotenv);
$this->assertSame('PACKAGE_ENV_LOADER', env('PACKAGE_NAME_FIRST'));

$responsePackageDotenvSecond = $this->createPackageDotenv(['.env.second.test']);
$this->assertIsArray($responsePackageDotenvSecond);
$this->assertArrayHasKey('PACKAGE_NAME_SECOND', $responsePackageDotenv);
$this->assertSame('PACKAGE_ENV_LOADER_SECOND', env('PACKAGE_NAME_SECOND'));
}

public function packageEnvRootPath(array $paths = [__DIR__ . '/..']): array
{
return $paths;
}

private function forgetEnvironmentVariables(): void
{
putenv('PACKAGE_NAME_FIRST');
putenv('PACKAGE_NAME_SECOND');
$_ENV = [];
$_SERVER = [];
}
}

0 comments on commit be708a8

Please sign in to comment.