Skip to content

Commit 39f9fe3

Browse files
committed
Add option for generating docs for a local project
1 parent 80149a4 commit 39f9fe3

File tree

4 files changed

+92
-28
lines changed

4 files changed

+92
-28
lines changed
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
<?php
22
declare(strict_types=1);
33

4-
54
namespace App\CodeRepository;
65

6+
use App\Configuration\Value\Source;
7+
use InvalidArgumentException;
78

89
class CodeRepositoryFactory
910
{
10-
11+
public static function fromType(string $type): CodeRepository
12+
{
13+
switch ($type) {
14+
case Source::TYPE_GIT:
15+
return new GitCodeRepository();
16+
case Source::TYPE_LOCAL:
17+
return new LocalCodeRepository();
18+
default:
19+
throw new InvalidArgumentException('Invalid type: ' . $type);
20+
}
21+
}
1122
}

src/CodeRepository/LocalCodeRepository.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
use Symfony\Component\Process\Exception\ProcessFailedException;
99
use Symfony\Component\Process\Process;
1010

11-
class GitCodeRepository implements CodeRepository
11+
class LocalCodeRepository implements CodeRepository
1212
{
13-
public function downloadCode(Source $source): Folder
13+
public function getFolder(Source $source): Folder
1414
{
15-
$localPath = new Folder(self::CODE_DOWNLOAD_PATH . $source->getLocalFolder() . '/');
15+
$this->runProcess($this->getComposerInstallProcess($source->getLocalFolder()));
1616

17-
$this->runProcess($this->getCloneOrPullProcess($localPath, $source->getPath()));
18-
$this->runProcess($this->getComposerInstallProcess($localPath));
19-
20-
return $localPath;
17+
return $source->getLocalFolder();
2118
}
2219

2320
private function runProcess(Process $process): void
@@ -29,25 +26,6 @@ private function runProcess(Process $process): void
2926
}
3027
}
3128

32-
private function getCloneOrPullProcess(Folder $localPath, string $sourcePath): Process
33-
{
34-
if (!is_dir((string)$localPath)) {
35-
return new Process([
36-
'git',
37-
'clone',
38-
$sourcePath,
39-
(string)$localPath
40-
]);
41-
}
42-
43-
return new Process([
44-
'git',
45-
'-C',
46-
(string)$localPath,
47-
'pull'
48-
]);
49-
}
50-
5129
private function getComposerInstallProcess(Folder $repoPath): Process
5230
{
5331
return new Process([
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\CodeRepository;
5+
6+
use App\CodeRepository\CodeRepositoryFactory;
7+
use App\CodeRepository\GitCodeRepository;
8+
use App\CodeRepository\LocalCodeRepository;
9+
use App\Configuration\Value\Source;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/** @covers \App\CodeRepository\CodeRepositoryFactory */
13+
class CodeRepositoryFactoryTest extends TestCase
14+
{
15+
/** @test */
16+
public function fromType_WithGit_ReturnGitImplementation()
17+
{
18+
self::assertInstanceOf(
19+
GitCodeRepository::class,
20+
CodeRepositoryFactory::fromType(Source::TYPE_GIT)
21+
);
22+
}
23+
24+
/** @test */
25+
public function fromType_WithLocal_ReturnLocalImplementation()
26+
{
27+
self::assertInstanceOf(
28+
LocalCodeRepository::class,
29+
CodeRepositoryFactory::fromType(Source::TYPE_LOCAL)
30+
);
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\CodeRepository;
5+
6+
use App\CodeRepository\LocalCodeRepository;
7+
use App\Configuration\Value\Source;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Filesystem\Filesystem;
10+
use Symfony\Component\Process\Exception\RuntimeException;
11+
12+
/** @covers \App\CodeRepository\LocalCodeRepository */
13+
class LocalCodeRepositoryTest extends TestCase
14+
{
15+
private const LOCAL_PATH = 'var/repos/Standard';
16+
private LocalCodeRepository $codeRepo;
17+
18+
/** @test */
19+
public function getFolder_WithMissingPath_ThrowException()
20+
{
21+
$this->expectException(RuntimeException::class);
22+
$sourcePath = 'var/repos/MISSING';
23+
$this->codeRepo->getFolder(new Source($sourcePath, []));
24+
}
25+
26+
/** @test */
27+
public function getFolder_WithExistingPath_InstallComposer()
28+
{
29+
$fs = new Filesystem;
30+
$sourcePath = self::LOCAL_PATH;
31+
$fs->remove($sourcePath);
32+
$fs->mkdir($sourcePath);
33+
$fs->dumpFile($sourcePath . '/composer.json', '{}');
34+
35+
$this->codeRepo->getFolder(new Source($sourcePath, []));
36+
self::assertFileExists('var/repos/Standard/composer.lock');
37+
}
38+
39+
protected function setUp(): void
40+
{
41+
$this->codeRepo = new LocalCodeRepository();
42+
}
43+
}

0 commit comments

Comments
 (0)