|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Tempest\Integration\Cryptography; |
| 4 | + |
| 5 | +use Dotenv\Dotenv; |
| 6 | +use Tempest\Core\FrameworkKernel; |
| 7 | +use Tempest\Cryptography\CreateSigningKeyCommand; |
| 8 | +use Tempest\Support\Filesystem; |
| 9 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 10 | + |
| 11 | +use function Tempest\root_path; |
| 12 | + |
| 13 | +final class CreateSigningKeyCommandTest 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(CreateSigningKeyCommand::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(CreateSigningKeyCommand::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(CreateSigningKeyCommand::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 | +} |
0 commit comments