diff --git a/README.md b/README.md index 9746ea1..79b99b8 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,8 @@ $container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']), // Wait until that message is in the logs $container->withWait(new WaitForLog('Ready to accept connections')); - -// Wait for an HTTP request to succeed -$container->withWait(new WaitForHttp($port, $method = 'GET', $path = '/')); +// Wait for an http request to succeed +$container->withWait((new WaitForHttp($port))->withMethod('GET')->withPath('/')); // Wait for all bound ports to be open $container->withWait(new WaitForHostPort()); diff --git a/src/Container/StartedGenericContainer.php b/src/Container/StartedGenericContainer.php index 5ae3f82..6e2acdd 100644 --- a/src/Container/StartedGenericContainer.php +++ b/src/Container/StartedGenericContainer.php @@ -98,6 +98,9 @@ public function logs(): string ?->getBody() ->getContents() ?? ''; + /** + * @var string|false $converted + */ $converted = mb_convert_encoding($output, 'UTF-8', 'UTF-8'); return $this->sanitizeOutput($converted == false ? $output : $converted); } diff --git a/src/Wait/WaitForHttp.php b/src/Wait/WaitForHttp.php index 855c55f..31d4c08 100644 --- a/src/Wait/WaitForHttp.php +++ b/src/Wait/WaitForHttp.php @@ -147,8 +147,8 @@ private function makeHttpRequest(string $url): int private function resolvePort(StartedTestContainer $container): void { - if ($this->port === null) { - $this->port = $container->getFirstMappedPort(); - } + $this->port = $this->port === null + ? $container->getFirstMappedPort() + : $container->getMappedPort($this->port); } } diff --git a/tests/Integration/GenericContainerTest.php b/tests/Integration/GenericContainerTest.php index 7ed65bb..0b4188f 100644 --- a/tests/Integration/GenericContainerTest.php +++ b/tests/Integration/GenericContainerTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use Testcontainers\Container\GenericContainer; use Testcontainers\Wait\WaitForHostPort; +use Testcontainers\Wait\WaitForHttp; class GenericContainerTest extends TestCase { @@ -111,7 +112,7 @@ public function testShouldCopyFileWithPermissions(): void $container->stop(); } - public function testShouldReturnFirstMappedPort(): void + public function testShouldReturnFirstMappedPortWithWaitForHostPort(): void { $container = (new GenericContainer('nginx')) ->withExposedPorts(80) @@ -124,6 +125,32 @@ public function testShouldReturnFirstMappedPort(): void $container->stop(); } + public function testShouldReturnFirstMappedPortWithWaitForHttp(): void + { + $container = (new GenericContainer('nginx')) + ->withExposedPorts(80) + ->withWait((new WaitForHttp())->withMethod('GET')->withPath('/')) + ->start(); + $firstMappedPort = $container->getFirstMappedPort(); + + self::assertSame($firstMappedPort, $container->getMappedPort(80)); + + $container->stop(); + } + + public function testShouldReturnMappedPortWithWaitForHttp(): void + { + $container = (new GenericContainer('nginx')) + ->withExposedPorts(80) + ->withWait((new WaitForHttp(80))->withMethod('GET')->withPath('/')) + ->start(); + $mappedPort = $container->getMappedPort(80); + + self::assertSame($mappedPort, $container->getFirstMappedPort()); + + $container->stop(); + } + public function testShouldSetLabels(): void { $labels = [