Skip to content

Instantly share code, notes, and snippets.

@bilogic
Created April 26, 2024 03:26
Show Gist options
  • Save bilogic/4114836067651905888abd542becee55 to your computer and use it in GitHub Desktop.
Save bilogic/4114836067651905888abd542becee55 to your computer and use it in GitHub Desktop.
Flaky tests
<?php
namespace App;
use App\Exceptions\UnknownEnvKey;
use Dotenv\Dotenv;
class EnvChecker
{
protected $requiredKeys;
protected $optionalKeys;
protected $dotenv = null;
protected $array = [];
/**
* A list of keys that are mandatory for the .env file
*
* @param array $array
*/
public function requiredKeys($array): static
{
$this->requiredKeys = $array;
return $this;
}
/**
* A list of keys that are optional for the .env file
*
* @param [type] $array
*/
public function optionalKeys($array): static
{
$this->optionalKeys = $array;
return $this;
}
/**
* Get the value of a key from our .env file
*
* @param string $key
*/
public function value($key)
{
return $this->array[$key];
}
/**
* Parse the contents of .env file and throw exceptions if unknown or missing keys
*/
public function parseFile(string $filename): static
{
$dir = dirname($filename);
$file = basename($filename);
$this->dotenv = Dotenv::createImmutable($dir, $file);
$this->array = $this->dotenv->load();
return $this
->checkForUnknownKeys()
->checkForMissingKeys();
}
private function checkForMissingKeys(): static
{
$this->dotenv->required($this->requiredKeys);
return $this;
}
private function checkForUnknownKeys(): static
{
$allKnown = array_merge($this->requiredKeys, $this->optionalKeys);
foreach ($this->array as $key => $value) {
if (! in_array($key, $allKnown)) {
throw new UnknownEnvKey($key);
}
}
return $this;
}
}
<?php
namespace App\Tests\Feature;
use App\EnvChecker;
use App\Exceptions\UnknownEnvKey;
use App\Tests\TestbenchTestCase;
class EnvCheckerTest extends TestbenchTestCase
{
// use \Illuminate\Foundation\Testing\DatabaseMigrations;
// use \Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use \Illuminate\Foundation\Testing\WithoutMiddleware;
protected $filename;
public function setUp(): void
{
parent::setUp();
$this->filename = __DIR__.'/../Fixtures/myconfig';
}
public function test_Can_parse_environment_file()
{
$content = <<<'TXT'
ABCD=123
DEF=456
TXT;
file_put_contents($this->filename, $content);
$checker = (new EnvChecker)
->requiredKeys([
'ABCD',
])
->optionalKeys([
'DEF',
]);
$checker->parseFile($this->filename);
$this->assertEquals('123', $checker->value('ABCD'));
$this->assertEquals('456', $checker->value('DEF'));
unlink($this->filename);
}
public function test_Can_detect_unknown_environment_keys()
{
$content = <<<'TXT'
UNKNOWN=123
DEF=456
TXT;
file_put_contents($this->filename, $content);
$this->expectException(UnknownEnvKey::class);
$checker = (new EnvChecker)
->requiredKeys([])
->optionalKeys([
'DEF',
]);
$checker->parseFile($this->filename);
unlink($this->filename);
}
public function test_Can_detect_missing_mandatory_environment_keys()
{
$content = <<<'TXT'
DEF=456
TXT;
file_put_contents($this->filename, $content);
$this->expectException(\Dotenv\Exception\ValidationException::class);
(new EnvChecker)
->requiredKeys([
'ABCD',
])
->optionalKeys([
'DEF',
])
->parseFile($this->filename);
unlink($this->filename);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment