Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Feb 6, 2019
1 parent 8f75f1b commit 162497e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 96 deletions.
8 changes: 3 additions & 5 deletions src/ComposerInstaller/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Installer extends LibraryInstaller
* @param string $packageType
* @return bool
*/
public function supports($packageType)
public function supports($packageType): bool
{
throw new RuntimeException('This method needs to be overridden.');
throw new RuntimeException('This method needs to be overridden.'); // @codeCoverageIgnore
}

/**
Expand Down Expand Up @@ -73,9 +73,7 @@ protected function postInstall(PackageInterface $package)
if (is_dir($packageVendorDir)) {
$success = $this->filesystem->removeDirectory($packageVendorDir);
if (!$success) {
// @codeCoverageIgnoreStart
throw new RuntimeException('Could not completely delete ' . $path . ', aborting.');
// @codeCoverageIgnoreEnd
throw new RuntimeException('Could not completely delete ' . $path . ', aborting.'); // @codeCoverageIgnore
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/ComposerInstaller/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ class Plugin implements PluginInterface
*/
public function activate(Composer $composer, IOInterface $io)
{
$cmsInstaller = new CmsInstaller($io, $composer);
$composer->getInstallationManager()->addInstaller($cmsInstaller);

$pluginInstaller = new PluginInstaller($io, $composer);
$composer->getInstallationManager()->addInstaller($pluginInstaller);
$installationManager = $composer->getInstallationManager();
$installationManager->addInstaller(new CmsInstaller($io, $composer));
$installationManager->addInstaller(new PluginInstaller($io, $composer));
}
}
8 changes: 4 additions & 4 deletions src/ComposerInstaller/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function supports($packageType): bool
public function getInstallPath(PackageInterface $package): string
{
// place into `vendor` directory as usual if Pluginkit is not supported
if (!$this->supportsPluginkit($package)) {
if ($this->supportsPluginkit($package) !== true) {
return parent::getInstallPath($package);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public function getInstallPath(PackageInterface $package): string
protected function postInstall(PackageInterface $package)
{
// only continue if Pluginkit is supported
if (!$this->supportsPluginkit($package)) {
if ($this->supportsPluginkit($package) !== true) {
return;
}

Expand All @@ -82,8 +82,8 @@ protected function postInstall(PackageInterface $package)

/**
* Checks if the package has explicitly required this installer;
* otherwise the installer will fall back to the behavior of the LibraryInstaller
* (Pluginkit is not yet supported by the plugin)
* otherwise (if the Pluginkit is not yet supported by the plugin)
* the installer will fall back to the behavior of the LibraryInstaller
*
* @param PackageInterface $package
* @return bool
Expand Down
37 changes: 9 additions & 28 deletions tests/ComposerInstaller/CmsInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Kirby\ComposerInstaller;

use Composer\Package\Package;
use Composer\Package\RootPackage;
use Composer\Repository\InstalledArrayRepository;

class CmsInstallerTest extends InstallerTestCase
Expand All @@ -30,11 +29,9 @@ public function testGetInstallPathDefault()

public function testGetInstallPathCustomPaths()
{
$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$rootPackage->setExtra([
$this->initRootPackage()->setExtra([
'kirby-cms-path' => 'cms'
]);
$this->composer->setPackage($rootPackage);

$package = $this->cmsPackageFactory();
$this->assertEquals('cms', $this->installer->getInstallPath($package));
Expand All @@ -46,11 +43,9 @@ public function testGetInstallPathCustomPaths()
*/
public function testGetInstallPathUnsafe1()
{
$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$rootPackage->setExtra([
$this->initRootPackage()->setExtra([
'kirby-cms-path' => '.'
]);
$this->composer->setPackage($rootPackage);

$package = $this->cmsPackageFactory();
$this->installer->getInstallPath($package);
Expand All @@ -62,17 +57,9 @@ public function testGetInstallPathUnsafe1()
*/
public function testGetInstallPathUnsafe2()
{
$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$rootPackage->setExtra([
$this->initRootPackage()->setExtra([
'kirby-cms-path' => 'vendor'
]);
$this->composer->setPackage($rootPackage);

$this->composer->getConfig()->merge([
'config' => [
'vendor-dir' => 'vendor'
]
]);

$package = $this->cmsPackageFactory();
$this->installer->getInstallPath($package);
Expand All @@ -84,11 +71,9 @@ public function testGetInstallPathUnsafe2()
*/
public function testGetInstallPathUnsafe3()
{
$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$rootPackage->setExtra([
$this->initRootPackage()->setExtra([
'kirby-cms-path' => 'custom-vendor'
]);
$this->composer->setPackage($rootPackage);

$package = $this->cmsPackageFactory();
$this->assertEquals('custom-vendor', $this->installer->getInstallPath($package));
Expand All @@ -105,9 +90,6 @@ public function testGetInstallPathUnsafe3()

public function testInstall()
{
$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$this->composer->setPackage($rootPackage);

$package = $this->cmsPackageFactory();
$this->assertEquals('kirby', $this->installer->getInstallPath($package));
$this->installer->install(new InstalledArrayRepository(), $package);
Expand All @@ -116,13 +98,10 @@ public function testInstall()
$this->assertDirectoryNotExists($this->testDir . '/kirby/vendor');
}

public function testUpdateCode()
public function testUpdate()
{
$repo = new InstalledArrayRepository();

$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$this->composer->setPackage($rootPackage);

$initial = $this->cmsPackageFactory();
$this->assertEquals('kirby', $this->installer->getInstallPath($initial));
$this->installer->install($repo, $initial);
Expand All @@ -131,8 +110,10 @@ public function testUpdateCode()
$this->assertFileExists($this->testDir . '/kirby/vendor-created.txt');
$this->assertDirectoryNotExists($this->testDir . '/kirby/vendor');

unlink($this->testDir . '/kirby/index.php');
$this->filesystem->emptyDirectory($this->testDir . '/kirby');
$this->assertFileNotExists($this->testDir . '/kirby/index.php');
$this->assertFileNotExists($this->testDir . '/kirby/vendor-created.txt');
$this->assertDirectoryNotExists($this->testDir . '/kirby/vendor');

$target = $this->cmsPackageFactory();
$this->assertEquals('kirby', $this->installer->getInstallPath($target));
Expand All @@ -154,7 +135,7 @@ protected function cmsPackageFactory(): Package
$package->setInstallationSource('dist');
$package->setDistType('mock');
$package->setExtra([
'with-vendor-dir' => true
'with-vendor-dir' => true // tell the MockDownloader to create a `vendor` dir
]);

return $package;
Expand Down
22 changes: 18 additions & 4 deletions tests/ComposerInstaller/InstallerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
use Composer\Config;
use Composer\Downloader\DownloadManager;
use Composer\IO\NullIO;
use Composer\Package\RootPackage;
use Composer\Util\Filesystem;

class InstallerTestCase extends TestCase
{
protected $testDir;
protected $io;

protected $composer;
protected $filesystem;
protected $installer;
protected $io;
protected $rootPackage;

public function setUp()
{
Expand All @@ -32,14 +35,25 @@ public function setUp()
$this->composer->setDownloadManager($downloadManager);

// initialize test dir and switch to it to make relative paths work
if (!is_dir($this->testDir)) {
$this->filesystem->ensureDirectoryExists($this->testDir);
}
$this->filesystem->ensureDirectoryExists($this->testDir);
chdir($this->testDir);
}

public function tearDown()
{
$this->filesystem->removeDirectory($this->testDir);
}

/**
* Initializes a root Kirby site package and returns it
*
* @return RootPackage
*/
public function initRootPackage(): RootPackage
{
$rootPackage = new RootPackage('getkirby/amazing-site', '1.0.0.0', '1.0.0');
$this->composer->setPackage($rootPackage);

return $rootPackage;
}
}
Loading

0 comments on commit 162497e

Please sign in to comment.