|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use Neuron\Cli\Console\Input; |
9 | 9 | use Neuron\Cli\Console\Output; |
| 10 | +use Neuron\Cli\IO\TestInputReader; |
10 | 11 |
|
11 | 12 | class ShowCommandTest extends TestCase |
12 | 13 | { |
@@ -135,6 +136,103 @@ public function testExecuteShowsSpecificKey(): void |
135 | 136 | $this->assertStringNotContainsString( 'api:', $outputContent ); |
136 | 137 | } |
137 | 138 |
|
| 139 | + /** |
| 140 | + * Test production environment confirmation prompt |
| 141 | + */ |
| 142 | + public function testExecuteProductionConfirmation(): void |
| 143 | + { |
| 144 | + // Create test secrets for production environment |
| 145 | + mkdir( $this->testConfigPath . '/secrets', 0755, true ); |
| 146 | + $keyPath = $this->testConfigPath . '/secrets/production.key'; |
| 147 | + $credentialsPath = $this->testConfigPath . '/secrets/production.yml.enc'; |
| 148 | + |
| 149 | + $key = $this->secretManager->generateKey( $keyPath ); |
| 150 | + |
| 151 | + $tempPlaintextPath = $this->testConfigPath . '/temp_plaintext.yml'; |
| 152 | + $testData = "database:\n password: production_secret"; |
| 153 | + file_put_contents( $tempPlaintextPath, $testData ); |
| 154 | + $this->secretManager->encrypt( $tempPlaintextPath, $credentialsPath, $keyPath ); |
| 155 | + unlink( $tempPlaintextPath ); |
| 156 | + |
| 157 | + // Test 1: User confirms - secrets should be shown |
| 158 | + $input = new Input( [ |
| 159 | + '--config=' . $this->testConfigPath, |
| 160 | + '--env=production' |
| 161 | + ] ); |
| 162 | + $input->parse( $this->command ); |
| 163 | + |
| 164 | + $output = new Output( false ); |
| 165 | + |
| 166 | + // Set up test input reader to confirm |
| 167 | + $inputReader = new TestInputReader(); |
| 168 | + $inputReader->addResponse( 'yes' ); |
| 169 | + |
| 170 | + $this->command->setInput( $input ); |
| 171 | + $this->command->setOutput( $output ); |
| 172 | + $this->command->setInputReader( $inputReader ); |
| 173 | + |
| 174 | + // Capture output |
| 175 | + ob_start(); |
| 176 | + $result = $this->command->execute(); |
| 177 | + $outputContent = ob_get_clean(); |
| 178 | + |
| 179 | + // Should succeed and show secrets |
| 180 | + $this->assertEquals( 0, $result ); |
| 181 | + $this->assertStringContainsString( 'You are about to display production secrets!', $outputContent ); |
| 182 | + $this->assertStringContainsString( 'production_secret', $outputContent ); |
| 183 | + |
| 184 | + // Test 2: User cancels - secrets should NOT be shown |
| 185 | + $input2 = new Input( [ |
| 186 | + '--config=' . $this->testConfigPath, |
| 187 | + '--env=production' |
| 188 | + ] ); |
| 189 | + $input2->parse( $this->command ); |
| 190 | + |
| 191 | + $output2 = new Output( false ); |
| 192 | + |
| 193 | + // Set up test input reader to cancel |
| 194 | + $inputReader2 = new TestInputReader(); |
| 195 | + $inputReader2->addResponse( 'no' ); |
| 196 | + |
| 197 | + $this->command->setInput( $input2 ); |
| 198 | + $this->command->setOutput( $output2 ); |
| 199 | + $this->command->setInputReader( $inputReader2 ); |
| 200 | + |
| 201 | + // Capture output |
| 202 | + ob_start(); |
| 203 | + $result2 = $this->command->execute(); |
| 204 | + $outputContent2 = ob_get_clean(); |
| 205 | + |
| 206 | + // Should exit gracefully without showing secrets |
| 207 | + $this->assertEquals( 0, $result2 ); |
| 208 | + $this->assertStringContainsString( 'Operation cancelled.', $outputContent2 ); |
| 209 | + $this->assertStringNotContainsString( 'production_secret', $outputContent2 ); |
| 210 | + |
| 211 | + // Test 3: Force flag should skip confirmation |
| 212 | + $input3 = new Input( [ |
| 213 | + '--config=' . $this->testConfigPath, |
| 214 | + '--env=production', |
| 215 | + '--force' |
| 216 | + ] ); |
| 217 | + $input3->parse( $this->command ); |
| 218 | + |
| 219 | + $output3 = new Output( false ); |
| 220 | + |
| 221 | + $this->command->setInput( $input3 ); |
| 222 | + $this->command->setOutput( $output3 ); |
| 223 | + // No input reader needed - force skips confirmation |
| 224 | + |
| 225 | + // Capture output |
| 226 | + ob_start(); |
| 227 | + $result3 = $this->command->execute(); |
| 228 | + $outputContent3 = ob_get_clean(); |
| 229 | + |
| 230 | + // Should succeed without confirmation prompt |
| 231 | + $this->assertEquals( 0, $result3 ); |
| 232 | + $this->assertStringNotContainsString( 'You are about to display production secrets!', $outputContent3 ); |
| 233 | + $this->assertStringContainsString( 'production_secret', $outputContent3 ); |
| 234 | + } |
| 235 | + |
138 | 236 | /** |
139 | 237 | * Test error when secrets file not found |
140 | 238 | */ |
|
0 commit comments