Skip to content

App: Add getList method #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Resource/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AbstractResource
{
protected array $data;
protected array $data = [];
protected array $dateAttributes = [];

public function __get(string $name)
Expand Down
24 changes: 24 additions & 0 deletions src/Resource/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Hypernode\Api\Resource;

use Hypernode\Api\HypernodeClient;
use Hypernode\Api\Resource\AbstractResource;

/**
* @property-read string $name
* @property-read string $type
* @property-read string|null $parent
*/
class App extends AbstractResource
{
protected HypernodeClient $client;

public function __construct(HypernodeClient $client, array $data = [])
{
$this->client = $client;
$this->data = $data;
}
}
33 changes: 33 additions & 0 deletions src/Service/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
115 changes: 115 additions & 0 deletions tests/unit/Service/AppTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Hypernode\Api\Service;

use GuzzleHttp\Psr7\Response;
use Hypernode\Api\HypernodeClientTestCase;

class AppTest extends HypernodeClientTestCase
{
public function testGetList()
{
$this->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());
}
}