Skip to content

Commit

Permalink
Merge pull request #8 from ByteInternet/app_list
Browse files Browse the repository at this point in the history
  • Loading branch information
tdgroot authored Nov 23, 2022
2 parents 742a59b + 0154326 commit 9a93ee9
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 1 deletion.
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());
}
}

0 comments on commit 9a93ee9

Please sign in to comment.