|
2 | 2 |
|
3 | 3 | namespace Tests\Tempest\Integration\Database; |
4 | 4 |
|
| 5 | +use PDO; |
| 6 | +use PDOStatement; |
5 | 7 | use PHPUnit\Framework\Attributes\Test; |
6 | 8 | use ReflectionMethod; |
| 9 | +use RuntimeException; |
| 10 | +use Tempest\Database\Config\DatabaseConfig; |
| 11 | +use Tempest\Database\Config\DatabaseDialect; |
7 | 12 | use Tempest\Database\Config\MysqlConfig; |
8 | 13 | use Tempest\Database\Config\SQLiteConfig; |
9 | 14 | use Tempest\Database\Connection\Connection; |
| 15 | +use Tempest\Database\Connection\ConnectionInitializer; |
10 | 16 | use Tempest\Database\Connection\PDOConnection; |
11 | 17 | use Tempest\Database\Database; |
12 | 18 | use Tempest\Database\DatabaseInitializer; |
13 | 19 | use Tempest\Database\GenericDatabase; |
| 20 | +use Tempest\Database\Query; |
14 | 21 | use Tempest\Framework\Testing\TestingDatabaseInitializer; |
15 | 22 | use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
16 | 23 |
|
@@ -47,6 +54,146 @@ public function it_resolves_multiple_persistent_connections_by_tag(): void |
47 | 54 | $this->assertSame($backup->connection, $this->container->get(Connection::class, 'backup')); |
48 | 55 | } |
49 | 56 |
|
| 57 | + #[Test] |
| 58 | + public function it_resolves_tagged_connections_before_databases_without_sharing_them(): void |
| 59 | + { |
| 60 | + $this->configureSqliteDatabase('main', 'connection-first-main.sqlite'); |
| 61 | + $this->configureSqliteDatabase('backup', 'connection-first-backup.sqlite'); |
| 62 | + |
| 63 | + $mainConnection = $this->container->get(Connection::class, 'main'); |
| 64 | + $backupConnection = $this->container->get(Connection::class, 'backup'); |
| 65 | + |
| 66 | + $this->assertInstanceOf(PDOConnection::class, $mainConnection); |
| 67 | + $this->assertInstanceOf(PDOConnection::class, $backupConnection); |
| 68 | + $this->assertNotSame($mainConnection, $backupConnection); |
| 69 | + $main = $this->container->get(Database::class, 'main'); |
| 70 | + $backup = $this->container->get(Database::class, 'backup'); |
| 71 | + $this->assertInstanceOf(GenericDatabase::class, $main); |
| 72 | + $this->assertInstanceOf(GenericDatabase::class, $backup); |
| 73 | + $this->assertSame($mainConnection, $main->connection); |
| 74 | + $this->assertSame($backupConnection, $backup->connection); |
| 75 | + $this->assertSame('main', $main->tag); |
| 76 | + $this->assertSame('backup', $backup->tag); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + public function it_honours_an_injected_connection_when_resolving_database_first(): void |
| 81 | + { |
| 82 | + $this->configureSqliteDatabase('main', 'injected-database-first.sqlite'); |
| 83 | + $connection = $this->createMock(Connection::class); |
| 84 | + $this->container->singleton(Connection::class, $connection, 'main'); |
| 85 | + |
| 86 | + $database = $this->container->get(Database::class, 'main'); |
| 87 | + |
| 88 | + $this->assertInstanceOf(GenericDatabase::class, $database); |
| 89 | + $this->assertSame($connection, $database->connection); |
| 90 | + $this->assertSame($connection, $this->container->get(Connection::class, 'main')); |
| 91 | + $this->assertSame(DatabaseDialect::SQLITE, $database->dialect); |
| 92 | + $this->assertSame('main', $database->tag); |
| 93 | + } |
| 94 | + |
| 95 | + #[Test] |
| 96 | + public function it_honours_an_untagged_injected_connection(): void |
| 97 | + { |
| 98 | + $connection = $this->createMock(Connection::class); |
| 99 | + $this->container->unregister(Database::class); |
| 100 | + $this->container->singleton(Connection::class, $connection); |
| 101 | + |
| 102 | + $database = $this->container->get(Database::class); |
| 103 | + |
| 104 | + $this->assertInstanceOf(GenericDatabase::class, $database); |
| 105 | + $this->assertSame($connection, $database->connection); |
| 106 | + $this->assertSame($connection, $this->container->get(Connection::class)); |
| 107 | + $this->assertSame($this->container->get(DatabaseConfig::class)->dialect, $database->dialect); |
| 108 | + $this->assertSame($this->container->get(DatabaseConfig::class)->tag, $database->tag); |
| 109 | + } |
| 110 | + |
| 111 | + #[Test] |
| 112 | + public function it_keeps_injected_tagged_connections_separate(): void |
| 113 | + { |
| 114 | + $this->configureSqliteDatabase('main', 'injected-main.sqlite'); |
| 115 | + $this->configureSqliteDatabase('backup', 'injected-backup.sqlite'); |
| 116 | + |
| 117 | + $mainConnection = $this->createMock(Connection::class); |
| 118 | + $backupConnection = $this->createMock(Connection::class); |
| 119 | + $this->container->singleton(Connection::class, $mainConnection, 'main'); |
| 120 | + $this->container->singleton(Connection::class, $backupConnection, 'backup'); |
| 121 | + |
| 122 | + $main = $this->container->get(Database::class, 'main'); |
| 123 | + $backup = $this->container->get(Database::class, 'backup'); |
| 124 | + $this->assertInstanceOf(GenericDatabase::class, $main); |
| 125 | + $this->assertInstanceOf(GenericDatabase::class, $backup); |
| 126 | + $this->assertSame($mainConnection, $main->connection); |
| 127 | + $this->assertSame($backupConnection, $backup->connection); |
| 128 | + $this->assertSame('main', $main->tag); |
| 129 | + $this->assertSame('backup', $backup->tag); |
| 130 | + } |
| 131 | + |
| 132 | + #[Test] |
| 133 | + public function it_honours_an_injected_connection_when_resolving_connection_first(): void |
| 134 | + { |
| 135 | + $this->configureSqliteDatabase('main', 'injected-connection-first.sqlite'); |
| 136 | + $connection = $this->createMock(Connection::class); |
| 137 | + $this->container->singleton(Connection::class, $connection, 'main'); |
| 138 | + |
| 139 | + $this->assertSame($connection, $this->container->get(Connection::class, 'main')); |
| 140 | + |
| 141 | + $database = $this->container->get(Database::class, 'main'); |
| 142 | + |
| 143 | + $this->assertInstanceOf(GenericDatabase::class, $database); |
| 144 | + $this->assertSame($connection, $database->connection); |
| 145 | + } |
| 146 | + |
| 147 | + #[Test] |
| 148 | + public function it_uses_the_injected_connection_in_an_externally_managed_transaction(): void |
| 149 | + { |
| 150 | + $this->configureSqliteDatabase('main', 'external-transaction.sqlite'); |
| 151 | + |
| 152 | + $pdo = new PDO('sqlite::memory:'); |
| 153 | + $pdo->exec('CREATE TABLE outbox (message TEXT)'); |
| 154 | + |
| 155 | + $connection = $this->createMock(Connection::class); |
| 156 | + $connection |
| 157 | + ->method('prepare') |
| 158 | + ->willReturnCallback( |
| 159 | + fn (string $sql): PDOStatement => $pdo->prepare($sql) ?: throw new RuntimeException('Could not prepare SQL statement.'), |
| 160 | + ); |
| 161 | + $connection->expects($this->never())->method('connect'); |
| 162 | + $connection->expects($this->never())->method('reconnect'); |
| 163 | + $connection->expects($this->never())->method('ping'); |
| 164 | + |
| 165 | + $this->container->singleton(Connection::class, $connection, 'main'); |
| 166 | + $database = $this->container->get(Database::class, 'main'); |
| 167 | + |
| 168 | + $pdo->beginTransaction(); |
| 169 | + $database->execute(new Query("INSERT INTO outbox (message) VALUES ('application')")); |
| 170 | + $pdo->exec("INSERT INTO outbox (message) VALUES ('messenger')"); |
| 171 | + |
| 172 | + $this->assertCount(2, $database->fetch(new Query('SELECT * FROM outbox'))); |
| 173 | + |
| 174 | + $pdo->rollBack(); |
| 175 | + |
| 176 | + $this->assertSame([], $database->fetch(new Query('SELECT * FROM outbox'))); |
| 177 | + } |
| 178 | + |
| 179 | + #[Test] |
| 180 | + public function the_testing_initializer_also_honours_an_injected_connection(): void |
| 181 | + { |
| 182 | + $this->container |
| 183 | + ->removeInitializer(DatabaseInitializer::class) |
| 184 | + ->addInitializer(TestingDatabaseInitializer::class); |
| 185 | + |
| 186 | + $this->configureSqliteDatabase('main', 'injected-testing.sqlite'); |
| 187 | + $connection = $this->createMock(Connection::class); |
| 188 | + $this->container->singleton(Connection::class, $connection, 'main'); |
| 189 | + |
| 190 | + $database = $this->container->get(Database::class, 'main'); |
| 191 | + |
| 192 | + $this->assertInstanceOf(GenericDatabase::class, $database); |
| 193 | + $this->assertSame($connection, $database->connection); |
| 194 | + $this->assertSame('main', $database->tag); |
| 195 | + } |
| 196 | + |
50 | 197 | #[Test] |
51 | 198 | public function it_reuses_a_persistent_connection_for_the_same_connection_config(): void |
52 | 199 | { |
@@ -117,7 +264,7 @@ public function it_does_not_reuse_a_persistent_connection_for_the_same_tag_with_ |
117 | 264 | #[Test] |
118 | 265 | public function it_does_not_reuse_a_persistent_connection_when_only_the_password_differs(): void |
119 | 266 | { |
120 | | - $initializer = new DatabaseInitializer(); |
| 267 | + $initializer = new ConnectionInitializer(); |
121 | 268 | $method = new ReflectionMethod($initializer, 'getConnectionKey'); |
122 | 269 |
|
123 | 270 | $first = new MysqlConfig( |
|
0 commit comments