101 lines
2.6 KiB
PHP
101 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Academy;
|
|
|
|
use App\Services\Academy\AcademyBillingPlanService;
|
|
use ReflectionMethod;
|
|
use Tests\TestCase;
|
|
|
|
final class AcademyBillingPlanServiceTest extends TestCase
|
|
{
|
|
private array $originalEnv = [];
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
$this->restoreEnv('STRIPE_SECRET');
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_stripe_secret_ignores_non_string_cashier_secret_and_falls_back_to_env(): void
|
|
{
|
|
config()->set('cashier.secret', ['secret' => 'invalid']);
|
|
$this->setEnv('STRIPE_SECRET', ' sk_test_fallback ');
|
|
|
|
$service = new AcademyBillingPlanService();
|
|
|
|
$this->assertSame('sk_test_fallback', $this->invokeStripeSecret($service));
|
|
}
|
|
|
|
public function test_stripe_secret_returns_null_when_no_string_secret_is_available(): void
|
|
{
|
|
config()->set('cashier.secret', ['secret' => 'invalid']);
|
|
$this->setEnv('STRIPE_SECRET', null);
|
|
|
|
$service = new AcademyBillingPlanService();
|
|
|
|
$this->assertNull($this->invokeStripeSecret($service));
|
|
}
|
|
|
|
private function invokeStripeSecret(AcademyBillingPlanService $service): ?string
|
|
{
|
|
$method = new ReflectionMethod($service, 'stripeSecret');
|
|
$method->setAccessible(true);
|
|
|
|
/** @var ?string $secret */
|
|
$secret = $method->invoke($service);
|
|
|
|
return $secret;
|
|
}
|
|
|
|
private function setEnv(string $key, ?string $value): void
|
|
{
|
|
if (! array_key_exists($key, $this->originalEnv)) {
|
|
$this->originalEnv[$key] = [
|
|
'server' => $_SERVER[$key] ?? null,
|
|
'env' => $_ENV[$key] ?? null,
|
|
'putenv' => getenv($key) === false ? null : getenv($key),
|
|
];
|
|
}
|
|
|
|
if ($value === null) {
|
|
putenv($key);
|
|
unset($_SERVER[$key], $_ENV[$key]);
|
|
|
|
return;
|
|
}
|
|
|
|
putenv($key.'='.$value);
|
|
$_SERVER[$key] = $value;
|
|
$_ENV[$key] = $value;
|
|
}
|
|
|
|
private function restoreEnv(string $key): void
|
|
{
|
|
if (! array_key_exists($key, $this->originalEnv)) {
|
|
return;
|
|
}
|
|
|
|
$original = $this->originalEnv[$key];
|
|
|
|
if ($original['putenv'] === null) {
|
|
putenv($key);
|
|
} else {
|
|
putenv($key.'='.$original['putenv']);
|
|
}
|
|
|
|
if ($original['server'] === null) {
|
|
unset($_SERVER[$key]);
|
|
} else {
|
|
$_SERVER[$key] = $original['server'];
|
|
}
|
|
|
|
if ($original['env'] === null) {
|
|
unset($_ENV[$key]);
|
|
} else {
|
|
$_ENV[$key] = $original['env'];
|
|
}
|
|
}
|
|
} |