-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Options --from & --to can be set from the config (Close #112) * Increase coverage * cs
- Loading branch information
Showing
12 changed files
with
245 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Riverwaysoft\PhpConverter\CodeProvider; | ||
|
||
interface CodeProviderInterface | ||
{ | ||
/** @return string[] */ | ||
public function getListings(): iterable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Riverwaysoft\PhpConverter\CodeProvider; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Process\Process; | ||
|
||
class RemoteRepositoryCodeProvider implements CodeProviderInterface | ||
{ | ||
private Filesystem $filesystem; | ||
|
||
public function __construct( | ||
private string $repositoryUrl, | ||
private string $branch, | ||
) { | ||
$this->filesystem = new Filesystem(); | ||
} | ||
|
||
public function getListings(): iterable | ||
{ | ||
$repoDownloadFolder = $this->generateRepoDownloadFolder($this->repositoryUrl); | ||
if ($this->filesystem->exists($repoDownloadFolder)) { | ||
$this->filesystem->remove($repoDownloadFolder); | ||
} | ||
$process = new Process(["git", "clone", "--branch", $this->branch, $this->repositoryUrl, "--depth", "1", $repoDownloadFolder]); | ||
$process->mustRun(); | ||
$fileSystemCodeProvider = FileSystemCodeProvider::phpFiles($repoDownloadFolder); | ||
|
||
return $fileSystemCodeProvider->getListings(); | ||
} | ||
|
||
private function generateRepoDownloadFolder(string $repositoryUrl): string | ||
{ | ||
return sprintf("%s/%s", sys_get_temp_dir(), basename($repositoryUrl)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Config; | ||
|
||
use Exception; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
use Riverwaysoft\PhpConverter\Ast\ConverterVisitor; | ||
use Riverwaysoft\PhpConverter\CodeProvider\CodeProviderInterface; | ||
use Riverwaysoft\PhpConverter\CodeProvider\FileSystemCodeProvider; | ||
use Riverwaysoft\PhpConverter\CodeProvider\RemoteRepositoryCodeProvider; | ||
use Riverwaysoft\PhpConverter\Config\PhpConverterConfig; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
|
||
class PhpConverterConfigTest extends TestCase | ||
{ | ||
public function testVisitors(): void | ||
{ | ||
$config = new PhpConverterConfig($this->createInputMock([])); | ||
|
||
$visitor1Mock = $this->createMock(ConverterVisitor::class); | ||
$visitor2Mock = $this->createMock(ConverterVisitor::class); | ||
|
||
$config->addVisitor($visitor1Mock); | ||
$config->addVisitor($visitor2Mock); | ||
|
||
$this->assertCount(2, $config->getVisitors()); | ||
} | ||
|
||
public function testGetCodeProviderWhenAlreadySet(): void | ||
{ | ||
$config = new PhpConverterConfig($this->createInputMock([])); | ||
|
||
$mockedProvider = $this->createMock(CodeProviderInterface::class); | ||
$config->setCodeProvider($mockedProvider); | ||
|
||
$this->assertSame($mockedProvider, $config->getCodeProvider()); | ||
} | ||
|
||
public function testGetCodeProviderFromDirectory(): void | ||
{ | ||
$input = $this->createInputMock([ | ||
'from' => __DIR__, | ||
]); | ||
$config = new PhpConverterConfig($input); | ||
|
||
$this->assertInstanceOf(FileSystemCodeProvider::class, $config->getCodeProvider()); | ||
} | ||
|
||
public function testGetCodeProviderFromGitLinkWithBranch(): void | ||
{ | ||
$input = $this->createInputMock([ | ||
'from' => 'https://example.com/repo.git', | ||
'branch' => 'main', | ||
]); | ||
$config = new PhpConverterConfig($input); | ||
|
||
$this->assertInstanceOf(RemoteRepositoryCodeProvider::class, $config->getCodeProvider()); | ||
} | ||
|
||
public function testGetCodeProviderFromGitShhLinkWithBranch(): void | ||
{ | ||
$input = $this->createInputMock([ | ||
'from' => 'git@gitlab.com:test_soft/project/repo.git', | ||
'branch' => 'main', | ||
]); | ||
$config = new PhpConverterConfig($input); | ||
|
||
$this->assertInstanceOf(RemoteRepositoryCodeProvider::class, $config->getCodeProvider()); | ||
} | ||
|
||
public function testGetCodeProviderFromGitLinkWithoutBranch(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Option --branch is required when using URL as repository source'); | ||
|
||
$input = $this->createInputMock([ | ||
'from' => 'https://example.com/repo.git', | ||
'branch' => '', | ||
]); | ||
$config = new PhpConverterConfig($input); | ||
|
||
$config->getCodeProvider(); | ||
} | ||
|
||
public function testGetCodeProviderFromInvalidOption(): void | ||
{ | ||
$this->expectException(Exception::class); | ||
|
||
$input = $this->createInputMock([ | ||
'from' => 'invalid', | ||
]); | ||
$config = new PhpConverterConfig($input); | ||
|
||
$config->getCodeProvider(); | ||
} | ||
|
||
private function createInputMock(mixed $values): InputInterface | ||
{ | ||
$mock = $this->createMock(InputInterface::class); | ||
$mock->method('getOption') | ||
->willReturnCallback(function ($argument) use ($values) { | ||
if (isset($values[$argument])) { | ||
return $values[$argument]; | ||
} | ||
throw new Exception('Unknown input mock argument ' . $argument); | ||
}); | ||
|
||
return $mock; | ||
} | ||
} |
Oops, something went wrong.