diff --git a/src/Console/KeysCommand.php b/src/Console/KeysCommand.php index 36ff6e6c6..30490c441 100644 --- a/src/Console/KeysCommand.php +++ b/src/Console/KeysCommand.php @@ -29,7 +29,7 @@ class KeysCommand extends Command /** * Execute the console command. * - * @return void + * @return int */ public function handle() { @@ -40,6 +40,8 @@ public function handle() if ((file_exists($publicKey) || file_exists($privateKey)) && ! $this->option('force')) { $this->error('Encryption keys already exist. Use the --force option to overwrite them.'); + + return 1; } else { if (class_exists(LegacyRSA::class)) { $keys = (new LegacyRSA)->createKey($this->input ? (int) $this->option('length') : 4096); @@ -55,5 +57,7 @@ public function handle() $this->info('Encryption keys generated successfully.'); } + + return 0; } } diff --git a/tests/Feature/KeysCommandTest.php b/tests/Feature/KeysCommandTest.php new file mode 100644 index 000000000..e7d528f82 --- /dev/null +++ b/tests/Feature/KeysCommandTest.php @@ -0,0 +1,29 @@ +assertFileExists(self::PUBLIC_KEY); + $this->assertFileExists(self::PRIVATE_KEY); + } + + public function testPrivateAndPublicKeysShouldNotBeGeneratedTwice() + { + $this->artisan('passport:keys') + ->assertFailed() + ->expectsOutput('Encryption keys already exist. Use the --force option to overwrite them.'); + } +} diff --git a/tests/Feature/PassportTestCase.php b/tests/Feature/PassportTestCase.php index d7da023cb..610bfef37 100644 --- a/tests/Feature/PassportTestCase.php +++ b/tests/Feature/PassportTestCase.php @@ -12,7 +12,7 @@ abstract class PassportTestCase extends TestCase { use RefreshDatabase; - const KEYS = __DIR__.'/keys'; + const KEYS = __DIR__.'/../keys'; const PUBLIC_KEY = self::KEYS.'/oauth-public.key'; const PRIVATE_KEY = self::KEYS.'/oauth-private.key'; @@ -24,6 +24,8 @@ protected function setUp(): void Passport::routes(); + Passport::loadKeysFrom(self::KEYS); + @unlink(self::PUBLIC_KEY); @unlink(self::PRIVATE_KEY); diff --git a/tests/Unit/KeysCommandTest.php b/tests/Unit/KeysCommandTest.php deleted file mode 100644 index 991d21564..000000000 --- a/tests/Unit/KeysCommandTest.php +++ /dev/null @@ -1,81 +0,0 @@ -makePartial() - ->shouldReceive('info') - ->with('Encryption keys generated successfully.') - ->getMock(); - - Container::getInstance()->instance('path.storage', self::KEYS); - - $command->handle(); - - $this->assertFileExists(self::PUBLIC_KEY); - $this->assertFileExists(self::PRIVATE_KEY); - } - - public function testPrivateAndPublicKeysAreGeneratedInCustomPath() - { - Passport::loadKeysFrom(self::KEYS); - - $command = m::mock(KeysCommand::class) - ->makePartial() - ->shouldReceive('info') - ->with('Encryption keys generated successfully.') - ->getMock(); - - $command->handle(); - - $this->assertFileExists(self::PUBLIC_KEY); - $this->assertFileExists(self::PRIVATE_KEY); - - return $command; - } - - /** - * @depends testPrivateAndPublicKeysAreGeneratedInCustomPath - */ - public function testPrivateAndPublicKeysShouldNotBeGeneratedTwice($command) - { - $command->shouldReceive('option') - ->with('force') - ->andReturn(false); - - $command->shouldReceive('error') - ->with('Encryption keys already exist. Use the --force option to overwrite them.'); - - $command->handle(); - } -}