Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $container->withWait(new WaitForLog('Ready to accept connections'));


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

// Wait for all bound ports to be open
$container->withWait(new WaitForHostPort());
Expand Down
3 changes: 3 additions & 0 deletions src/Container/StartedGenericContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Wait/WaitForHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
29 changes: 28 additions & 1 deletion tests/Integration/GenericContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use Testcontainers\Container\GenericContainer;
use Testcontainers\Wait\WaitForHostPort;
use Testcontainers\Wait\WaitForHttp;

class GenericContainerTest extends TestCase
{
Expand Down Expand Up @@ -111,7 +112,7 @@ public function testShouldCopyFileWithPermissions(): void
$container->stop();
}

public function testShouldReturnFirstMappedPort(): void
public function testShouldReturnFirstMappedPortWithWaitForHostPort(): void
{
$container = (new GenericContainer('nginx'))
->withExposedPorts(80)
Expand All @@ -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 = [
Expand Down