Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Add test for DotEnvV5Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrious committed Jun 11, 2020
1 parent 7154975 commit bde494d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"test": "phpunit -c phpunit.xml.dist --teamcity --testsuite unit",
"integration-test": "phpunit -c phpunit.xml.dist --testsuite integration --verbose --no-coverage",
"analyse": "phpstan analyse",
"coverage": "phpunit --coverage-html coverage",
"coverage": "phpunit -c phpunit.xml.dist --teamcity --testsuite unit --coverage-html coverage",
"coverage-check": "coverage-check build/logs/clover.xml 100"
}
}
91 changes: 91 additions & 0 deletions tests/unit/LicenseKey/Providers/DotEnv/DotEnvV5AdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);

namespace PivvenIT\Composer\Installers\ACFPro\Test\LicenseKey\Providers\DotEnv;

use PHPUnit\Framework\TestCase;
use PivvenIT\Composer\Installers\ACFPro\LicenseKey\Providers\DotEnv\DotEnvV5Adapter;
use PivvenIT\Composer\Installers\ACFPro\LicenseKey\Providers\EnvironmentVariableLicenseKeyProvider;

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class DotEnvV5AdapterTest extends TestCase
{
/**
* @var callable
*/
private $autoloader;

public function testLoadWithKeyInEnvFileMakesItAvailable()
{
$key = "ab83a014-61f5-412b-9084-5c5b056105c0";
$this->autoloader = function ($className) {
if ($className == "Dotenv\\Dotenv") {
$mock = new class {
public static function createUnsafeImmutable()
{
return new self;
}

public static function safeLoad()
{
// Load the ACF_PRO_KEY with the key specified above
putenv(
sprintf(
"%s=%s",
EnvironmentVariableLicenseKeyProvider::ENV_VARIABLE_NAME,
"ab83a014-61f5-412b-9084-5c5b056105c0"
)
);
}
};
class_alias(get_class($mock), 'Dotenv\\Dotenv');
}
};
spl_autoload_register($this->autoloader, false, true);
$sut = new DotEnvV5Adapter();
$this->assertFalse(getenv(EnvironmentVariableLicenseKeyProvider::ENV_VARIABLE_NAME));
$sut->load(getcwd());
$this->assertEquals($key, getenv(EnvironmentVariableLicenseKeyProvider::ENV_VARIABLE_NAME));
}

public function testLoadWithoutKeyInEnvFileDoesNotSetKey()
{
$this->autoloader = function ($className) {
if ($className == "Dotenv\\Dotenv") {
$mock = new class {
public static function createUnsafeImmutable()
{
return new self;
}

public static function safeLoad()
{
// Does not load anything
return;
}
};
class_alias(get_class($mock), 'Dotenv\\Dotenv');
}
};
spl_autoload_register($this->autoloader, false, true);
$sut = new DotEnvV5Adapter();
$this->assertFalse(getenv(EnvironmentVariableLicenseKeyProvider::ENV_VARIABLE_NAME));
$sut->load(getcwd());
$this->assertFalse(getenv(EnvironmentVariableLicenseKeyProvider::ENV_VARIABLE_NAME));
}

/**
* @inheritdoc
*/
protected function tearDown(): void
{
parent::tearDown();
if ($this->autoloader != null) {
spl_autoload_unregister($this->autoloader);
}
putenv(EnvironmentVariableLicenseKeyProvider::ENV_VARIABLE_NAME); //Clears the environment variable
}
}

0 comments on commit bde494d

Please sign in to comment.