|
3 | 3 | namespace Tests\Tempest\Integration\Database; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\Attributes\Test; |
| 6 | +use ReflectionMethod; |
| 7 | +use Tempest\Database\Config\MysqlConfig; |
6 | 8 | use Tempest\Database\Config\SQLiteConfig; |
7 | 9 | use Tempest\Database\Connection\Connection; |
| 10 | +use Tempest\Database\Connection\PDOConnection; |
8 | 11 | use Tempest\Database\Database; |
9 | 12 | use Tempest\Database\DatabaseInitializer; |
10 | 13 | use Tempest\Database\GenericDatabase; |
@@ -111,6 +114,58 @@ public function test_it_does_not_reuse_a_persistent_connection_for_the_same_tag_ |
111 | 114 | $this->assertSame($second->connection, $this->container->get(Connection::class, 'main')); |
112 | 115 | } |
113 | 116 |
|
| 117 | + #[Test] |
| 118 | + public function test_it_does_not_reuse_a_persistent_connection_when_only_the_password_differs(): void |
| 119 | + { |
| 120 | + $initializer = new DatabaseInitializer(); |
| 121 | + $method = new ReflectionMethod($initializer, 'getConnectionKey'); |
| 122 | + |
| 123 | + $first = new MysqlConfig( |
| 124 | + host: 'localhost', |
| 125 | + username: 'tempest', |
| 126 | + password: 'first-password', // @mago-expect lint:no-literal-password |
| 127 | + database: 'tempest', |
| 128 | + persistent: true, |
| 129 | + tag: 'main', |
| 130 | + ); |
| 131 | + |
| 132 | + $second = new MysqlConfig( |
| 133 | + host: 'localhost', |
| 134 | + username: 'tempest', |
| 135 | + password: 'second-password', // @mago-expect lint:no-literal-password |
| 136 | + database: 'tempest', |
| 137 | + persistent: true, |
| 138 | + tag: 'main', |
| 139 | + ); |
| 140 | + |
| 141 | + $this->assertNotSame( |
| 142 | + $method->invoke($initializer, $first), |
| 143 | + $method->invoke($initializer, $second), |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + #[Test] |
| 148 | + public function test_it_reconnects_a_stale_persistent_connection_when_reusing_it(): void |
| 149 | + { |
| 150 | + $this->configureSqliteDatabase('main', 'stale-persistent-main.sqlite'); |
| 151 | + |
| 152 | + $first = $this->container->get(Database::class, 'main'); |
| 153 | + |
| 154 | + $this->assertInstanceOf(GenericDatabase::class, $first); |
| 155 | + $this->assertInstanceOf(PDOConnection::class, $first->connection); |
| 156 | + |
| 157 | + $first->connection->close(); |
| 158 | + |
| 159 | + $this->container->unregister(Database::class, tagged: true); |
| 160 | + $this->container->unregister(Connection::class, tagged: true); |
| 161 | + |
| 162 | + $second = $this->container->get(Database::class, 'main'); |
| 163 | + |
| 164 | + $this->assertInstanceOf(GenericDatabase::class, $second); |
| 165 | + $this->assertSame($first->connection, $second->connection); |
| 166 | + $this->assertTrue($second->connection->ping()); |
| 167 | + } |
| 168 | + |
114 | 169 | private function configureSqliteDatabase(string $tag, string $filename, bool $persistent = true): void |
115 | 170 | { |
116 | 171 | $path = $this->databasePath($filename); |
|
0 commit comments