Skip to content

Commit 7373272

Browse files
committed
test(phpunit): Add tests for validator, client and zip manager
1 parent ff60293 commit 7373272

File tree

9 files changed

+262
-42
lines changed

9 files changed

+262
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ composer.phar
77
.phpunit.result.cache
88
.phan
99
/builds
10+
.env

app/Commands/PrefixCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,29 @@ public function handle()
5252
$validator = new Validator();
5353
$sourceDirectory = $this->argumentOrEnv('source-directory');
5454

55-
if (!$sourcePath = $validator->validateDirectoryExists($sourceDirectory)) {
55+
if (!$sourcePath = $validator->isValidSourceDirectory($sourceDirectory)) {
5656
$this->error("{$sourceDirectory} not found");
5757

5858
return 1;
5959
}
6060

6161
$targetPath = $this->argumentOrEnv('target-directory');
6262

63-
if (!$sourcePath = $validator->validateDirectoryEmpty($sourceDirectory)) {
63+
if (!$sourcePath = $validator->isValidTargetDirectory($sourceDirectory)) {
6464
$this->error("{$sourceDirectory} not found");
6565

6666
return 1;
6767
}
6868

69-
if (!$personalAccessToken = $validator->validatePAT($this->argumentOrEnv('personal-access-token'))) {
69+
if (!$personalAccessToken = $validator->isPersonalAccessToken($this->argumentOrEnv('personal-access-token'))) {
7070
$this->error(
7171
'The Personal Access Token is invalid. Please, generate a new token on https://php-prefixer.com.'
7272
);
7373

7474
return 1;
7575
}
7676

77-
if (!$projectId = $validator->validateProject($personalAccessToken, $this->argumentOrEnv('project-id'))) {
77+
if (!$projectId = $validator->isValidProjectId($personalAccessToken, (int) $this->argumentOrEnv('project-id'))) {
7878
$this->error(
7979
'The Project ID is invalid'
8080
);
@@ -84,7 +84,7 @@ public function handle()
8484

8585
$githubAccessToken = $this->optionOrEnv('github-access-token');
8686

87-
if ($githubAccessToken && !$validator->validateGAT($githubAccessToken)) {
87+
if ($githubAccessToken && !$validator->isValidGithubAccessToken($githubAccessToken)) {
8888
$this->error(
8989
'The Github Access Token is invalid'
9090
);

app/Support/PrefixerClient.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Exception;
1616
use GuzzleHttp\Client as GuzzleClient;
1717
use GuzzleHttp\Psr7\Utils;
18-
use Psr\Http\Message\ResponseInterface;
1918

2019
class PrefixerClient
2120
{
@@ -32,22 +31,22 @@ public function authenticate($personalAccessToken)
3231

3332
public function isAuthenticated()
3433
{
35-
$user = $this->user();
34+
$response = $this->user();
3635

37-
return $user->id > 0;
36+
return $response->user->id > 0;
3837
}
3938

4039
public function user()
4140
{
42-
return $this->request('GET', 'user');
41+
return $this->request('GET', '/user');
4342
}
4443

45-
public function project($projectId)
44+
public function project(int $projectId)
4645
{
47-
return $this->request('GET', 'projects/'.$projectId);
46+
return $this->request('GET', '/projects/'.$projectId);
4847
}
4948

50-
public function createBuild($projectId, $zipFilename, $githubAccessToken = null)
49+
public function createBuild(int $projectId, $zipFilename, $githubAccessToken = null)
5150
{
5251
$options = [
5352
'multipart' => [
@@ -68,36 +67,37 @@ public function createBuild($projectId, $zipFilename, $githubAccessToken = null)
6867

6968
return $this->request(
7069
'POST',
71-
'projects/'.$projectId.'/build',
70+
'/projects/'.$projectId.'/build',
7271
$options
7372
);
7473
}
7574

76-
public function build($projectId, $buildId)
75+
public function build(int $projectId, int $buildId)
7776
{
7877
return $this->request(
7978
'GET',
80-
'projects/'.$projectId.'/builds/'.$buildId
79+
'/projects/'.$projectId.'/builds/'.$buildId
8180
);
8281
}
8382

84-
public function download($projectId, $buildId)
83+
public function download(int $projectId, int $buildId)
8584
{
8685
return $this->request(
8786
'GET',
88-
'projects/'.$projectId.'/builds/'.$buildId.'/download'
87+
'/projects/'.$projectId.'/builds/'.$buildId.'/download'
8988
);
9089
}
9190

92-
public function downloadLog($projectId, $buildId)
91+
public function downloadLog(int $projectId, int $buildId, $file)
9392
{
9493
return $this->request(
9594
'GET',
96-
'projects/'.$projectId.'/builds/'.$buildId.'/download/log'
95+
'/projects/'.$projectId.'/builds/'.$buildId.'/download/log',
96+
['sink' => $file]
9797
);
9898
}
9999

100-
private function request(string $method, $relApiUri = '', array $options = []): ResponseInterface
100+
private function request(string $method, $relApiUri = '', array $options = [])
101101
{
102102
$options = array_merge($this->jsonHeaders(), $options);
103103

@@ -108,7 +108,11 @@ private function request(string $method, $relApiUri = '', array $options = []):
108108
);
109109

110110
if (200 === $res->getStatusCode()) {
111-
return json_decode($res->getBody());
111+
if (\in_array('application/json', $res->getHeader('Content-Type'), true)) {
112+
return json_decode($res->getBody());
113+
}
114+
115+
return $res;
112116
}
113117

114118
throw new Exception($res->getReasonPhrase(), $res->getStatusCode());

app/Support/Validator.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
namespace App\Support;
1414

15+
use Exception;
1516
use Github\Client as GithubClient;
1617
use Illuminate\Support\Facades\File;
1718

1819
class Validator
1920
{
20-
public function validateDirectoryExists($param)
21+
public function isValidSourceDirectory($param)
2122
{
2223
if (!File::exists($param)) {
2324
return false;
@@ -27,28 +28,28 @@ public function validateDirectoryExists($param)
2728
return false;
2829
}
2930

30-
if (empty(File::files($param))) {
31+
if (empty(File::allFiles($param))) {
3132
return false;
3233
}
3334

3435
return (bool) realpath($param);
3536
}
3637

37-
public function validateDirectoryEmpty($param)
38+
public function isValidTargetDirectory($param)
3839
{
3940
if (File::exists($param)) {
4041
if (!File::isDirectory($param)) {
4142
return false;
4243
}
4344

44-
if (!empty(File::files($param))) {
45+
if (!empty(File::allFiles($param))) {
4546
return false;
4647
}
4748

4849
return true;
4950
}
5051

51-
mkdir($this->targetPath, 0700, true);
52+
mkdir($param, 0700, true);
5253

5354
if (!File::exists($param)) {
5455
return false;
@@ -57,26 +58,30 @@ public function validateDirectoryEmpty($param)
5758
return true;
5859
}
5960

60-
public function validatePAT($personalAccessToken)
61+
public function isPersonalAccessToken($personalAccessToken)
6162
{
62-
$prefixerClient = (new PrefixerClient())->authenticate($personalAccessToken);
63+
try {
64+
$prefixerClient = (new PrefixerClient())->authenticate($personalAccessToken);
6365

64-
return $prefixerClient->isAuthenticated();
66+
return $prefixerClient->isAuthenticated();
67+
} catch (Exception $e) {
68+
return false;
69+
}
6570
}
6671

67-
public function validateProject($personalAccessToken, $projectId)
72+
public function isValidProjectId($personalAccessToken, int $projectId)
6873
{
6974
try {
7075
$prefixerClient = (new PrefixerClient())->authenticate($personalAccessToken);
71-
$prefixerClient->project($projectId);
76+
$response = $prefixerClient->project($projectId);
7277

73-
return true;
74-
} finally {
78+
return $projectId === $response->project->id;
79+
} catch (Exception $e) {
7580
return false;
7681
}
7782
}
7883

79-
public function validateGAT($githubAccessToken)
84+
public function isValidGithubAccessToken($githubAccessToken)
8085
{
8186
try {
8287
$githubClient = new GithubClient();

config/filesystems.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* @package PHP Prefixer REST API CLI
5+
*
6+
* @author Desarrollos Inteligentes Virtuales, SL. <team@div.com.es>
7+
* @copyright Copyright (c)2019-2021 Desarrollos Inteligentes Virtuales, SL. All rights reserved.
8+
* @license MIT
9+
*
10+
* @see https://php-prefixer.com
11+
*/
12+
13+
return [
14+
'default' => 'local',
15+
'disks' => [
16+
'local' => [
17+
'driver' => 'local',
18+
'root' => getcwd(),
19+
],
20+
],
21+
];

env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ GITHUB_ACCESS_TOKEN="..."
88
# Only for testing
99
INVALID_PERSONAL_ACCESS_TOKEN="..."
1010
INVALID_GITHUB_ACCESS_TOKEN="..."
11+
TEST_BUILD_ID="..."
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* @package PHP Prefixer REST API CLI
5+
*
6+
* @author Desarrollos Inteligentes Virtuales, SL. <team@div.com.es>
7+
* @copyright Copyright (c)2019-2021 Desarrollos Inteligentes Virtuales, SL. All rights reserved.
8+
* @license MIT
9+
*
10+
* @see https://php-prefixer.com
11+
*/
12+
13+
namespace Tests\Commands;
14+
15+
use App\Support\PrefixerClient;
16+
use Tests\TestCase;
17+
18+
/**
19+
* @coversNothing
20+
*/
21+
final class PrefixerClientTest extends TestCase
22+
{
23+
public function testIsAuthenticated()
24+
{
25+
$this->assertTrue($this->apiClient()->IsAuthenticated());
26+
27+
$personalAccessToken = env('INVALID_PERSONAL_ACCESS_TOKEN');
28+
$prefixerClient = (new PrefixerClient())->authenticate($personalAccessToken);
29+
$this->expectExceptionCode(401);
30+
$prefixerClient->IsAuthenticated();
31+
}
32+
33+
public function testUser()
34+
{
35+
$response = $this->apiClient()->user();
36+
$this->assertGreaterThan(0, $response->user->id);
37+
}
38+
39+
public function testProject()
40+
{
41+
$projectId = (int) env('PROJECT_ID');
42+
43+
$response = $this->apiClient()->project($projectId);
44+
$this->assertSame($projectId, $response->project->id);
45+
46+
$this->expectExceptionCode(404);
47+
$this->apiClient()->project(404);
48+
}
49+
50+
public function testCreateBuild()
51+
{
52+
}
53+
54+
public function testBuild()
55+
{
56+
$projectId = (int) env('PROJECT_ID');
57+
$buildId = (int) env('TEST_BUILD_ID');
58+
$response = $this->apiClient()->build($projectId, $buildId);
59+
60+
$this->assertSame($buildId, $response->build->id);
61+
62+
$this->expectExceptionCode(404);
63+
$this->apiClient()->build($projectId, 404);
64+
}
65+
66+
public function testDownload()
67+
{
68+
$projectId = (int) env('PROJECT_ID');
69+
$buildId = (int) env('TEST_BUILD_ID');
70+
$response = $this->apiClient()->download($projectId, $buildId);
71+
72+
$this->assertSame(200, $response->getStatusCode());
73+
$this->assertTrue(\in_array('application/x-zip', $response->getHeader('Content-Type'), true));
74+
75+
$this->expectExceptionCode(404);
76+
$this->apiClient()->download($projectId, 404);
77+
}
78+
79+
public function testLogDownload()
80+
{
81+
$projectId = (int) env('PROJECT_ID');
82+
$buildId = (int) env('TEST_BUILD_ID');
83+
84+
$tmpFile = tempnam(getcwd(), 'ppp');
85+
$response = $this->apiClient()->downloadLog($projectId, $buildId, $tmpFile);
86+
87+
$this->assertSame(200, $response->getStatusCode());
88+
$this->assertTrue(\in_array('application/x-zip', $response->getHeader('Content-Type'), true));
89+
90+
$contentDisposition = $response->getHeader('Content-Disposition');
91+
list(, $filename) = explode('filename=', $contentDisposition[0]);
92+
$this->assertStringEndsWith('.log.zip', $filename);
93+
94+
unlink($tmpFile);
95+
96+
$this->expectExceptionCode(404);
97+
$this->apiClient()->downloadLog($projectId, 404, $tmpFile);
98+
}
99+
100+
private function apiClient()
101+
{
102+
$personalAccessToken = env('PERSONAL_ACCESS_TOKEN');
103+
104+
return (new PrefixerClient())->authenticate($personalAccessToken);
105+
}
106+
}

0 commit comments

Comments
 (0)