Skip to content

Commit bd9067e

Browse files
committed
Add unit tests for Settings and Logbook\Job
1 parent 512c9e7 commit bd9067e

File tree

7 files changed

+190
-11
lines changed

7 files changed

+190
-11
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"Hypernode\\Api\\": "src/"
99
}
1010
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"Hypernode\\Api\\": "tests/unit"
14+
}
15+
},
1116
"authors": [
1217
{
1318
"name": "Hypernode"
@@ -24,6 +29,7 @@
2429
"require-dev": {
2530
"friendsofphp/php-cs-fixer": "^3.9",
2631
"guzzlehttp/guzzle": "^7.4",
27-
"nikic/php-parser": "^4.14"
32+
"nikic/php-parser": "^4.14",
33+
"phpunit/phpunit": "^9.5"
2834
}
2935
}

src/HypernodeClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hypernode\Api;
66

77
use Http\Client\Common\HttpMethodsClient;
8+
use Http\Client\Common\HttpMethodsClientInterface;
89
use Hypernode\Api\Exception\HypernodeApiClientException;
910
use Hypernode\Api\Exception\HypernodeApiServerException;
1011
use Hypernode\Api\Service\App;
@@ -15,11 +16,11 @@ class HypernodeClient
1516
{
1617
public const VERSION = '0.1.0';
1718

18-
public HttpMethodsClient $api;
19+
public HttpMethodsClientInterface $api;
1920
public App $app;
2021
public Settings $settings;
2122

22-
public function __construct(HttpMethodsClient $apiClient)
23+
public function __construct(HttpMethodsClientInterface $apiClient)
2324
{
2425
$this->api = $apiClient;
2526
$this->app = new App($this);

src/Resource/Logbook/Job.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,35 @@ public function __construct(HypernodeClient $client, string $urlOrId, array $dat
3030
}
3131

3232
/**
33-
* Refresh the job data, return true when job has not completed yet, return false if job has completed.
34-
* Useful to run this command like:
33+
* Refresh the job data. Can be used to wait for a job to be completed, like:
3534
* while (!$job->completed()) {
3635
* $job->refresh();
3736
* }
3837
*/
39-
public function refresh(): bool
38+
public function refresh()
4039
{
4140
$response = $this->client->api->get($this->url);
4241

4342
if ($response->getStatusCode() === 404) {
4443
$this->data = [];
4544
$this->exists = false;
4645
$this->running = false;
47-
return true;
46+
return;
4847
}
4948

5049
if ($response->getStatusCode() === 303) {
5150
$this->data = [];
5251
$this->exists = true;
5352
$this->running = false;
5453
$this->complete = true;
55-
return false;
54+
return;
5655
}
5756

5857
$this->client->maybeThrowApiExceptions($response);
5958

6059
$this->data = $this->client->getJsonFromResponse($response);
6160
$this->exists = true;
6261
$this->running = true;
63-
64-
return false;
6562
}
6663

6764
public function exists(): bool

src/Service/Settings.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function setBatch(string $app, array $settings): ?Job
2222

2323
if ($response->getStatusCode() === 202) {
2424
$job = new Job($this->client, $response->getHeaderLine('Location'));
25-
$job->refresh();
2625
return $job;
2726
}
2827

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Hypernode\Api;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use Http\Client\Common\HttpMethodsClient;
9+
use Http\Client\Common\HttpMethodsClientInterface;
10+
use Http\Discovery\Psr17FactoryDiscovery;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class HypernodeClientTestCase extends TestCase
14+
{
15+
protected HttpMethodsClientInterface $api;
16+
protected MockHandler $responses;
17+
protected HypernodeClient $client;
18+
19+
protected function setUp(): void
20+
{
21+
$this->responses = new MockHandler();
22+
$this->api = new HttpMethodsClient(
23+
new Client(['handler' => HandlerStack::create($this->responses)]),
24+
Psr17FactoryDiscovery::findRequestFactory(),
25+
Psr17FactoryDiscovery::findStreamFactory()
26+
);
27+
$this->client = new HypernodeClient($this->api);
28+
}
29+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Hypernode\Api\Resource\Logbook;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Hypernode\Api\HypernodeClientTestCase;
7+
8+
class JobTest extends HypernodeClientTestCase
9+
{
10+
private Job $job;
11+
private string $jobUrl;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->jobUrl = "https://api.hypernode.com/logbook/v1/jobs/abcd/";
17+
$this->job = new Job($this->client, $this->jobUrl);
18+
}
19+
20+
public function testRefresh()
21+
{
22+
$this->responses->append(
23+
new Response(404, [], json_encode([])),
24+
new Response(200, [], json_encode([
25+
'result' => 'pending',
26+
'flow_name' => 'update_node',
27+
'app_name' => 'johndoe'
28+
])),
29+
new Response(200, [], json_encode([
30+
'result' => 'running',
31+
'flow_name' => 'update_node',
32+
'app_name' => 'johndoe'
33+
])),
34+
new Response(303, [], json_encode([])),
35+
);
36+
37+
$this->job->refresh();
38+
39+
$this->assertFalse($this->job->exists());
40+
$this->assertFalse($this->job->completed());
41+
42+
$this->job->refresh();
43+
44+
$this->assertTrue($this->job->exists());
45+
$this->assertFalse($this->job->completed());
46+
47+
$this->job->refresh();
48+
49+
$this->assertTrue($this->job->exists());
50+
$this->assertFalse($this->job->completed());
51+
52+
$this->job->refresh();
53+
54+
$this->assertTrue($this->job->exists());
55+
$this->assertTrue($this->job->completed());
56+
}
57+
58+
public function testExistsReturnsFalseByDefault()
59+
{
60+
$this->assertFalse($this->job->exists());
61+
}
62+
63+
public function testCompletedReturnsFalseByDefault()
64+
{
65+
$this->assertFalse($this->job->completed());
66+
}
67+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Hypernode\Api\Service;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Hypernode\Api\HypernodeClientTestCase;
7+
8+
class SettingsTest extends HypernodeClientTestCase
9+
{
10+
public function testSetSingleSettingToSameValue()
11+
{
12+
$this->responses->append(
13+
new Response(200, [], null),
14+
);
15+
16+
$job = $this->client->settings->set('johndoe', 'php_version', '8.1');
17+
18+
$request = $this->responses->getLastRequest();
19+
20+
$this->assertNull($job);
21+
$this->assertEquals('PATCH', $request->getMethod());
22+
$this->assertEquals('/v2/app/johndoe/', $request->getUri());
23+
$this->assertJson($request->getBody());
24+
$this->assertEquals(
25+
['php_version' => '8.1'],
26+
json_decode($request->getBody(), true)
27+
);
28+
}
29+
30+
public function testSetSingleSettingToDifferentValue()
31+
{
32+
$jobUrl = 'https://api.hypernode.com/logbook/v1/jobs/abcd/';
33+
$this->responses->append(
34+
new Response(202, ['Location: ' . $jobUrl], null),
35+
);
36+
37+
$job = $this->client->settings->set('johndoe', 'php_version', '8.1');
38+
39+
$request = $this->responses->getLastRequest();
40+
41+
$this->assertNotNull($job);
42+
$this->assertEquals('PATCH', $request->getMethod());
43+
$this->assertEquals('/v2/app/johndoe/', $request->getUri());
44+
$this->assertJson($request->getBody());
45+
$this->assertEquals(
46+
['php_version' => '8.1'],
47+
json_decode($request->getBody(), true)
48+
);
49+
}
50+
51+
public function testSetMultipleSettings()
52+
{
53+
$jobUrl = 'https://api.hypernode.com/logbook/v1/jobs/abcd/';
54+
$this->responses->append(
55+
new Response(202, ['Location: ' . $jobUrl], null),
56+
);
57+
58+
$job = $this->client->settings->setBatch(
59+
'johndoe',
60+
[
61+
'php_version' => '8.1',
62+
'nodejs_version' => '18'
63+
]
64+
);
65+
66+
$request = $this->responses->getLastRequest();
67+
68+
$this->assertNotNull($job);
69+
$this->assertEquals('PATCH', $request->getMethod());
70+
$this->assertEquals('/v2/app/johndoe/', $request->getUri());
71+
$this->assertJson($request->getBody());
72+
$this->assertEquals(
73+
[
74+
'php_version' => '8.1',
75+
'nodejs_version' => '18'
76+
],
77+
json_decode($request->getBody(), true)
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)