Skip to content

Commit dbb7f15

Browse files
committed
refactor: remove redundant key generation
1 parent e3dfc0c commit dbb7f15

File tree

6 files changed

+144
-81
lines changed

6 files changed

+144
-81
lines changed

packages/cryptography/src/CreateSigningKeyCommand.php renamed to packages/cryptography/src/GenerateSigningKeyCommand.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,28 @@
1313

1414
use function Tempest\root_path;
1515

16-
final readonly class CreateSigningKeyCommand
16+
final readonly class GenerateSigningKeyCommand
1717
{
1818
public function __construct(
1919
private EncryptionConfig $encryptionConfig,
2020
private Console $console,
2121
) {}
2222

2323
#[ConsoleCommand('key:generate', description: 'Generates the signing key required to sign and verify data.')]
24-
public function __invoke(): ExitCode
24+
public function __invoke(bool $override = true): ExitCode
2525
{
2626
$key = EncryptionKey::generate($this->encryptionConfig->algorithm);
2727

28+
$this->createDotEnvIfNotExists();
29+
$this->addToDotEnv($key->toString(), $override);
30+
2831
$this->console->writeln();
29-
$this->console->success('Signing key generated successfully.');
3032

31-
$this->createDotEnvIfNotExists();
32-
$this->addToDotEnv($key->toString());
33+
if ($override) {
34+
$this->console->success('Signing key generated successfully.');
35+
} else {
36+
$this->console->info('The signing key already exists.');
37+
}
3338

3439
return ExitCode::SUCCESS;
3540
}
@@ -39,13 +44,13 @@ private function getDotEnvPath(): string
3944
return root_path('.env');
4045
}
4146

42-
private function addToDotEnv(string $key): void
47+
private function addToDotEnv(string $key, bool $override): void
4348
{
4449
$file = Filesystem\read_file($this->getDotEnvPath());
4550

4651
if (! Str\contains($file, 'SIGNING_KEY=')) {
4752
$file = "SIGNING_KEY={$key}\n" . $file;
48-
} else {
53+
} elseif ($override) {
4954
$file = Regex\replace($file, '/^SIGNING_KEY=.*$/m', "SIGNING_KEY={$key}");
5055
}
5156

tests/Integration/Cryptography/CreateSigningKeyCommandTest.php

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Integration\Cryptography;
4+
5+
use Dotenv\Dotenv;
6+
use Tempest\Core\FrameworkKernel;
7+
use Tempest\Cryptography\GenerateSigningKeyCommand;
8+
use Tempest\Support\Filesystem;
9+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
10+
11+
use function Tempest\root_path;
12+
13+
final class GenerateSigningKeyCommandTest extends FrameworkIntegrationTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->container->get(FrameworkKernel::class)->root = __DIR__;
20+
}
21+
22+
protected function tearDown(): void
23+
{
24+
parent::tearDown();
25+
26+
Filesystem\delete_file(root_path('.env'));
27+
}
28+
29+
public function test_creates_dot_env(): void
30+
{
31+
$this->assertFalse(Filesystem\is_file(root_path('.env')));
32+
$this->console->call(GenerateSigningKeyCommand::class)->assertSuccess();
33+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
34+
35+
$file = Filesystem\read_file(root_path('.env'));
36+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
37+
38+
$this->assertArrayHasKey('SIGNING_KEY', $env);
39+
$this->assertIsString($env['SIGNING_KEY']);
40+
}
41+
42+
public function test_updates_existing(): void
43+
{
44+
Filesystem\write_file(root_path('.env'), 'SIGNING_KEY=abc');
45+
$this->console->call(GenerateSigningKeyCommand::class)->assertSuccess();
46+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
47+
48+
$file = Filesystem\read_file(root_path('.env'));
49+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
50+
51+
$this->assertArrayHasKey('SIGNING_KEY', $env);
52+
$this->assertNotSame('abc', $env['SIGNING_KEY']);
53+
}
54+
55+
public function test_add_if_missing(): void
56+
{
57+
Filesystem\create_file(root_path('.env'));
58+
$this->console->call(GenerateSigningKeyCommand::class)->assertSuccess();
59+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
60+
61+
$file = Filesystem\read_file(root_path('.env'));
62+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
63+
64+
$this->assertArrayHasKey('SIGNING_KEY', $env);
65+
$this->assertIsString($env['SIGNING_KEY']);
66+
}
67+
68+
public function test_override_flag_true_replaces_existing_key(): void
69+
{
70+
Filesystem\write_file(root_path('.env'), 'SIGNING_KEY=original_key');
71+
$this->console->call(GenerateSigningKeyCommand::class, ['override' => true])->assertSuccess();
72+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
73+
74+
$file = Filesystem\read_file(root_path('.env'));
75+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
76+
77+
$this->assertArrayHasKey('SIGNING_KEY', $env);
78+
$this->assertNotSame('original_key', $env['SIGNING_KEY']);
79+
$this->assertIsString($env['SIGNING_KEY']);
80+
}
81+
82+
public function test_override_flag_false_preserves_existing_key(): void
83+
{
84+
Filesystem\write_file(root_path('.env'), 'SIGNING_KEY=original_key');
85+
$this->console->call(GenerateSigningKeyCommand::class, ['override' => false])->assertSuccess();
86+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
87+
88+
$file = Filesystem\read_file(root_path('.env'));
89+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
90+
91+
$this->assertArrayHasKey('SIGNING_KEY', $env);
92+
$this->assertSame('original_key', $env['SIGNING_KEY']);
93+
}
94+
95+
public function test_override_flag_false_still_adds_key_when_missing(): void
96+
{
97+
Filesystem\write_file(root_path('.env'), 'OTHER_VAR=value');
98+
$this->console->call(GenerateSigningKeyCommand::class, ['override' => false])->assertSuccess();
99+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
100+
101+
$file = Filesystem\read_file(root_path('.env'));
102+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
103+
104+
$this->assertArrayHasKey('SIGNING_KEY', $env);
105+
$this->assertArrayHasKey('OTHER_VAR', $env);
106+
107+
$this->assertSame('value', $env['OTHER_VAR']);
108+
$this->assertIsString($env['SIGNING_KEY']);
109+
}
110+
111+
public function test_override_flag_preserves_other_env_variables(): void
112+
{
113+
Filesystem\write_file(root_path('.env'), "APP_NAME=Tempest\nSIGNING_KEY=old_key\nDATABASE_URL=sqlite://db.sqlite");
114+
$this->console->call(GenerateSigningKeyCommand::class, ['override' => true])->assertSuccess();
115+
$this->assertTrue(Filesystem\is_file(root_path('.env')));
116+
117+
$file = Filesystem\read_file(root_path('.env'));
118+
$env = Dotenv::createImmutable(__DIR__)->parse($file);
119+
120+
$this->assertArrayHasKey('SIGNING_KEY', $env);
121+
$this->assertArrayHasKey('APP_NAME', $env);
122+
$this->assertArrayHasKey('DATABASE_URL', $env);
123+
124+
$this->assertSame('Tempest', $env['APP_NAME']);
125+
$this->assertSame('sqlite://db.sqlite', $env['DATABASE_URL']);
126+
127+
$this->assertNotSame('old_key', $env['SIGNING_KEY']);
128+
$this->assertIsString($env['SIGNING_KEY']);
129+
}
130+
}

tests/Integration/FrameworkIntegrationTestCase.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Tempest\Core\Application;
1616
use Tempest\Core\ShellExecutor;
1717
use Tempest\Core\ShellExecutors\NullShellExecutor;
18-
use Tempest\Cryptography\CreateSigningKeyCommand;
1918
use Tempest\Database\DatabaseInitializer;
2019
use Tempest\Database\Migrations\MigrationManager;
2120
use Tempest\Discovery\DiscoveryLocation;
@@ -62,9 +61,6 @@ protected function setUp(): void
6261
->removeInitializer(DatabaseInitializer::class)
6362
->addInitializer(TestingDatabaseInitializer::class);
6463

65-
// Generate signing key required for tests
66-
$this->console->call(CreateSigningKeyCommand::class);
67-
6864
$databaseConfigPath = __DIR__ . '/../Fixtures/Config/database.config.php';
6965

7066
if (! file_exists($databaseConfigPath)) {

tests/Integration/Route/ClientTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ protected function setUp(): void
2727
{
2828
parent::setUp();
2929

30-
new Process([root_path('tempest'), 'key:generate'])->run();
31-
3230
$this->server = new Process([root_path('tempest'), 'serve', 'localhost', '8088']);
3331
$this->server->start();
3432

@@ -42,7 +40,7 @@ protected function setUp(): void
4240
protected function tearDown(): void
4341
{
4442
if ($this->server->isRunning()) {
45-
$this->server->stop(2);
43+
$this->server->stop(timeout: 10);
4644
}
4745

4846
parent::tearDown();

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
require_once __DIR__ . '/../vendor/autoload.php';
88

9+
passthru('php tempest key:generate --no-override --no-interaction');
910
echo PHP_EOL;
1011
passthru('php tempest discovery:generate --no-interaction');
1112
echo PHP_EOL;

0 commit comments

Comments
 (0)