framework
framework copied to clipboard
[12.x] Add `disableConfigCache` method to the `WithCachedConfig` trait
This addition is a follow-up to #57663 and makes it possible to use WithCachedConfig in the base TestCase and disable it for certain tests that override the ENV or Config.
For example for these two test examples which fail without the disableConfigCache:
<?php declare(strict_types=1);
namespace Tests\Unit;
use Illuminate\Contracts\Encryption\DecryptException;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
final class EncrypterTest extends TestCase
{
private string $originalAppKey;
private string $oldAppKey;
protected function setUp(): void
{
parent::setUp();
$this->disableConfigCache();
$this->oldAppKey = 'A6Ic4cFVOr5veD9TferaIhgDyNnqz3eq';
/** @phpstan-ignore disallowed.function */
putenv('APP_PREVIOUS_KEYS=' . $this->oldAppKey);
$this->originalAppKey = config('app.key');
}
protected function tearDown(): void
{
$this->setAppKeyAndClearEncrypterInstance($this->originalAppKey);
parent::tearDown();
}
#[Test]
public function it_can_encrypt_and_decrypt_using_regular_app_key(): void
{
$payload = fake()->text();
$encrypted = encrypt($payload);
self::assertNotSame($encrypted, $payload);
$decrypted = decrypt($encrypted);
self::assertSame($decrypted, $payload);
}
#[Test]
public function it_can_decrypt_payload_with_old_app_key(): void
{
$payload = fake()->text();
$this->setAppKeyAndClearEncrypterInstance($this->oldAppKey);
$encrypted = encrypt($payload);
self::assertNotSame($encrypted, $payload);
$this->setAppKeyAndClearEncrypterInstance($this->originalAppKey);
$decrypted = decrypt($encrypted);
self::assertSame($decrypted, $payload);
}
#[Test]
public function decrypting_payload_with_invalid_old_app_key_throws_exception(): void
{
$payload = fake()->text();
$this->expectException(DecryptException::class);
$incorrectOldAppKey = 'd5So7vPWLw9dnR2UfswmJidBpEhyr5tq';
$this->setAppKeyAndClearEncrypterInstance($incorrectOldAppKey);
$encrypted = encrypt($payload);
self::assertNotSame($encrypted, $payload);
$this->setAppKeyAndClearEncrypterInstance($this->originalAppKey);
decrypt($encrypted);
}
private function setAppKeyAndClearEncrypterInstance(string $appKey): void
{
config()->set('app.key', $appKey);
app()->forgetInstance('encrypter');
}
}
And
<?php declare(strict_types=1);
namespace Tests\Unit;
use Illuminate\Foundation\Console\PackageDiscoverCommand;
use Illuminate\Support\Env;
use Illuminate\Testing\PendingCommand;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
final class CreateProjectTest extends TestCase
{
private string $appKey;
protected function setUp(): void
{
parent::setUp();
$this->disableConfigCache();
$this->appKey = config('app.key');
Env::enablePutenv();
/** @phpstan-ignore disallowed.function */
putenv('APP_KEY=');
$_SERVER['APP_KEY'] = null;
$_ENV['APP_KEY'] = null;
$this->refreshApplication();
}
protected function assertPreConditions(): void
{
self::assertEmpty(config('app.key'));
}
protected function tearDown(): void
{
parent::tearDown();
/** @phpstan-ignore disallowed.function */
putenv('APP_KEY=' . $this->appKey);
$_SERVER['APP_KEY'] = $this->appKey;
$_ENV['APP_KEY'] = $this->appKey;
}
#[Test]
public function it_can_discover_packages_without_an_app_key_set(): void
{
$pendingCommand = $this->artisan(PackageDiscoverCommand::class);
if (! $pendingCommand instanceof PendingCommand) {
throw new \RuntimeException('Expecting to mockConsoleOutput');
}
$pendingCommand->assertSuccessful();
}
}
Thanks for submitting a PR!
Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.
Pull requests that are abandoned in draft may be closed due to inactivity.
@cosmastech could you shine your light on this PR, would this be a logical approach to achieving this functionality?