Skip to content

Commit 813a912

Browse files
committed
Added console command test
1 parent e2cf897 commit 813a912

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/Command/UserSetApiKeyCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Symfony\Component\Console\Attribute\Argument;
88
use Symfony\Component\Console\Attribute\AsCommand;
99
use Symfony\Component\Console\Command\Command;
10-
use Symfony\Component\Console\Exception\InvalidArgumentException;
1110
use Symfony\Component\Console\Style\SymfonyStyle;
1211

1312
#[AsCommand(
@@ -31,7 +30,9 @@ public function __invoke(
3130
?? $this->userRepository->findOneBy(['name' => $userId]);
3231

3332
if (null === $user) {
34-
throw new InvalidArgumentException(sprintf('Cannot load user with id %s', $userId));
33+
$io->error(sprintf('Cannot load user with id %s', $userId));
34+
35+
return Command::INVALID;
3536
}
3637

3738
$question = sprintf('Really set API key on user %s', $user->getUserIdentifier());
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)