This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
forked from PhilippBaschke/acf-pro-installer
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tests/unit/LicenseKey/Providers/DotEnv/DotEnvV5AdapterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |