diff --git a/src/Container/Container.php b/src/Container/Container.php index 46776c1..634e159 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -9,9 +9,12 @@ use Testcontainer\Registry; use Testcontainer\Wait\WaitForNothing; use Testcontainer\Wait\WaitInterface; +use UnexpectedValueException; /** - * @phpstan-type ContainerInspect array{0: array{NetworkSettings: array{IPAddress: string}}} + * @phpstan-type ContainerInspectSingleNetwork array + * @phpstan-type ContainerInspectMultipleNetworks array}}> + * @phpstan-type ContainerInspect ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks */ class Container { @@ -165,7 +168,7 @@ public function run(bool $wait = true): self $inspect = new Process(['docker', 'inspect', $this->id]); $inspect->mustRun(); - /** @var ContainerInspect $inspectedData */ + /** @var ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks $inspectedData */ $inspectedData = json_decode($inspect->getOutput(), true, 512, JSON_THROW_ON_ERROR); $this->inspectedData = $inspectedData; @@ -256,10 +259,20 @@ public function logs(): string public function getAddress(): string { - if ($this->network !== null && !empty($this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'])) { - return $this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress']; + if (is_string($this->network)) { + $containerAddress = $this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'] ?? null; + + if (is_string($containerAddress)) { + return $containerAddress; + } + } + + $containerAddress = $this->inspectedData[0]['NetworkSettings']['IPAddress'] ?? null; + + if (is_string($containerAddress)) { + return $containerAddress; } - return $this->inspectedData[0]['NetworkSettings']['IPAddress']; + throw new UnexpectedValueException('Unable to find container IP address'); } } diff --git a/src/Wait/WaitForTcpPortOpen.php b/src/Wait/WaitForTcpPortOpen.php index 14946ba..3c574cf 100644 --- a/src/Wait/WaitForTcpPortOpen.php +++ b/src/Wait/WaitForTcpPortOpen.php @@ -10,7 +10,7 @@ use Testcontainer\Exception\ContainerNotReadyException; /** - * @phpstan-type ContainerInspect array + * @phpstan-import-type ContainerInspect from \Testcontainer\Container\Container */ final class WaitForTcpPortOpen implements WaitInterface {