Skip to content

Commit 6c5f6b2

Browse files
authored
Merge pull request #46 from wundii/main
feat: add dynamic port resolution for WaitForHttp
2 parents b77cd2b + 69db0ed commit 6c5f6b2

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ $container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']),
6161
// Wait until that message is in the logs
6262
$container->withWait(new WaitForLog('Ready to accept connections'));
6363

64-
65-
// Wait for an HTTP request to succeed
66-
$container->withWait(new WaitForHttp($port, $method = 'GET', $path = '/'));
64+
// Wait for an http request to succeed
65+
$container->withWait((new WaitForHttp($port))->withMethod('GET')->withPath('/'));
6766

6867
// Wait for all bound ports to be open
6968
$container->withWait(new WaitForHostPort());

src/Container/StartedGenericContainer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public function logs(): string
9898
?->getBody()
9999
->getContents() ?? '';
100100

101+
/**
102+
* @var string|false $converted
103+
*/
101104
$converted = mb_convert_encoding($output, 'UTF-8', 'UTF-8');
102105
return $this->sanitizeOutput($converted == false ? $output : $converted);
103106
}

src/Wait/WaitForHttp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ private function makeHttpRequest(string $url): int
147147

148148
private function resolvePort(StartedTestContainer $container): void
149149
{
150-
if ($this->port === null) {
151-
$this->port = $container->getFirstMappedPort();
152-
}
150+
$this->port = $this->port === null
151+
? $container->getFirstMappedPort()
152+
: $container->getMappedPort($this->port);
153153
}
154154
}

tests/Integration/GenericContainerTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99
use Testcontainers\Container\GenericContainer;
1010
use Testcontainers\Wait\WaitForHostPort;
11+
use Testcontainers\Wait\WaitForHttp;
1112

1213
class GenericContainerTest extends TestCase
1314
{
@@ -111,7 +112,7 @@ public function testShouldCopyFileWithPermissions(): void
111112
$container->stop();
112113
}
113114

114-
public function testShouldReturnFirstMappedPort(): void
115+
public function testShouldReturnFirstMappedPortWithWaitForHostPort(): void
115116
{
116117
$container = (new GenericContainer('nginx'))
117118
->withExposedPorts(80)
@@ -124,6 +125,32 @@ public function testShouldReturnFirstMappedPort(): void
124125
$container->stop();
125126
}
126127

128+
public function testShouldReturnFirstMappedPortWithWaitForHttp(): void
129+
{
130+
$container = (new GenericContainer('nginx'))
131+
->withExposedPorts(80)
132+
->withWait((new WaitForHttp())->withMethod('GET')->withPath('/'))
133+
->start();
134+
$firstMappedPort = $container->getFirstMappedPort();
135+
136+
self::assertSame($firstMappedPort, $container->getMappedPort(80));
137+
138+
$container->stop();
139+
}
140+
141+
public function testShouldReturnMappedPortWithWaitForHttp(): void
142+
{
143+
$container = (new GenericContainer('nginx'))
144+
->withExposedPorts(80)
145+
->withWait((new WaitForHttp(80))->withMethod('GET')->withPath('/'))
146+
->start();
147+
$mappedPort = $container->getMappedPort(80);
148+
149+
self::assertSame($mappedPort, $container->getFirstMappedPort());
150+
151+
$container->stop();
152+
}
153+
127154
public function testShouldSetLabels(): void
128155
{
129156
$labels = [

0 commit comments

Comments
 (0)