Skip to content

Commit 41932b7

Browse files
.env file load setup
1 parent 5c06ec8 commit 41932b7

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

src/Util/EnvSetup.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace Supabase\Client\Util;
3+
use Dotenv\Dotenv;
4+
5+
class EnvSetup
6+
{
7+
public static function env($path): array
8+
{
9+
// If the env vars are set and not empty use them
10+
$key = getenv('API_KEY');
11+
$url = getenv('URL');
12+
13+
// else check try to load the .env file in the $path
14+
if (empty($key) || empty($url)) {
15+
$loaded = Dotenv::createArrayBacked($path)->safeLoad();
16+
17+
if (key_exists('API_KEY', $loaded)) {
18+
$key = $loaded['API_KEY'];
19+
}
20+
21+
if (key_exists('URL', $loaded)) {
22+
$url = $loaded['URL'];
23+
}
24+
}
25+
26+
if (empty($key)) {
27+
throw new \Exception('Could not load API_KEY');
28+
}
29+
30+
if (empty($url)) {
31+
throw new \Exception('Could not load URL');
32+
}
33+
34+
return [
35+
'API_KEY' => $key,
36+
'URL' => $url,
37+
];
38+
}
39+
}

tests/SupabaseTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@
66
use PHPUnit\Framework\Attributes\Test;
77
use PHPUnit\Framework\TestCase;
88
use Supabase\Client\Functions as Supabase;
9-
use Dotenv\Dotenv;
9+
use Supabase\Client\Util\EnvSetup;
1010

1111
#[CoversClass(Functions::class)]
1212
class SupabaseTest extends TestCase
1313
{
1414
#[Test]
15-
public function connected()
15+
public function setUp() :void
1616
{
17-
$dotenv = Dotenv::createImmutable(__DIR__."/../");
18-
$dotenv->load();
17+
parent::setUp();
18+
$keys = EnvSetup::env(__DIR__.'/../');
19+
$key = $keys['API_KEY'];
20+
$url = $keys['URL'];
1921

20-
$config = [
21-
'url' => $_ENV['URL'],
22-
'apikey' => $_ENV['APIKEY']
23-
];
24-
25-
$client = new Supabase($config['url'], $config['apikey']);
22+
$client = new Supabase($url, $key);
2623
$this->assertInstanceOf(Supabase::class, $client);
27-
// $this->assertTrue($client->getAllData('Users'));
2824
}
2925
}

0 commit comments

Comments
 (0)