Skip to content

Commit bf77c31

Browse files
committed
test(Processor): Add test for complete command execution
1 parent 1c39d0d commit bf77c31

File tree

6 files changed

+142
-49
lines changed

6 files changed

+142
-49
lines changed

app/Support/PrefixerClient.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,53 @@ public function build(int $projectId, int $buildId)
8080
);
8181
}
8282

83-
public function download(int $projectId, int $buildId)
83+
public function download(int $projectId, int $buildId, $targetDirectory)
8484
{
85-
return $this->request(
86-
'GET',
87-
'/projects/'.$projectId.'/builds/'.$buildId.'/download'
85+
return $this->downloadWithSink(
86+
'/projects/'.$projectId.'/builds/'.$buildId.'/download',
87+
$targetDirectory
8888
);
8989
}
9090

91-
public function downloadLog(int $projectId, int $buildId, $file)
91+
public function downloadLog(int $projectId, int $buildId, $targetDirectory)
9292
{
93-
return $this->request(
94-
'GET',
93+
return $this->downloadWithSink(
9594
'/projects/'.$projectId.'/builds/'.$buildId.'/download/log',
96-
['sink' => $file]
95+
$targetDirectory
9796
);
9897
}
9998

99+
private function downloadWithSink($downloadUri, $targetDirectory)
100+
{
101+
$sink = $this->temporaryZipFilename($targetDirectory);
102+
103+
try {
104+
$response = $this->request(
105+
'GET',
106+
$downloadUri,
107+
[
108+
'sink' => $sink,
109+
]
110+
);
111+
112+
if (200 !== $response->getStatusCode() ||
113+
!\in_array('application/x-zip', $response->getHeader('Content-Type'), true)) {
114+
unlink($sink);
115+
116+
throw new Exception('Unexpected response', $response->getStatusCode());
117+
}
118+
119+
$downloadedFile = $targetDirectory.'/'.$this->filename($response);
120+
rename($sink, $downloadedFile);
121+
} catch (Exception $e) {
122+
unlink($sink);
123+
124+
throw $e;
125+
}
126+
127+
return $downloadedFile;
128+
}
129+
100130
private function request(string $method, $relApiUri = '', array $options = [])
101131
{
102132
$options = array_merge($this->headers(), $options);
@@ -129,4 +159,17 @@ private function headers()
129159
],
130160
];
131161
}
162+
163+
private function filename($response)
164+
{
165+
$contentDisposition = $response->getHeader('Content-Disposition');
166+
list(, $filename) = explode('filename=', $contentDisposition[0]);
167+
168+
return $filename;
169+
}
170+
171+
private function temporaryZipFilename($targetPath)
172+
{
173+
return tempnam($targetPath, 'PPP-');
174+
}
132175
}

app/Support/Processor.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class Processor
1616
{
17-
const WAIT_TIMEOUT = 15;
17+
const WAIT_TIMEOUT = 30;
1818
private $personalAccessToken;
1919

2020
private $prefixerClient;
@@ -25,47 +25,54 @@ public function __construct($personalAccessToken)
2525
$this->prefixerClient = (new PrefixerClient())->authenticate($this->personalAccessToken);
2626
}
2727

28-
public function run($sourcePath, $targetPath, $projectId, $githubAccessToken = null)
28+
public function run($sourcePath, $targetPath, int $projectId, $githubAccessToken = null)
2929
{
30-
$tmpZip = $this->temporaryZipFilename($sourcePath, $targetPath, $projectId, $githubAccessToken);
31-
3230
$zipManager = new ZipManager();
33-
$zipManager->compress($this->sourcePath, $tmpZip);
31+
$tmpZip = $this->temporaryZipFilename($targetPath);
32+
$zipManager->compress($sourcePath, $tmpZip);
33+
34+
$response = $this->prefixerClient->createBuild($projectId, $tmpZip, $githubAccessToken);
35+
36+
$build = $response->build;
37+
unlink($tmpZip);
3438

35-
$build = $this->prefixerClient->createBuild($tmpZip, $projectId, $this->githubAccessToken);
36-
$build = $this->waitForProcessing($build);
37-
$downloadedZip = $this->download($build);
38-
$zipManager->uncompress($downloadedZip, $this->targetPath);
39+
$this->waitForProcessing($build);
40+
$downloadedZip = $this->download($build, $targetPath);
41+
$zipManager->uncompress($downloadedZip, $targetPath);
42+
unlink($downloadedZip);
43+
44+
$response = $this->prefixerClient->build($build->project_id, $build->id);
45+
46+
return $build;
3947
}
4048

41-
private function temporaryZipFilename()
49+
private function temporaryZipFilename($targetPath)
4250
{
43-
return tempnam($this->targetPath, 'PPP').'.zip';
51+
return $targetPath.'/'.basename($targetPath).'.zip';
4452
}
4553

4654
private function waitForProcessing($build)
4755
{
48-
while ($updatedBuild = $this->isProcessing($build)) {
56+
while ($this->isProcessing($build)) {
4957
sleep(self::WAIT_TIMEOUT);
5058
}
51-
52-
return $updatedBuild;
5359
}
5460

5561
private function isProcessing($build)
5662
{
57-
$nuild = $this->prefixerClient->build($build->project_id, $build->build_id);
63+
$response = $this->prefixerClient->build($build->project_id, $build->id);
64+
$build = $response->build;
5865

5966
return !\in_array($build->state, ['success', 'failed'], true);
6067
}
6168

62-
private function download($build)
69+
private function download($build, $targetPath)
6370
{
6471
if ('success' === $build->state) {
65-
return $this->prefixerClient->download($build->project_id, $build->build_id);
72+
return $this->prefixerClient->download($build->project_id, $build->id, $targetPath);
6673
}
6774

6875
// Failed
69-
return $this->prefixerClient->downloadLog($build->project_id, $build->build_id);
76+
return $this->prefixerClient->downloadLog($build->project_id, $build->id, $targetPath);
7077
}
7178
}

tests/Support/PrefixerClientTest.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public function testCreateBuild()
5151
{
5252
$projectId = (int) env('PROJECT_ID');
5353
$sourceDirectory = env('SOURCE_DIRECTORY');
54+
$githubAccessToken = env('GITHUB_ACCESS_TOKEN');
5455
$projectZip = realpath($sourceDirectory.'/../Source.zip');
55-
$response = $this->apiClient()->createBuild($projectId, $projectZip);
56+
57+
$response = $this->apiClient()->createBuild($projectId, $projectZip, $githubAccessToken);
5658

5759
$this->assertSame('initial-state', $response->build->state);
5860
}
@@ -73,34 +75,31 @@ public function testDownload()
7375
{
7476
$projectId = (int) env('PROJECT_ID');
7577
$buildId = (int) env('TEST_BUILD_ID');
76-
$response = $this->apiClient()->download($projectId, $buildId);
78+
$targetDirectory = env('TARGET_DIRECTORY');
79+
$downloadedFile = $this->apiClient()->download($projectId, $buildId, $targetDirectory);
7780

78-
$this->assertSame(200, $response->getStatusCode());
79-
$this->assertTrue(\in_array('application/x-zip', $response->getHeader('Content-Type'), true));
81+
$this->assertStringEndsWith('.zip', $downloadedFile);
82+
$this->fileExists($downloadedFile);
83+
unlink($downloadedFile);
8084

8185
$this->expectExceptionCode(404);
82-
$this->apiClient()->download($projectId, 404);
86+
$this->apiClient()->download($projectId, 404, $targetDirectory);
8387
}
8488

8589
public function testLogDownload()
8690
{
8791
$projectId = (int) env('PROJECT_ID');
8892
$buildId = (int) env('TEST_BUILD_ID');
93+
$targetDirectory = env('TARGET_DIRECTORY');
8994

90-
$tmpFile = tempnam(getcwd(), 'ppp');
91-
$response = $this->apiClient()->downloadLog($projectId, $buildId, $tmpFile);
92-
93-
$this->assertSame(200, $response->getStatusCode());
94-
$this->assertTrue(\in_array('application/x-zip', $response->getHeader('Content-Type'), true));
95-
96-
$contentDisposition = $response->getHeader('Content-Disposition');
97-
list(, $filename) = explode('filename=', $contentDisposition[0]);
98-
$this->assertStringEndsWith('.log.zip', $filename);
95+
$downloadedFile = $this->apiClient()->downloadLog($projectId, $buildId, $targetDirectory);
9996

100-
unlink($tmpFile);
97+
$this->assertStringEndsWith('.log.zip', $downloadedFile);
98+
$this->fileExists($downloadedFile);
99+
unlink($downloadedFile);
101100

102101
$this->expectExceptionCode(404);
103-
$this->apiClient()->downloadLog($projectId, 404, $tmpFile);
102+
$this->apiClient()->download($projectId, 404, $targetDirectory);
104103
}
105104

106105
private function apiClient()

tests/Support/ProcessorTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Processor;
16+
use Tests\TestCase;
17+
18+
/**
19+
* @coversNothing
20+
*/
21+
final class ProcessorTest extends TestCase
22+
{
23+
public function testRun()
24+
{
25+
$this->cleanTargetDirectory();
26+
27+
$personalAccessToken = env('PERSONAL_ACCESS_TOKEN');
28+
$processor = new Processor($personalAccessToken);
29+
30+
$sourceDirectory = env('SOURCE_DIRECTORY');
31+
$targetDirectory = env('TARGET_DIRECTORY');
32+
$projectId = (int) env('PROJECT_ID');
33+
$githubAccessToken = env('GITHUB_ACCESS_TOKEN');
34+
35+
$build = $processor->run($sourceDirectory, $targetDirectory, $projectId, $githubAccessToken);
36+
37+
$this->assertSame('success', $build->state);
38+
$this->cleanTargetDirectory();
39+
}
40+
}

tests/Support/ZipManagerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
namespace Tests\Commands;
1414

1515
use App\Support\ZipManager;
16-
use Illuminate\Support\Facades\File;
1716
use Tests\TestCase;
1817

1918
/**
@@ -23,25 +22,24 @@ final class ZipManagerTest extends TestCase
2322
{
2423
public function testCompressUncompress()
2524
{
26-
$zipManager = new ZipManager();
27-
28-
// Let's start fresh
29-
$targetDirectory = env('TARGET_DIRECTORY');
30-
File::cleanDirectory($targetDirectory);
25+
$this->cleanTargetDirectory();
3126

3227
$sourceDirectory = env('SOURCE_DIRECTORY');
3328
$tmpZip = getcwd().'/test.zip';
29+
30+
$zipManager = new ZipManager();
3431
$status = $zipManager->compress($sourceDirectory, $tmpZip);
3532

3633
$this->assertTrue($status);
3734
$this->assertFileExists($tmpZip);
3835

36+
$targetDirectory = env('TARGET_DIRECTORY')
3937
$zipManager->uncompress($tmpZip, $targetDirectory);
4038

4139
$this->assertFileExists($targetDirectory.'/app');
4240
$this->assertFileExists($targetDirectory.'/composer.json');
4341

4442
unlink($tmpZip);
45-
File::cleanDirectory($targetDirectory);
43+
$this->cleanTargetDirectory();
4644
}
4745
}

tests/TestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212

1313
namespace Tests;
1414

15+
use Illuminate\Support\Facades\File;
1516
use LaravelZero\Framework\Testing\TestCase as BaseTestCase;
1617

1718
abstract class TestCase extends BaseTestCase
1819
{
1920
use CreatesApplication;
21+
22+
protected function cleanTargetDirectory()
23+
{
24+
File::cleanDirectory(env('TARGET_DIRECTORY'));
25+
}
2026
}

0 commit comments

Comments
 (0)