Skip to content

Commit ff789cb

Browse files
author
Jonathan Visser
committed
Add support for listing preinstall configurations (GET /v2/configuration/)
1 parent ad5f69c commit ff789cb

3 files changed

Lines changed: 181 additions & 0 deletions

File tree

src/HypernodeClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Hypernode\Api\Service\App;
1212
use Hypernode\Api\Service\Backup;
1313
use Hypernode\Api\Service\BrancherApp;
14+
use Hypernode\Api\Service\Configuration;
1415
use Hypernode\Api\Service\Logbook;
1516
use Hypernode\Api\Service\Settings;
1617
use Psr\Http\Message\ResponseInterface;
@@ -22,6 +23,7 @@ class HypernodeClient
2223
public App $app;
2324
public Backup $backup;
2425
public BrancherApp $brancherApp;
26+
public Configuration $configuration;
2527
public Settings $settings;
2628
public Logbook $logbook;
2729

@@ -32,6 +34,7 @@ public function __construct(HttpMethodsClientInterface $apiClient)
3234
$this->app = new App($this);
3335
$this->backup = new Backup($this);
3436
$this->brancherApp = new BrancherApp($this);
37+
$this->configuration = new Configuration($this);
3538
$this->settings = new Settings($this);
3639
$this->logbook = new Logbook($this);
3740
}

src/Service/Configuration.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Api\Service;
6+
7+
use Hypernode\Api\Exception\HypernodeApiClientException;
8+
use Hypernode\Api\Exception\HypernodeApiServerException;
9+
10+
class Configuration extends AbstractService
11+
{
12+
public const V2_CONFIGURATION_URL = "/v2/configuration/";
13+
14+
/**
15+
* List all available configurations for preinstalls.
16+
*
17+
* @param array $params Query parameters, e.g. allow_preinstall, limit and offset
18+
* @return array List of app configurations
19+
* @throws HypernodeApiClientException
20+
* @throws HypernodeApiServerException
21+
*/
22+
public function getList(array $params = []): array
23+
{
24+
$configurations = [];
25+
26+
$requestUrl = self::V2_CONFIGURATION_URL;
27+
if ($params) {
28+
$requestUrl .= '?' . http_build_query($params);
29+
}
30+
31+
while ($requestUrl) {
32+
$response = $this->client->api->get($requestUrl);
33+
34+
$this->client->maybeThrowApiExceptions($response);
35+
$data = $this->client->getJsonFromResponse($response);
36+
37+
foreach ($data['results'] as $item) {
38+
$configurations[] = $item;
39+
}
40+
41+
$requestUrl = $data['next'];
42+
}
43+
44+
return $configurations;
45+
}
46+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Api\Service;
6+
7+
use GuzzleHttp\Psr7\Response;
8+
use Hypernode\Api\Exception\HypernodeApiClientException;
9+
use Hypernode\Api\Exception\HypernodeApiServerException;
10+
use Hypernode\Api\HypernodeClientTestCase;
11+
12+
class ConfigurationTest extends HypernodeClientTestCase
13+
{
14+
public function testGetList()
15+
{
16+
$configurations = [
17+
[
18+
'name' => 'Magento 2',
19+
'configuration_id' => 'magento_2',
20+
'php_version' => '8.1',
21+
'mysql_version' => '8.0',
22+
'varnish_enabled' => false,
23+
],
24+
[
25+
'name' => 'Shopware 6',
26+
'configuration_id' => 'shopware_6',
27+
'php_version' => '8.2',
28+
'mysql_version' => '8.0',
29+
'varnish_enabled' => true,
30+
],
31+
];
32+
$this->responses->append(
33+
new Response(200, [], json_encode([
34+
'count' => 2,
35+
'next' => null,
36+
'previous' => null,
37+
'results' => $configurations,
38+
])),
39+
);
40+
41+
$result = $this->client->configuration->getList();
42+
43+
$request = $this->responses->getLastRequest();
44+
$this->assertEquals('GET', $request->getMethod());
45+
$this->assertEquals('/v2/configuration/', $request->getUri());
46+
$this->assertEquals($configurations, $result);
47+
}
48+
49+
public function testGetListAcceptsParams()
50+
{
51+
$this->responses->append(
52+
new Response(200, [], json_encode([
53+
'count' => 0,
54+
'next' => null,
55+
'previous' => null,
56+
'results' => [],
57+
])),
58+
);
59+
60+
$result = $this->client->configuration->getList(['allow_preinstall' => 'true']);
61+
62+
$request = $this->responses->getLastRequest();
63+
$this->assertEquals('GET', $request->getMethod());
64+
$this->assertEquals('/v2/configuration/?allow_preinstall=true', $request->getUri());
65+
$this->assertEquals([], $result);
66+
}
67+
68+
public function testGetListPaginates()
69+
{
70+
$this->responses->append(
71+
new Response(200, [], json_encode([
72+
'count' => 2,
73+
'next' => 'https://api.hypernode.com/v2/configuration/?limit=1&offset=1',
74+
'previous' => null,
75+
'results' => [
76+
[
77+
'name' => 'Magento 2',
78+
'configuration_id' => 'magento_2',
79+
],
80+
],
81+
])),
82+
new Response(200, [], json_encode([
83+
'count' => 2,
84+
'next' => null,
85+
'previous' => null,
86+
'results' => [
87+
[
88+
'name' => 'Shopware 6',
89+
'configuration_id' => 'shopware_6',
90+
],
91+
],
92+
])),
93+
);
94+
95+
$result = $this->client->configuration->getList();
96+
97+
$this->assertCount(2, $result);
98+
$this->assertEquals('magento_2', $result[0]['configuration_id']);
99+
$this->assertEquals('shopware_6', $result[1]['configuration_id']);
100+
101+
$request = $this->responses->getLastRequest();
102+
$this->assertEquals('GET', $request->getMethod());
103+
$this->assertEquals(
104+
'https://api.hypernode.com/v2/configuration/?limit=1&offset=1',
105+
$request->getUri()
106+
);
107+
}
108+
109+
public function testGetListRaisesClientExceptions()
110+
{
111+
$badRequestResponse = new Response(400, [], json_encode([
112+
'non_field_errors' => ['Your request was invalid.']
113+
]));
114+
$this->responses->append($badRequestResponse);
115+
116+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
117+
118+
$this->client->configuration->getList();
119+
}
120+
121+
public function testGetListRaisesServerExceptions()
122+
{
123+
$badRequestResponse = new Response(500, [], json_encode([
124+
'non_field_errors' => ['Something went wrong processing your request.']
125+
]));
126+
$this->responses->append($badRequestResponse);
127+
128+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
129+
130+
$this->client->configuration->getList();
131+
}
132+
}

0 commit comments

Comments
 (0)