forked from tienvx/composer-downloads-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Composer\Installer; | ||
|
||
use Composer\Composer; | ||
use Composer\IO\IOInterface; | ||
use Composer\Package\PackageInterface; | ||
use Composer\Repository\InstalledRepositoryInterface; | ||
use Composer\Util\Filesystem; | ||
use LastCall\DownloadsPlugin\Composer\Package\ExtraDownloadInterface; | ||
use LastCall\DownloadsPlugin\Enum\PackageType; | ||
use LastCall\DownloadsPlugin\Installer\ExecutableInstallerInterface; | ||
use React\Promise\PromiseInterface; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class FileInstaller extends AbstractInstaller | ||
{ | ||
public const TMP_PREFIX = '.composer-extra-tmp-'; | ||
|
||
protected Filesystem $filesystem; | ||
|
||
public function __construct( | ||
IOInterface $io, | ||
Composer $composer, | ||
?Filesystem $filesystem = null, | ||
?ExecutableInstallerInterface $executableInstaller = null, | ||
) { | ||
parent::__construct($io, $composer, $executableInstaller); | ||
$this->filesystem = $filesystem ?: new Filesystem(); | ||
} | ||
|
||
public function supports(string $packageType): bool | ||
{ | ||
return $packageType === PackageType::FILE->value; | ||
} | ||
|
||
public function install(InstalledRepositoryInterface $repo, PackageInterface $package): PromiseInterface | ||
{ | ||
if (!$package instanceof ExtraDownloadInterface) { | ||
return \React\Promise\resolve(null); | ||
} | ||
|
||
$expectedPath = $package->getInstallPath(); | ||
$tmpDir = \dirname($expectedPath).\DIRECTORY_SEPARATOR.uniqid(self::TMP_PREFIX, true); | ||
$promise = $this->composer->getDownloadManager()->install($package, $tmpDir); | ||
|
||
return $promise->then(function () use ($repo, $package, $expectedPath, $tmpDir) { | ||
$finder = new Finder(); | ||
foreach ($finder->files()->in($tmpDir) as $file) { | ||
$this->filesystem->rename($file, $expectedPath); | ||
break; | ||
} | ||
$this->filesystem->remove($tmpDir); | ||
|
||
return parent::install($repo, $package); | ||
}); | ||
} | ||
} |
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,97 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Tests\Unit\Composer\Installer; | ||
|
||
use Composer\Installer\InstallerInterface; | ||
use Composer\Util\Filesystem; | ||
use LastCall\DownloadsPlugin\Composer\Installer\FileInstaller; | ||
use LastCall\DownloadsPlugin\Composer\Package\ExtraDownloadInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use React\Promise\Promise; | ||
|
||
class FileInstallerTest extends AbstractInstallerTestCase | ||
{ | ||
private Filesystem|MockObject $filesystem; | ||
private string $tmpDir; | ||
private string $fileName = 'file.ext'; | ||
private string $fileContents = 'file contents'; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->filesystem = $this->createMock(Filesystem::class); | ||
parent::setUp(); | ||
} | ||
|
||
protected function createInstaller(): InstallerInterface | ||
{ | ||
return new FileInstaller($this->io, $this->composer, $this->filesystem, $this->executableInstaller); | ||
} | ||
|
||
/** | ||
* @testWith ["extra-download:file", true] | ||
* ["extra-download:archive", false] | ||
*/ | ||
public function testSupports(string $type, bool $supports): void | ||
{ | ||
$this->assertSame($supports, $this->installer->supports($type)); | ||
} | ||
|
||
/** | ||
* @testWith [true] | ||
* [false] | ||
*/ | ||
public function testInstallExtraDownload(bool $hasPackage): void | ||
{ | ||
$this->composer | ||
->expects($this->once()) | ||
->method('getDownloadManager') | ||
->willReturn($this->downloadManager); | ||
$this->downloadManager | ||
->expects($this->once()) | ||
->method('install') | ||
->with($this->extraDownload, $this->callback(function (string $targetDir): bool { | ||
$this->assertTrue(str_contains($targetDir, FileInstaller::TMP_PREFIX)); | ||
|
||
return true; | ||
})) | ||
->willReturnCallback(function (ExtraDownloadInterface $extraDownload, string $tmpDir) { | ||
$this->tmpDir = $tmpDir; | ||
mkdir($this->tmpDir, 0777, true); | ||
file_put_contents($this->tmpDir.\DIRECTORY_SEPARATOR.$this->fileName, $this->fileContents); | ||
|
||
$this->filesystem | ||
->expects($this->once()) | ||
->method('rename') | ||
->with($this->tmpDir.\DIRECTORY_SEPARATOR.$this->fileName, $this->fs->path($this->installPath)); | ||
$this->filesystem | ||
->expects($this->once()) | ||
->method('remove') | ||
->with($this->tmpDir); | ||
|
||
$downloaderPromise = new Promise(fn (callable $resolve) => $resolve(null)); | ||
|
||
return $downloaderPromise; | ||
}); | ||
$this->extraDownload | ||
->expects($this->once()) | ||
->method('getInstallPath') | ||
->willReturn($this->fs->path($this->installPath)); | ||
$this->executableInstaller | ||
->expects($this->once()) | ||
->method('install') | ||
->with($this->extraDownload); | ||
$this->repository | ||
->expects($this->once()) | ||
->method('hasPackage') | ||
->with($this->extraDownload) | ||
->willReturn($hasPackage); | ||
$this->repository | ||
->expects($this->exactly(!$hasPackage)) | ||
->method('addPackage') | ||
->with($this->extraDownload); | ||
$installerPromise = $this->installer->install($this->repository, $this->extraDownload); | ||
$installerPromise->then(function ($result) { | ||
$this->assertNull($result); | ||
}); | ||
} | ||
} |