Skip to content

Commit 2eab84c

Browse files
authored
Move test suite to unit tests (#60)
Co-authored-by: LKaemmerling <LKaemmerling@users.noreply.github.com>
1 parent 4a4a694 commit 2eab84c

File tree

71 files changed

+2046
-297
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2046
-297
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ jobs:
1010
operating-system: [ubuntu-latest]
1111
php-versions: ['7.1', '7.2', '7.3', '7.4']
1212
steps:
13-
- uses: actions/checkout@v1
14-
- name: Start Container
15-
run: |
16-
docker pull lkdevelopment/hetzner-cloud-api-mock
17-
docker run -d -p 127.0.0.1:4000:8080 lkdevelopment/hetzner-cloud-api-mock
18-
docker ps -a
13+
- uses: actions/checkout@v2
1914
- name: Setup PHP
20-
uses: shivammathur/setup-php@v1
15+
uses: shivammathur/setup-php@v2
2116
with:
2217
php-version: ${{ matrix.php-versions }}
2318
- name: Install depdendencies

phpunit.xml.dist

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,4 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2922
</phpunit>

src/Models/FloatingIps/FloatingIps.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function getById(int $floatingIpId): ?FloatingIp
8282
{
8383
$response = $this->httpClient->get('floating_ips/'.$floatingIpId);
8484
if (! HetznerAPIClient::hasError($response)) {
85-
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
85+
return FloatingIp::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']});
8686
}
8787

8888
return null;
@@ -122,15 +122,26 @@ public function create(
122122
Server $server = null,
123123
string $name = null
124124
): ?FloatingIp {
125-
$response = $this->httpClient->post('floating_ips', [
125+
$parameters = [
126126
'type' => $type,
127-
'description' => $description,
128-
'server' => $server ?: $server->id,
129-
'home_location' => $location ?: $location->name,
130-
'name' => $name ?: $name,
127+
];
128+
if ($description != null) {
129+
$parameters['description'] = $description;
130+
}
131+
if ($name != null) {
132+
$parameters['name'] = $name;
133+
}
134+
if ($location != null) {
135+
$parameters['home_location'] = $location->name;
136+
}
137+
if ($server != null) {
138+
$parameters['server'] = $server->id ?: $server->name;
139+
}
140+
$response = $this->httpClient->post('floating_ips', [
141+
'json' => $parameters,
131142
]);
132143
if (! HetznerAPIClient::hasError($response)) {
133-
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
144+
return FloatingIp::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']});
134145
}
135146

136147
return null;

src/Models/ISOs/ISOs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public function getById(int $isoId): ?ISO
9696
*/
9797
public function getByName(string $name): ?ISO
9898
{
99-
$isos = $this->list(new ISORequestOpts($name));
99+
$resp = $this->list(new ISORequestOpts($name));
100100

101-
return (count($isos) > 0) ? $isos[0] : null;
101+
return (count($resp->isos) > 0) ? $resp->isos[0] : null;
102102
}
103103

104104
/**

src/Models/Networks/Network.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function addRoute(Route $route)
123123
public function deleteRoute(Route $route)
124124
{
125125
$response = $this->httpClient->post('networks/'.$this->id.'/actions/delete_route', [
126-
'json' => [$route->__toRequestPayload()],
126+
'json' => $route->__toRequestPayload(),
127127
]);
128128
if (! HetznerAPIClient::hasError($response)) {
129129
return APIResponse::create([

src/Models/Networks/Route.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace LKDev\HetznerCloud\Models\Networks;
44

55
use GuzzleHttp\Client;
6-
use LKDev\HetznerCloud\Clients\GuzzleClient;
76
use LKDev\HetznerCloud\Models\Model;
87

98
/**
@@ -35,10 +34,10 @@ public function __construct(string $destination, string $gateway, Client $client
3534

3635
/**
3736
* @param $input
38-
* @param GuzzleClient|null $client
37+
* @param Client|null $client
3938
* @return array|Model
4039
*/
41-
public static function parse($input, GuzzleClient $client = null)
40+
public static function parse($input, Client $client = null)
4241
{
4342
return collect($input)->map(function ($route) use ($client) {
4443
return new self($route->destination, $route->gateway, $client);

src/Models/Networks/Subnet.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace LKDev\HetznerCloud\Models\Networks;
44

55
use GuzzleHttp\Client;
6-
use LKDev\HetznerCloud\Clients\GuzzleClient;
76
use LKDev\HetznerCloud\Models\Model;
87

98
/**
@@ -49,10 +48,10 @@ public function __construct(string $type, string $ipRange, string $networkZone,
4948

5049
/**
5150
* @param $input
52-
* @param GuzzleClient|null $client
51+
* @param Client|null $client
5352
* @return array|Model
5453
*/
55-
public static function parse($input, GuzzleClient $client = null)
54+
public static function parse($input, Client $client = null)
5655
{
5756
return collect($input)->map(function ($subnet) use ($client) {
5857
return new self($subnet->type, $subnet->ip_range, $subnet->network_zone, $subnet->gateway, $client);

src/Models/Servers/Server.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class Server extends Model implements Resource
117117
*/
118118
public $volumes;
119119

120+
/**
121+
* @var int
122+
*/
123+
public $primaryDiskSize;
124+
120125
/**
121126
* @param int $serverId
122127
* @param Client|null $httpClient
@@ -151,6 +156,7 @@ public function setAdditionalData($data)
151156
$this->volumes = property_exists($data, 'volumes') ? $data->volumes : [];
152157
$this->protection = $data->protection ?: Protection::parse($data->protection);
153158
$this->labels = $data->labels;
159+
$this->primaryDiskSize = $data->primary_disk_size ?: null;
154160

155161
return $this;
156162
}
@@ -388,7 +394,7 @@ public function rebuildFromImage(Image $image): ?APIResponse
388394
{
389395
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/rebuild'), [
390396
'json' => [
391-
'image' => $image->name,
397+
'image' => $image->id ?: $image->name,
392398
],
393399
]);
394400
if (! HetznerAPIClient::hasError($response)) {
@@ -478,7 +484,7 @@ public function attachISO(ISO $iso): ?APIResponse
478484
{
479485
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/attach_iso'), [
480486
'json' => [
481-
'iso' => $iso->name == null ? $iso->id : $iso->name,
487+
'iso' => $iso->name ?: $iso->id,
482488
],
483489
]);
484490
if (! HetznerAPIClient::hasError($response)) {

src/Models/Volumes/Volume.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,23 @@ public function delete(): ?APIResponse
110110

111111
/**
112112
* @param Server $server
113+
* @param bool|null $automount
113114
* @return APIResponse|null
114115
* @throws \LKDev\HetznerCloud\APIException
115116
*/
116-
public function attach(Server $server): ?APIResponse
117+
public function attach(Server $server, $automount = null): ?APIResponse
117118
{
119+
$payload = [
120+
'server' => $server->id,
121+
];
122+
if ($automount !== null) {
123+
$payload['automount'] = $automount;
124+
}
125+
118126
$response = $this->httpClient->post('volumes/'.$this->id.'/actions/attach', [
119-
'json' => [
120-
'server' => $server->id,
121-
],
127+
'json' => $payload,
122128
]);
129+
123130
if (! HetznerAPIClient::hasError($response)) {
124131
return APIResponse::create([
125132
'action' => Action::parse(json_decode((string) $response->getBody())->action),

src/Models/Volumes/Volumes.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ public function create(string $name, int $size, Server $server = null, Location
125125
'automount' => $automount,
126126
];
127127
if ($location == null && $server != null) {
128-
$payload['server'] = $server->id;
128+
$payload['server'] = $server->name ?: $server->id;
129129
} elseif ($location != null && $server == null) {
130-
$payload['location'] = $location->id;
130+
$payload['location'] = $location->name ?: $location->id;
131131
} else {
132132
throw new \InvalidArgumentException('Please specify only a server or a location');
133133
}
@@ -143,6 +143,9 @@ public function create(string $name, int $size, Server $server = null, Location
143143
return APIResponse::create([
144144
'action' => Action::parse($payload->action),
145145
'volume' => Volume::parse($payload->volume),
146+
'next_actions' => collect($payload->next_actions)->map(function ($action) {
147+
return Action::parse($action);
148+
})->toArray(),
146149
], $response->getHeaders());
147150
}
148151

0 commit comments

Comments
 (0)