Skip to content

Commit 9a93ee9

Browse files
authored
Merge pull request #8 from ByteInternet/app_list
2 parents 742a59b + 0154326 commit 9a93ee9

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

src/Resource/AbstractResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class AbstractResource
1010
{
11-
protected array $data;
11+
protected array $data = [];
1212
protected array $dateAttributes = [];
1313

1414
public function __get(string $name)

src/Resource/App.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Api\Resource;
6+
7+
use Hypernode\Api\HypernodeClient;
8+
use Hypernode\Api\Resource\AbstractResource;
9+
10+
/**
11+
* @property-read string $name
12+
* @property-read string $type
13+
* @property-read string|null $parent
14+
*/
15+
class App extends AbstractResource
16+
{
17+
protected HypernodeClient $client;
18+
19+
public function __construct(HypernodeClient $client, array $data = [])
20+
{
21+
$this->client = $client;
22+
$this->data = $data;
23+
}
24+
}

src/Service/App.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,41 @@
66

77
class App extends AbstractService
88
{
9+
public const V2_APP_LIST_URL = "/v2/app/";
910
public const V2_APP_DETAIL_URL = "/v2/app/%s/";
1011
public const V2_APP_CANCEL_URL = "/v2/app/%s/cancel/";
1112
public const V2_APP_BRANCHER_URL = "/v2/app/%s/brancher/";
1213
public const V1_APP_FLOWS_URL = "/logbook/v1/logbooks/%s/flows/";
14+
15+
/**
16+
* @param array $params
17+
* @return \Hypernode\Api\Resource\App[]
18+
* @throws \Http\Client\Exception
19+
* @throws \Hypernode\Api\Exception\HypernodeApiClientException
20+
* @throws \Hypernode\Api\Exception\HypernodeApiServerException
21+
*/
22+
public function getList(array $params = []): array
23+
{
24+
$apps = [];
25+
26+
$requestUrl = self::V2_APP_LIST_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+
$apps[] = new \Hypernode\Api\Resource\App($this->client, $item);
39+
}
40+
41+
$requestUrl = $data['next'];
42+
}
43+
44+
return $apps;
45+
}
1346
}

tests/unit/Service/AppTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Api\Service;
6+
7+
use GuzzleHttp\Psr7\Response;
8+
use Hypernode\Api\HypernodeClientTestCase;
9+
10+
class AppTest extends HypernodeClientTestCase
11+
{
12+
public function testGetList()
13+
{
14+
$this->responses->append(
15+
new Response(200, [], json_encode([
16+
'next' => null,
17+
'results' => [
18+
[
19+
'name' => 'johndoe',
20+
'type' => 'persistent',
21+
'product' => 'FALCON_M_202203',
22+
],
23+
[
24+
'name' => 'tdgroot',
25+
'type' => 'persistent',
26+
'product' => 'FALCON_M_202203',
27+
],
28+
]
29+
])),
30+
);
31+
32+
$result = $this->client->app->getList();
33+
34+
$this->assertCount(2, $result);
35+
$this->assertContainsOnlyInstancesOf(\Hypernode\Api\Resource\App::class, $result);
36+
$this->assertEquals('johndoe', $result[0]->name);
37+
$this->assertEquals('tdgroot', $result[1]->name);
38+
39+
$request = $this->responses->getLastRequest();
40+
$this->assertEquals('GET', $request->getMethod());
41+
$this->assertEquals('/v2/app/', $request->getUri());
42+
}
43+
44+
public function testGetListAcceptsParams()
45+
{
46+
$this->responses->append(
47+
new Response(200, [], json_encode([
48+
'next' => null,
49+
'results' => [
50+
[
51+
'name' => 'johndoe',
52+
'type' => 'persistent',
53+
'parent' => null,
54+
'product' => 'FALCON_M_202203',
55+
],
56+
[
57+
'name' => 'tdgroot',
58+
'type' => 'persistent',
59+
'parent' => 'johndoe',
60+
'product' => 'FALCON_M_202203',
61+
],
62+
]
63+
])),
64+
);
65+
66+
$result = $this->client->app->getList(['parent' => 'johndoe']);
67+
68+
$this->assertCount(2, $result);
69+
$this->assertContainsOnlyInstancesOf(\Hypernode\Api\Resource\App::class, $result);
70+
$this->assertEquals('johndoe', $result[0]->name);
71+
$this->assertEquals('tdgroot', $result[1]->name);
72+
$this->assertEquals('johndoe', $result[1]->parent);
73+
74+
$request = $this->responses->getLastRequest();
75+
$this->assertEquals('GET', $request->getMethod());
76+
$this->assertEquals('/v2/app/?parent=johndoe', $request->getUri());
77+
}
78+
79+
public function testGetListPaginates()
80+
{
81+
$this->responses->append(
82+
new Response(200, [], json_encode([
83+
'next' => 'https://api.hypernode.com/v2/app/?limit=1&offset=1',
84+
'results' => [
85+
[
86+
'name' => 'johndoe',
87+
'type' => 'persistent',
88+
'product' => 'FALCON_M_202203',
89+
],
90+
]
91+
])),
92+
new Response(200, [], json_encode([
93+
'next' => null,
94+
'results' => [
95+
[
96+
'name' => 'tdgroot',
97+
'type' => 'persistent',
98+
'product' => 'FALCON_M_202203',
99+
],
100+
]
101+
])),
102+
);
103+
104+
$result = $this->client->app->getList();
105+
106+
$this->assertCount(2, $result);
107+
$this->assertContainsOnlyInstancesOf(\Hypernode\Api\Resource\App::class, $result);
108+
$this->assertEquals('johndoe', $result[0]->name);
109+
$this->assertEquals('tdgroot', $result[1]->name);
110+
111+
$request = $this->responses->getLastRequest();
112+
$this->assertEquals('GET', $request->getMethod());
113+
$this->assertEquals('https://api.hypernode.com/v2/app/?limit=1&offset=1', $request->getUri());
114+
}
115+
}

0 commit comments

Comments
 (0)