55namespace Docker \Tests ;
66
77use Docker \DockerClientFactory ;
8+ use Http \Client \Common \Plugin \AddPathPlugin ;
9+ use Http \Client \Common \PluginClient ;
10+ use Http \Client \Common \PluginClientFactory ;
811use Psr \Http \Client \ClientInterface ;
912
1013class DockerClientFactoryTest extends TestCase
1114{
1215 protected function tearDown (): void
1316 {
1417 parent ::tearDown ();
18+ PluginClientFactory::setFactory (static function ($ client , array $ plugins , array $ options ): PluginClient {
19+ unset($ options ['client_name ' ]);
20+
21+ return new PluginClient ($ client , $ plugins , $ options );
22+ });
23+ putenv ('DOCKER_API_VERSION ' );
1524 putenv ('DOCKER_TLS_VERIFY ' );
1625 }
1726
@@ -20,6 +29,32 @@ public function testStaticConstructor(): void
2029 $ this ->assertInstanceOf (ClientInterface::class, DockerClientFactory::create ());
2130 }
2231
32+ public function testDefaultApiVersion (): void
33+ {
34+ $ this ->assertSame ('/v1.45 ' , $ this ->getApiPath ());
35+ }
36+
37+ public function testApiVersionFromEnvironment (): void
38+ {
39+ putenv ('DOCKER_API_VERSION=v1.52 ' );
40+
41+ $ this ->assertSame ('/v1.52 ' , $ this ->getApiPath ());
42+ }
43+
44+ public function testApiVersionWithLeadingSlash (): void
45+ {
46+ putenv ('DOCKER_API_VERSION=/v1.52 ' );
47+
48+ $ this ->assertSame ('/v1.52 ' , $ this ->getApiPath ());
49+ }
50+
51+ public function testApiVersionWithoutVersionPrefix (): void
52+ {
53+ putenv ('DOCKER_API_VERSION=1.52 ' );
54+
55+ $ this ->assertSame ('/v1.52 ' , $ this ->getApiPath ());
56+ }
57+
2358 public function testCreateFromEnvWithoutCertPath (): void
2459 {
2560 $ this ->expectException (\RuntimeException::class);
@@ -68,4 +103,27 @@ public function testCreateCustomPeerName(): void
68103 $ this ->assertSame ('/abc/key.pem ' , $ context ['ssl ' ]['local_pk ' ]);
69104 $ this ->assertSame ('test ' , $ context ['ssl ' ]['peer_name ' ]);
70105 }
106+
107+ private function getApiPath (): string
108+ {
109+ $ plugins = [];
110+ PluginClientFactory::setFactory (static function ($ client , array $ createdPlugins , array $ options ) use (&$ plugins ): PluginClient {
111+ $ plugins = $ createdPlugins ;
112+ unset($ options ['client_name ' ]);
113+
114+ return new PluginClient ($ client , $ createdPlugins , $ options );
115+ });
116+
117+ DockerClientFactory::create ();
118+
119+ foreach ($ plugins as $ plugin ) {
120+ if ($ plugin instanceof AddPathPlugin) {
121+ $ uri = (new \ReflectionProperty ($ plugin , 'uri ' ))->getValue ($ plugin );
122+
123+ return $ uri ->getPath ();
124+ }
125+ }
126+
127+ $ this ->fail ('The Docker API path plugin was not configured. ' );
128+ }
71129}
0 commit comments