diff --git a/src/Resource/AbstractResource.php b/src/Resource/AbstractResource.php index 10ad200..47c141a 100644 --- a/src/Resource/AbstractResource.php +++ b/src/Resource/AbstractResource.php @@ -8,7 +8,7 @@ class AbstractResource { - protected array $data; + protected array $data = []; protected array $dateAttributes = []; public function __get(string $name) diff --git a/src/Resource/App.php b/src/Resource/App.php new file mode 100644 index 0000000..1e826cb --- /dev/null +++ b/src/Resource/App.php @@ -0,0 +1,24 @@ +client = $client; + $this->data = $data; + } +} diff --git a/src/Service/App.php b/src/Service/App.php index 823316e..ad34de8 100644 --- a/src/Service/App.php +++ b/src/Service/App.php @@ -6,8 +6,41 @@ class App extends AbstractService { + public const V2_APP_LIST_URL = "/v2/app/"; public const V2_APP_DETAIL_URL = "/v2/app/%s/"; public const V2_APP_CANCEL_URL = "/v2/app/%s/cancel/"; public const V2_APP_BRANCHER_URL = "/v2/app/%s/brancher/"; public const V1_APP_FLOWS_URL = "/logbook/v1/logbooks/%s/flows/"; + + /** + * @param array $params + * @return \Hypernode\Api\Resource\App[] + * @throws \Http\Client\Exception + * @throws \Hypernode\Api\Exception\HypernodeApiClientException + * @throws \Hypernode\Api\Exception\HypernodeApiServerException + */ + public function getList(array $params = []): array + { + $apps = []; + + $requestUrl = self::V2_APP_LIST_URL; + if ($params) { + $requestUrl .= '?' . http_build_query($params); + } + + while ($requestUrl) { + $response = $this->client->api->get($requestUrl); + + $this->client->maybeThrowApiExceptions($response); + $data = $this->client->getJsonFromResponse($response); + + foreach ($data['results'] as $item) { + $apps[] = new \Hypernode\Api\Resource\App($this->client, $item); + } + + $requestUrl = $data['next']; + } + + return $apps; + } } diff --git a/tests/unit/Service/AppTest.php b/tests/unit/Service/AppTest.php new file mode 100644 index 0000000..15edf05 --- /dev/null +++ b/tests/unit/Service/AppTest.php @@ -0,0 +1,115 @@ +responses->append( + new Response(200, [], json_encode([ + 'next' => null, + 'results' => [ + [ + 'name' => 'johndoe', + 'type' => 'persistent', + 'product' => 'FALCON_M_202203', + ], + [ + 'name' => 'tdgroot', + 'type' => 'persistent', + 'product' => 'FALCON_M_202203', + ], + ] + ])), + ); + + $result = $this->client->app->getList(); + + $this->assertCount(2, $result); + $this->assertContainsOnlyInstancesOf(\Hypernode\Api\Resource\App::class, $result); + $this->assertEquals('johndoe', $result[0]->name); + $this->assertEquals('tdgroot', $result[1]->name); + + $request = $this->responses->getLastRequest(); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('/v2/app/', $request->getUri()); + } + + public function testGetListAcceptsParams() + { + $this->responses->append( + new Response(200, [], json_encode([ + 'next' => null, + 'results' => [ + [ + 'name' => 'johndoe', + 'type' => 'persistent', + 'parent' => null, + 'product' => 'FALCON_M_202203', + ], + [ + 'name' => 'tdgroot', + 'type' => 'persistent', + 'parent' => 'johndoe', + 'product' => 'FALCON_M_202203', + ], + ] + ])), + ); + + $result = $this->client->app->getList(['parent' => 'johndoe']); + + $this->assertCount(2, $result); + $this->assertContainsOnlyInstancesOf(\Hypernode\Api\Resource\App::class, $result); + $this->assertEquals('johndoe', $result[0]->name); + $this->assertEquals('tdgroot', $result[1]->name); + $this->assertEquals('johndoe', $result[1]->parent); + + $request = $this->responses->getLastRequest(); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('/v2/app/?parent=johndoe', $request->getUri()); + } + + public function testGetListPaginates() + { + $this->responses->append( + new Response(200, [], json_encode([ + 'next' => 'https://api.hypernode.com/v2/app/?limit=1&offset=1', + 'results' => [ + [ + 'name' => 'johndoe', + 'type' => 'persistent', + 'product' => 'FALCON_M_202203', + ], + ] + ])), + new Response(200, [], json_encode([ + 'next' => null, + 'results' => [ + [ + 'name' => 'tdgroot', + 'type' => 'persistent', + 'product' => 'FALCON_M_202203', + ], + ] + ])), + ); + + $result = $this->client->app->getList(); + + $this->assertCount(2, $result); + $this->assertContainsOnlyInstancesOf(\Hypernode\Api\Resource\App::class, $result); + $this->assertEquals('johndoe', $result[0]->name); + $this->assertEquals('tdgroot', $result[1]->name); + + $request = $this->responses->getLastRequest(); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('https://api.hypernode.com/v2/app/?limit=1&offset=1', $request->getUri()); + } +}