packages/container/tests/Unit/PostgreSqlConfigurationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit;
6
7use Container\PostgreSqlConfiguration;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\TestCase;
10use Testcontainers\Containers\ContainerInstance;
11use Testcontainers\Containers\GenericContainer\GenericContainer;
12use Testcontainers\Containers\Types\Mount;
13use Testcontainers\Containers\WaitStrategy\LogMessageWaitStrategy;
14use Testcontainers\Containers\WaitStrategy\WaitStrategy;
15
16#[CoversClass(PostgreSqlConfiguration::class)]
17final class PostgreSqlConfigurationTest extends TestCase
18{
19 /**
20 * @throws \Testcontainers\Exceptions\InvalidFormatException
21 */
22 public function testApplyConfiguresTheDatabaseStartupContract(): void
23 {
24 $container = new class ('database:version') extends GenericContainer {
25 /**
26 * @return array{ports: array<int>, env: array<string, string>, mounts: array<Mount>, timeout: int|null, retries: int, remove: bool, wait: WaitStrategy|null}
27 * @throws \Testcontainers\Exceptions\InvalidFormatException
28 */
29 public function launchOptions(ContainerInstance $instance): array
30 {
31 return [
32 'ports' => $this->exposedPorts(),
33 'env' => $this->env(),
34 'mounts' => $this->mounts(),
35 'timeout' => $this->startupTimeout(),
36 'retries' => $this->startupConflictRetryAttempts(),
37 'remove' => $this->autoRemoveOnExit(),
38 'wait' => $this->waitStrategy($instance),
39 ];
40 }
41 };
42 (new PostgreSqlConfiguration())->apply($container);
43 $options = $container->launchOptions(self::createStub(ContainerInstance::class));
44 self::assertTrue($container->reuseMode()->isReuse());
45 self::assertSame([5432], $options['ports']);
46 self::assertSame(['POSTGRES_USER' => 'test', 'POSTGRES_PASSWORD' => 'test', 'POSTGRES_DB' => 'test'], $options['env']);
47 self::assertEquals([], $options['mounts']);
48 self::assertSame(300, $options['timeout']);
49 self::assertSame(10, $options['retries']);
50 self::assertTrue($options['remove']);
51 self::assertEquals((new LogMessageWaitStrategy())->withPattern('\\[1\\].*database system is ready to accept connections')->withTimeoutSeconds(120), $options['wait']);
52 }
53}
54