22
33namespace Thenativeweb \Eventsourcingdb ;
44
5- use Symfony \Component \Process \Process ;
5+ use GuzzleHttp \Client as HttpClient ;
6+ use GuzzleHttp \Exception \GuzzleException ;
67use Testcontainers \Container \GenericContainer ;
7- use Testcontainers \DockerClient \DockerClient ;
8- use Testcontainers \DockerClient \DockerClientFactory ;
9- use Testcontainers \Wait \WaitForHttp ;
8+ use Testcontainers \Container \StartedGenericContainer ;
109
1110class 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