Skip to content

Commit 21ab447

Browse files
committed
Make tests run.
1 parent bba6356 commit 21ab447

2 files changed

Lines changed: 41 additions & 24 deletions

File tree

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"scripts": {
2727
"analyze": "vendor/bin/ecs check",
2828
"format": "vendor/bin/ecs check --fix",
29-
"qa": [ "composer analyze", "composer test" ],
29+
"qa": [
30+
"composer analyze",
31+
"composer test"
32+
],
3033
"test": "vendor/bin/phpunit"
3134
},
3235
"config": {

src/Container.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
namespace Thenativeweb\Eventsourcingdb;
44

5-
use Symfony\Component\Process\Process;
5+
use GuzzleHttp\Client as HttpClient;
6+
use GuzzleHttp\Exception\GuzzleException;
67
use Testcontainers\Container\GenericContainer;
7-
use Testcontainers\DockerClient\DockerClient;
8-
use Testcontainers\DockerClient\DockerClientFactory;
9-
use Testcontainers\Wait\WaitForHttp;
8+
use Testcontainers\Container\StartedGenericContainer;
109

1110
class Container
1211
{
1312
private string $imageName = 'thenativeweb/eventsourcingdb';
1413
private string $imageTag;
1514
private int $internalPort = 3000;
1615
private string $apiToken = 'secret';
17-
private ?GenericContainer $container = null;
16+
private ?StartedGenericContainer $container = null;
17+
private HttpClient $httpClient;
1818

1919
public function __construct()
2020
{
2121
$this->imageTag = $this->getImageVersionFromDockerfile();
22+
$this->httpClient = new HttpClient([
23+
'http_errors' => false
24+
]);
2225
}
2326

2427
public function withImageTag(string $tag): self
@@ -41,24 +44,35 @@ public function withPort(int $port): self
4144

4245
public function start(): void
4346
{
44-
$this->container = new GenericContainer("{$this->imageName}:{$this->imageTag}");
45-
46-
$this->container
47-
->withExposedPorts($this->internalPort)
48-
->withCommand([
49-
'run',
50-
'--api-token', $this->apiToken,
51-
'--data-directory-temporary',
52-
'--http-enabled',
53-
'--https-enabled=false'
54-
])
55-
->withWait(
56-
new WaitForHttp($this->internalPort)
57-
->withMethod('GET')
58-
->withPath('/api/v1/ping')
59-
);
60-
61-
$this->container->start();
47+
$container =
48+
new GenericContainer("{$this->imageName}:{$this->imageTag}")
49+
->withExposedPorts($this->internalPort)
50+
->withCommand([
51+
'run',
52+
'--api-token', $this->apiToken,
53+
'--data-directory-temporary',
54+
'--http-enabled',
55+
'--https-enabled=false'
56+
]);
57+
58+
$this->container = $container->start();
59+
60+
$baseUrl = rtrim($this->getBaseUrl());
61+
$pingUrl = $baseUrl . '/api/v1/ping';
62+
63+
while (true) {
64+
try {
65+
$response = $this->httpClient->get($pingUrl);
66+
} catch (GuzzleException $e) {
67+
usleep(100_000);
68+
continue;
69+
}
70+
$status = $response->getStatusCode();
71+
72+
if ($status === 200) {
73+
break;
74+
}
75+
}
6276
}
6377

6478
public function getHost(): string

0 commit comments

Comments
 (0)