|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Tempest\Integration\Database; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\Test; |
| 6 | +use Tempest\Database\Config\SQLiteConfig; |
| 7 | +use Tempest\Database\Connection\Connection; |
| 8 | +use Tempest\Database\Database; |
| 9 | +use Tempest\Database\DatabaseInitializer; |
| 10 | +use Tempest\Database\GenericDatabase; |
| 11 | +use Tempest\Framework\Testing\TestingDatabaseInitializer; |
| 12 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 13 | + |
| 14 | +final class DatabaseInitializerTest extends FrameworkIntegrationTestCase |
| 15 | +{ |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + |
| 20 | + $this->container |
| 21 | + ->removeInitializer(TestingDatabaseInitializer::class) |
| 22 | + ->addInitializer(DatabaseInitializer::class); |
| 23 | + } |
| 24 | + |
| 25 | + #[Test] |
| 26 | + public function test_it_resolves_multiple_persistent_connections_by_tag(): void |
| 27 | + { |
| 28 | + $this->configureSqliteDatabase('main', 'multi-main.sqlite'); |
| 29 | + $this->configureSqliteDatabase('backup', 'multi-backup.sqlite'); |
| 30 | + |
| 31 | + $main = $this->container->get(Database::class, 'main'); |
| 32 | + $backup = $this->container->get(Database::class, 'backup'); |
| 33 | + |
| 34 | + $this->assertInstanceOf(GenericDatabase::class, $main); |
| 35 | + $this->assertInstanceOf(GenericDatabase::class, $backup); |
| 36 | + |
| 37 | + $this->assertNotSame($main->connection, $backup->connection); |
| 38 | + $this->assertSame($this->databasePath('multi-main.sqlite'), $main->connection->config->path); |
| 39 | + $this->assertSame($this->databasePath('multi-backup.sqlite'), $backup->connection->config->path); |
| 40 | + |
| 41 | + $this->assertSame($main->connection, $this->container->get(Connection::class, 'main')); |
| 42 | + $this->assertSame($backup->connection, $this->container->get(Connection::class, 'backup')); |
| 43 | + } |
| 44 | + |
| 45 | + #[Test] |
| 46 | + public function test_it_reuses_a_persistent_connection_for_the_same_connection_config(): void |
| 47 | + { |
| 48 | + $this->configureSqliteDatabase('main', 'persistent-main.sqlite'); |
| 49 | + |
| 50 | + $first = $this->container->get(Database::class, 'main'); |
| 51 | + |
| 52 | + $this->container->unregister(Database::class, tagged: true); |
| 53 | + $this->container->unregister(Connection::class, tagged: true); |
| 54 | + |
| 55 | + $second = $this->container->get(Database::class, 'main'); |
| 56 | + |
| 57 | + $this->assertInstanceOf(GenericDatabase::class, $first); |
| 58 | + $this->assertInstanceOf(GenericDatabase::class, $second); |
| 59 | + |
| 60 | + $this->assertSame($first->connection, $second->connection); |
| 61 | + $this->assertSame($second->connection, $this->container->get(Connection::class, 'main')); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function test_it_does_not_reuse_a_non_persistent_connection_for_the_same_connection_config(): void |
| 66 | + { |
| 67 | + $this->configureSqliteDatabase('main', 'non-persistent-main.sqlite', persistent: false); |
| 68 | + |
| 69 | + $first = $this->container->get(Database::class, 'main'); |
| 70 | + |
| 71 | + $this->container->unregister(Database::class, tagged: true); |
| 72 | + $this->container->unregister(Connection::class, tagged: true); |
| 73 | + |
| 74 | + $second = $this->container->get(Database::class, 'main'); |
| 75 | + |
| 76 | + $this->assertInstanceOf(GenericDatabase::class, $first); |
| 77 | + $this->assertInstanceOf(GenericDatabase::class, $second); |
| 78 | + |
| 79 | + $this->assertNotSame($first->connection, $second->connection); |
| 80 | + $this->assertSame($this->databasePath('non-persistent-main.sqlite'), $first->connection->config->path); |
| 81 | + $this->assertSame($this->databasePath('non-persistent-main.sqlite'), $second->connection->config->path); |
| 82 | + $this->assertSame($second->connection, $this->container->get(Connection::class, 'main')); |
| 83 | + } |
| 84 | + |
| 85 | + #[Test] |
| 86 | + public function test_it_does_not_reuse_a_persistent_connection_for_the_same_tag_with_a_different_connection_config(): void |
| 87 | + { |
| 88 | + $this->configureSqliteDatabase('main', 'first-main.sqlite'); |
| 89 | + |
| 90 | + $first = $this->container->get(Database::class, 'main'); |
| 91 | + |
| 92 | + $this->container->unregister(Database::class, tagged: true); |
| 93 | + $this->container->unregister(Connection::class, tagged: true); |
| 94 | + |
| 95 | + $this->configureSqliteDatabase('main', 'second-main.sqlite'); |
| 96 | + |
| 97 | + $second = $this->container->get(Database::class, 'main'); |
| 98 | + |
| 99 | + $this->assertInstanceOf(GenericDatabase::class, $first); |
| 100 | + $this->assertInstanceOf(GenericDatabase::class, $second); |
| 101 | + |
| 102 | + $this->assertNotSame($first->connection, $second->connection); |
| 103 | + $this->assertSame($this->databasePath('first-main.sqlite'), $first->connection->config->path); |
| 104 | + $this->assertSame($this->databasePath('second-main.sqlite'), $second->connection->config->path); |
| 105 | + $this->assertSame($second->connection, $this->container->get(Connection::class, 'main')); |
| 106 | + } |
| 107 | + |
| 108 | + private function configureSqliteDatabase(string $tag, string $filename, bool $persistent = true): void |
| 109 | + { |
| 110 | + $path = $this->databasePath($filename); |
| 111 | + |
| 112 | + if (is_file($path)) { |
| 113 | + unlink($path); |
| 114 | + } |
| 115 | + |
| 116 | + $this->container->config(new SQLiteConfig( |
| 117 | + path: $path, |
| 118 | + persistent: $persistent, |
| 119 | + tag: $tag, |
| 120 | + )); |
| 121 | + } |
| 122 | + |
| 123 | + private function databasePath(string $filename): string |
| 124 | + { |
| 125 | + return $this->internalStorage . '/' . $filename; |
| 126 | + } |
| 127 | +} |
0 commit comments