Skip to content

Commit 9f14e6c

Browse files
feat: add GitHub Actions integration (#3)
* implement github action * remove version from composer.json * add absolute check condition to PathResolver::normalize * check file is readable or not in file BaseLoader::resolveFiles
1 parent 976c68e commit 9f14e6c

File tree

7 files changed

+69
-4
lines changed

7 files changed

+69
-4
lines changed

.github/workflows/tests.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Set up PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: 8.2
24+
25+
- name: Validate composer.json and composer.lock
26+
run: composer validate --strict
27+
28+
- name: Cache Composer packages
29+
id: composer-cache
30+
uses: actions/cache@v3
31+
with:
32+
path: vendor
33+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
34+
restore-keys: |
35+
${{ runner.os }}-php-
36+
37+
- name: Install dependencies
38+
run: composer install --prefer-dist --no-progress --no-suggest
39+
40+
- name: Run test suite
41+
run: composer test

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "naingaunglwin-dev/dotenv",
33
"description": "Simple and lightweight php dotenv library",
44
"minimum-stability": "dev",
5-
"version": "2.0.0",
65
"prefer-stable": true,
76
"license": "MIT",
87
"authors": [
@@ -22,5 +21,8 @@
2221
},
2322
"require-dev": {
2423
"phpunit/phpunit": "^11.5"
24+
},
25+
"scripts": {
26+
"test": "vendor/bin/phpunit tests"
2527
}
2628
}

src/Loader/BaseLoader.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NAL\Dotenv\Loader;
44

5+
use NAL\Dotenv\Exceptions\UnableToOpenFileException;
56
use NAL\Dotenv\Parser\ParserInterface;
67
use NAL\Dotenv\PathResolver;
78

@@ -54,7 +55,15 @@ protected function getDefaultFile(): string
5455
*/
5556
protected function resolveFiles(): array
5657
{
57-
return array_map(fn ($file) => $this->resolver->resolve($file), $this->files);
58+
return array_map(function ($file) {
59+
$file = $this->resolver->resolve($file);
60+
61+
if (!is_file($file) || !is_readable($file)) {
62+
throw new UnableToOpenFileException($file);
63+
}
64+
65+
return $file;
66+
}, $this->files);
5867
}
5968

6069
/**

src/Loader/DotenvLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function load(): array
4343
$handle = @fopen($file, 'r');
4444

4545
if ($handle === false) {
46-
throw new UnableToOpenFileException($file);
46+
throw new UnableToOpenFileException($file); // @codeCoverageIgnore
4747
}
4848

4949
$contents = fread($handle, filesize($file));

src/Loader/JsonLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function load(): array
4646
$handle = @fopen($file, 'r');
4747

4848
if ($handle === false) {
49-
throw new UnableToOpenFileException($file);
49+
throw new UnableToOpenFileException($file); // @codeCoverageIgnore
5050
}
5151

5252
$content = fread($handle, filesize($file));

src/PathResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public static function isAbsolute(string $path): bool
107107
*/
108108
public static function normalize(string $path): string
109109
{
110+
if (self::isAbsolute($path)) {
111+
return str_replace(['/', '\\'], self::DS, $path);
112+
}
113+
110114
return trim(str_replace(['/', '\\'], self::DS, $path), self::DS);
111115
}
112116
}

tests/EnvTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ public function testThrowExceptionIfEnvFileNotFound()
146146
$env->load();
147147
}
148148

149+
public function testEnvFileLoadWithCustomPath()
150+
{
151+
$file = $this->createEnvFile('.env', "FOO=bar\n");
152+
153+
$env = Env::create(loader: new DotenvLoader('.env', resolver: new PathResolver($this->dir)));
154+
155+
$this->assertSame('bar', $env->get('FOO'));
156+
}
157+
149158
public function testThrowExceptionEnvLoadWithGuessLoaderForUnregisteredFileType()
150159
{
151160
$file = $this->createEnvFile('env.txt', "APP_TEST=true");

0 commit comments

Comments
 (0)