|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Command; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use App\Util\AppUtil; |
| 7 | +use App\Manager\PasteManager; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use App\Command\RotateAppSecretCommand; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Tester\CommandTester; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class RotateAppSecretCommandTest |
| 16 | + * |
| 17 | + * Test cases for execute secret key rotation command |
| 18 | + * |
| 19 | + * @package App\Tests\Command |
| 20 | + */ |
| 21 | +class RotateAppSecretCommandTest extends TestCase |
| 22 | +{ |
| 23 | + private CommandTester $commandTester; |
| 24 | + private AppUtil & MockObject $appUtil; |
| 25 | + private RotateAppSecretCommand $command; |
| 26 | + private PasteManager & MockObject $pasteManager; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + // mock dependencies |
| 31 | + $this->appUtil = $this->createMock(AppUtil::class); |
| 32 | + $this->pasteManager = $this->createMock(PasteManager::class); |
| 33 | + |
| 34 | + // initialize command instance |
| 35 | + $this->command = new RotateAppSecretCommand($this->appUtil, $this->pasteManager); |
| 36 | + $this->commandTester = new CommandTester($this->command); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test execute command successfully |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function testExecuteSuccess(): void |
| 45 | + { |
| 46 | + // mock generateKey |
| 47 | + $this->appUtil->expects($this->once())->method('generateKey')->with(16)->willReturn('new-secret-value'); |
| 48 | + |
| 49 | + // mock updateEnvValue |
| 50 | + $this->appUtil->expects($this->once())->method('updateEnvValue')->with('APP_SECRET', 'new-secret-value'); |
| 51 | + |
| 52 | + // execute command |
| 53 | + $exitCode = $this->commandTester->execute([]); |
| 54 | + |
| 55 | + // get command output |
| 56 | + $output = $this->commandTester->getDisplay(); |
| 57 | + |
| 58 | + // assert output |
| 59 | + $this->assertStringContainsString('APP_SECRET has been rotated successfully', $output); |
| 60 | + $this->assertEquals(Command::SUCCESS, $exitCode); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test execute command with exception |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function testExecuteThrowsException(): void |
| 69 | + { |
| 70 | + // mock generateKey to throw exception |
| 71 | + $this->appUtil->method('generateKey')->willThrowException(new Exception('Something went wrong')); |
| 72 | + |
| 73 | + // execute command |
| 74 | + $exitCode = $this->commandTester->execute([]); |
| 75 | + |
| 76 | + // get command output |
| 77 | + $output = $this->commandTester->getDisplay(); |
| 78 | + |
| 79 | + // assert output |
| 80 | + $this->assertStringContainsString('Error during rotation: Something went wrong', $output); |
| 81 | + $this->assertEquals(Command::FAILURE, $exitCode); |
| 82 | + } |
| 83 | +} |
0 commit comments