Skip to content

Commit 3253f35

Browse files
committed
test(command): Test the main command
1 parent bf77c31 commit 3253f35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+7448
-37
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
composer.phar
22
/vendor
3-
/tests/Mock/Source/vendor
3+
/tests/Mock/*/vendor
44
/.idea
55
/.vscode
66
/.vagrant

app/Commands/PrefixCommand.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,36 @@ class PrefixCommand extends Command
4646
*/
4747
public function handle()
4848
{
49-
$appName = config('app.name');
50-
$invalidPersonalAccessToken = env('INVALID_PERSONAL_ACCESS_TOKEN');
51-
5249
$validator = new Validator();
53-
$sourceDirectory = $this->argumentOrEnv('source-directory');
50+
$sourceDirectory = realpath($this->argumentOrEnv('source-directory'));
5451

55-
if (!$sourcePath = $validator->isValidSourceDirectory($sourceDirectory)) {
52+
if (!$validator->isValidSourceDirectory($sourceDirectory)) {
5653
$this->error("{$sourceDirectory} not found");
5754

5855
return 1;
5956
}
6057

61-
$targetPath = $this->argumentOrEnv('target-directory');
58+
$targetDirectory = realpath($this->argumentOrEnv('target-directory'));
6259

63-
if (!$sourcePath = $validator->isValidTargetDirectory($sourceDirectory)) {
64-
$this->error("{$sourceDirectory} not found");
60+
if (!$validator->isValidTargetDirectory($targetDirectory)) {
61+
$this->error("{$targetDirectory} not found");
6562

6663
return 1;
6764
}
6865

69-
if (!$personalAccessToken = $validator->isPersonalAccessToken($this->argumentOrEnv('personal-access-token'))) {
66+
$personalAccessToken = $this->argumentOrEnv('personal-access-token');
67+
68+
if (!$validator->isPersonalAccessToken($personalAccessToken)) {
7069
$this->error(
7170
'The Personal Access Token is invalid. Please, generate a new token on https://php-prefixer.com.'
7271
);
7372

7473
return 1;
7574
}
7675

77-
if (!$projectId = $validator->isValidProjectId($personalAccessToken, (int) $this->argumentOrEnv('project-id'))) {
76+
$projectId = (int) $this->argumentOrEnv('project-id');
77+
78+
if (!$validator->isValidProjectId($personalAccessToken, $projectId)) {
7879
$this->error(
7980
'The Project ID is invalid'
8081
);
@@ -93,11 +94,26 @@ public function handle()
9394
}
9495

9596
$processor = new Processor($personalAccessToken);
96-
$processor->run($sourcePath, $targetPath, $projectId, $githubAccessToken);
97+
$build = $processor->run($sourceDirectory, $targetDirectory, $projectId, $githubAccessToken);
98+
99+
switch ($build->state) {
100+
case 'success':
101+
$this->info('Project prefixed successfully.');
102+
103+
return 0;
104+
case 'cancelled':
105+
$this->error('Project prefixing cancelled.');
106+
107+
return 1;
108+
case 'failed':
109+
$this->error('Project prefixing failed.');
110+
111+
return 1;
112+
}
97113

98-
$this->info('Project prefixed successfully');
114+
$this->error('Project prefixing error.' - $build->state);
99115

100-
return 0;
116+
return 1;
101117
}
102118

103119
private function argumentOrEnv($key)

app/Support/PrefixerClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function authenticate($personalAccessToken)
2929
return $this;
3030
}
3131

32-
public function isAuthenticated()
32+
public function isAuthenticated(): bool
3333
{
3434
$response = $this->user();
3535

app/Support/Processor.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ public function run($sourcePath, $targetPath, int $projectId, $githubAccessToken
3232
$zipManager->compress($sourcePath, $tmpZip);
3333

3434
$response = $this->prefixerClient->createBuild($projectId, $tmpZip, $githubAccessToken);
35-
36-
$build = $response->build;
3735
unlink($tmpZip);
3836

37+
$build = $response->build;
3938
$this->waitForProcessing($build);
39+
$response = $this->prefixerClient->build($build->project_id, $build->id);
40+
41+
$build = $response->build;
4042
$downloadedZip = $this->download($build, $targetPath);
41-
$zipManager->uncompress($downloadedZip, $targetPath);
42-
unlink($downloadedZip);
4343

44-
$response = $this->prefixerClient->build($build->project_id, $build->id);
44+
if ($downloadedZip) {
45+
$zipManager->uncompress($downloadedZip, $targetPath);
46+
unlink($downloadedZip);
47+
}
4548

4649
return $build;
4750
}
@@ -63,7 +66,7 @@ private function isProcessing($build)
6366
$response = $this->prefixerClient->build($build->project_id, $build->id);
6467
$build = $response->build;
6568

66-
return !\in_array($build->state, ['success', 'failed'], true);
69+
return !\in_array($build->state, ['success', 'failed', 'cancelled'], true);
6770
}
6871

6972
private function download($build, $targetPath)
@@ -72,7 +75,11 @@ private function download($build, $targetPath)
7275
return $this->prefixerClient->download($build->project_id, $build->id, $targetPath);
7376
}
7477

75-
// Failed
76-
return $this->prefixerClient->downloadLog($build->project_id, $build->id, $targetPath);
78+
if ('failed' === $build->state) {
79+
return $this->prefixerClient->downloadLog($build->project_id, $build->id, $targetPath);
80+
}
81+
82+
// cancelled
83+
return null;
7784
}
7885
}

app/Support/Validator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class Validator
2020
{
21-
public function isValidSourceDirectory($param)
21+
public function isValidSourceDirectory(string $param): bool
2222
{
2323
if (!File::exists($param)) {
2424
return false;
@@ -35,7 +35,7 @@ public function isValidSourceDirectory($param)
3535
return (bool) realpath($param);
3636
}
3737

38-
public function isValidTargetDirectory($param)
38+
public function isValidTargetDirectory(string $param): bool
3939
{
4040
if (File::exists($param)) {
4141
if (!File::isDirectory($param)) {
@@ -58,7 +58,7 @@ public function isValidTargetDirectory($param)
5858
return true;
5959
}
6060

61-
public function isPersonalAccessToken($personalAccessToken)
61+
public function isPersonalAccessToken(string $personalAccessToken): bool
6262
{
6363
try {
6464
$prefixerClient = (new PrefixerClient())->authenticate($personalAccessToken);
@@ -69,7 +69,7 @@ public function isPersonalAccessToken($personalAccessToken)
6969
}
7070
}
7171

72-
public function isValidProjectId($personalAccessToken, int $projectId)
72+
public function isValidProjectId(string $personalAccessToken, int $projectId): bool
7373
{
7474
try {
7575
$prefixerClient = (new PrefixerClient())->authenticate($personalAccessToken);
@@ -81,7 +81,7 @@ public function isValidProjectId($personalAccessToken, int $projectId)
8181
}
8282
}
8383

84-
public function isValidGithubAccessToken($githubAccessToken)
84+
public function isValidGithubAccessToken(string $githubAccessToken): bool
8585
{
8686
try {
8787
$githubClient = new GithubClient();

env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ GITHUB_ACCESS_TOKEN="..."
99
INVALID_PERSONAL_ACCESS_TOKEN="..."
1010
INVALID_GITHUB_ACCESS_TOKEN="..."
1111
TEST_BUILD_ID="..."
12+
BROKEN_COMPOSER_SOURCE_DIRECTORY="..."
13+
BROKEN_DEPS_SOURCE_DIRECTORY="..."
14+

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
2+
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true">
33
<testsuites>
44
<testsuite name="Commands">
55
<directory suffix="Test.php">./tests/Commands</directory>

tests/Commands/PrefixCommandTest.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,49 @@
1515
use Tests\TestCase;
1616

1717
/**
18-
* @group disabled
1918
* @coversNothing
2019
*/
2120
final class PrefixCommandTest extends TestCase
2221
{
2322
public function testPrefix()
2423
{
24+
$this->cleanTargetDirectory();
25+
2526
$this->artisan(
2627
'prefix',
2728
[
28-
'source-directory' => 'source-directory',
29-
'target-directory' => 'target-directory',
30-
'personal-access-token' => 'personal-access-token',
31-
'project-id' => 'project-id',
29+
'source-directory' => env('SOURCE_DIRECTORY'),
30+
'target-directory' => env('TARGET_DIRECTORY'),
31+
'personal-access-token' => env('PERSONAL_ACCESS_TOKEN'),
32+
'project-id' => env('PROJECT_ID'),
3233
'--github-access-token' => null,
3334
]
3435
)
35-
->expectsOutput('Simplicity is the ultimate sophistication.')
36+
->expectsOutput('Project prefixed successfully.')
3637
->assertExitCode(0);
38+
39+
$this->assertFileExists(env('TARGET_DIRECTORY').'/composer.json');
40+
41+
$this->cleanTargetDirectory();
42+
}
43+
44+
public function testCancelledPrefix()
45+
{
46+
$this->cleanTargetDirectory();
47+
48+
$this->artisan(
49+
'prefix',
50+
[
51+
'source-directory' => env('BROKEN_COMPOSER_SOURCE_DIRECTORY'),
52+
'target-directory' => env('TARGET_DIRECTORY'),
53+
'personal-access-token' => env('PERSONAL_ACCESS_TOKEN'),
54+
'project-id' => env('PROJECT_ID'),
55+
'--github-access-token' => null,
56+
]
57+
)
58+
->expectsOutput('Project prefixing cancelled.')
59+
->assertExitCode(1);
60+
61+
$this->cleanTargetDirectory();
3762
}
3863
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 Just\AMockUp;
14+
15+
use Dotenv\Dotenv;
16+
use League\Uri\Modifiers\Normalize;
17+
use Stringy\Stringy;
18+
19+
/**
20+
* @coversNothing
21+
*/
22+
class SampleClass extends TestCase
23+
{
24+
public function __construct()
25+
{
26+
$test = Stringy::create('Is this a test?')->ensureRight(' Oh, yes!');
27+
$normalize = new Normalize();
28+
$dotenv = new Dotenv();
29+
30+
// To be prefixed
31+
$thisView = view('MyView');
32+
33+
// Not be prefixed
34+
$a = 'view';
35+
36+
$arr = [];
37+
38+
// To be prefixed
39+
$v = value('a-value');
40+
41+
// Not be prefixed
42+
$v = $arr['value'];
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
use Dotenv\Dotenv;
14+
use League\Uri\Modifiers\Normalize;
15+
use Stringy\Stringy;
16+
17+
$test = Stringy::create('Is this a test?')->ensureRight(' Oh, yes!');
18+
$normalize = new Normalize();
19+
$dotenv = new Dotenv();
20+
21+
// To be prefixed
22+
$thisView = view('MyView');
23+
24+
// Not be prefixed
25+
$a = 'view';
26+
27+
$arr = [];
28+
29+
// To be prefixed
30+
$v = value('a-value');
31+
32+
// Not be prefixed
33+
$v = $arr['value'];

0 commit comments

Comments
 (0)