Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Tests

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: composer test
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "naingaunglwin-dev/dotenv",
"description": "Simple and lightweight php dotenv library",
"minimum-stability": "dev",
"version": "2.0.0",
"prefer-stable": true,
"license": "MIT",
"authors": [
Expand All @@ -22,5 +21,8 @@
},
"require-dev": {
"phpunit/phpunit": "^11.5"
},
"scripts": {
"test": "vendor/bin/phpunit tests"
}
}
11 changes: 10 additions & 1 deletion src/Loader/BaseLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NAL\Dotenv\Loader;

use NAL\Dotenv\Exceptions\UnableToOpenFileException;
use NAL\Dotenv\Parser\ParserInterface;
use NAL\Dotenv\PathResolver;

Expand Down Expand Up @@ -54,7 +55,15 @@ protected function getDefaultFile(): string
*/
protected function resolveFiles(): array
{
return array_map(fn ($file) => $this->resolver->resolve($file), $this->files);
return array_map(function ($file) {
$file = $this->resolver->resolve($file);

if (!is_file($file) || !is_readable($file)) {
throw new UnableToOpenFileException($file);
}

return $file;
}, $this->files);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/DotenvLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function load(): array
$handle = @fopen($file, 'r');

if ($handle === false) {
throw new UnableToOpenFileException($file);
throw new UnableToOpenFileException($file); // @codeCoverageIgnore
}

$contents = fread($handle, filesize($file));
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function load(): array
$handle = @fopen($file, 'r');

if ($handle === false) {
throw new UnableToOpenFileException($file);
throw new UnableToOpenFileException($file); // @codeCoverageIgnore
}

$content = fread($handle, filesize($file));
Expand Down
4 changes: 4 additions & 0 deletions src/PathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public static function isAbsolute(string $path): bool
*/
public static function normalize(string $path): string
{
if (self::isAbsolute($path)) {
return str_replace(['/', '\\'], self::DS, $path);
}

return trim(str_replace(['/', '\\'], self::DS, $path), self::DS);
}
}
9 changes: 9 additions & 0 deletions tests/EnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public function testThrowExceptionIfEnvFileNotFound()
$env->load();
}

public function testEnvFileLoadWithCustomPath()
{
$file = $this->createEnvFile('.env', "FOO=bar\n");

$env = Env::create(loader: new DotenvLoader('.env', resolver: new PathResolver($this->dir)));

$this->assertSame('bar', $env->get('FOO'));
}

public function testThrowExceptionEnvLoadWithGuessLoaderForUnregisteredFileType()
{
$file = $this->createEnvFile('env.txt', "APP_TEST=true");
Expand Down