From e2d55af66635941d931b8e89af17553625c9699d Mon Sep 17 00:00:00 2001 From: Rias Date: Fri, 24 May 2024 16:44:08 +0200 Subject: [PATCH] [11.x] Add an option to remove the original environment file after encrypting (#51556) * Add option to remove original environment file * Rename remove to prune * Update test --- .../Console/EnvironmentEncryptCommand.php | 5 +++++ .../Console/EnvironmentEncryptCommandTest.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php b/src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php index f2c55fd3e98d..192112569c8e 100644 --- a/src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php +++ b/src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php @@ -21,6 +21,7 @@ class EnvironmentEncryptCommand extends Command {--key= : The encryption key} {--cipher= : The encryption cipher} {--env= : The environment to be encrypted} + {--prune= : Delete the original environment file} {--force : Overwrite the existing encrypted environment file}'; /** @@ -98,6 +99,10 @@ public function handle() return Command::FAILURE; } + if ($this->option('prune')) { + $this->files->delete($environmentFile); + } + $this->components->info('Environment successfully encrypted.'); $this->components->twoColumnDetail('Key', $keyPassed ? $key : 'base64:'.base64_encode($key)); diff --git a/tests/Integration/Console/EnvironmentEncryptCommandTest.php b/tests/Integration/Console/EnvironmentEncryptCommandTest.php index ac99801d74b8..8de3023f5525 100644 --- a/tests/Integration/Console/EnvironmentEncryptCommandTest.php +++ b/tests/Integration/Console/EnvironmentEncryptCommandTest.php @@ -155,4 +155,24 @@ public function testItEncryptsWithGivenGeneratedBase64KeyAndDisplaysIt() ->expectsOutputToContain('.env.encrypted') ->assertExitCode(0); } + + public function testItCanRemoveTheOriginalFile() + { + $this->filesystem->shouldReceive('exists') + ->once() + ->andReturn(true) + ->shouldReceive('exists') + ->once() + ->andReturn(false); + + $this->artisan('env:encrypt', ['--prune' => true]) + ->expectsOutputToContain('.env.encrypted') + ->assertExitCode(0); + + $this->filesystem->shouldHaveReceived('put') + ->with(base_path('.env.encrypted'), m::any()); + + $this->filesystem->shouldHaveReceived('delete') + ->with(base_path('.env')); + } }