|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Command; |
| 4 | + |
| 5 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 6 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 7 | +use Symfony\Component\Console\Tester\ApplicationTester; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests for UserSetApiKeyCommand. |
| 11 | + * |
| 12 | + * @see UserSetApiKeyCommand |
| 13 | + */ |
| 14 | +class UserSetApiKeyCommandTest extends KernelTestCase |
| 15 | +{ |
| 16 | + public function testUserSetApiKey(): void |
| 17 | + { |
| 18 | + self::bootKernel(); |
| 19 | + $application = new Application(self::$kernel); |
| 20 | + // this is key: don't terminate the PHP process after running the command |
| 21 | + $application->setAutoExit(false); |
| 22 | + |
| 23 | + $applicationTester = new ApplicationTester($application); |
| 24 | + $applicationTester->run([ |
| 25 | + 'command' => 'app:user:set-api-key', |
| 26 | + '--help' => true, |
| 27 | + ]); |
| 28 | + $applicationTester->assertCommandIsSuccessful(); |
| 29 | + |
| 30 | + // Missing user ID argument. |
| 31 | + $applicationTester->run([ |
| 32 | + 'command' => 'app:user:set-api-key', |
| 33 | + ]); |
| 34 | + $applicationTester->assertCommandFailed(); |
| 35 | + |
| 36 | + // Invalid user ID argument. |
| 37 | + $applicationTester->run([ |
| 38 | + 'command' => 'app:user:set-api-key', |
| 39 | + 'user-id' => 'this-user-does-no-exist', |
| 40 | + ]); |
| 41 | + $applicationTester->assertCommandIsInvalid(); |
| 42 | + $output = $applicationTester->getDisplay(); |
| 43 | + $this->assertStringContainsString('Cannot load user with id this-user-does-no-exist', $output); |
| 44 | + |
| 45 | + // Valid user ID (name). |
| 46 | + $applicationTester->run([ |
| 47 | + 'command' => 'app:user:set-api-key', |
| 48 | + 'user-id' => 'admin', |
| 49 | + '--no-interaction' => true, |
| 50 | + ]); |
| 51 | + $applicationTester->assertCommandIsSuccessful(); |
| 52 | + |
| 53 | + $output = $applicationTester->getDisplay(); |
| 54 | + $this->assertStringContainsString('API key for user admin@example.com set to', $output); |
| 55 | + |
| 56 | + // Valid user ID (email). |
| 57 | + $applicationTester->run([ |
| 58 | + 'command' => 'app:user:set-api-key', |
| 59 | + 'user-id' => 'admin@example.com', |
| 60 | + '--no-interaction' => true, |
| 61 | + ]); |
| 62 | + $applicationTester->assertCommandIsSuccessful(); |
| 63 | + |
| 64 | + $output = $applicationTester->getDisplay(); |
| 65 | + $this->assertStringContainsString('API key for user admin@example.com set to', $output); |
| 66 | + } |
| 67 | +} |
0 commit comments