Skip to content

Commit

Permalink
Merge pull request #225 from lemberg/feature/composer-2
Browse files Browse the repository at this point in the history
Add Composer 2 support
  • Loading branch information
T2L committed Nov 21, 2020
2 parents 7683fcb + 3d82856 commit 2e384d5
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 38 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/Integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:

jobs:
php:
name: Composer plugin with PHP ${{ matrix.php-version }}
name: Composer ${{ matrix.composer-version }} / PHP ${{ matrix.php-version }}

runs-on: ubuntu-latest

Expand All @@ -22,6 +22,9 @@ jobs:
- '7.2'
- '7.3'
- '7.4'
composer-version:
- 1
- 2
include:
- php-version: '7.2'
prefer-lowest: --prefer-lowest
Expand All @@ -41,14 +44,14 @@ jobs:
with:
coverage: none
php-version: ${{ matrix.php-version }}
tools: composer:v1, prestissimo
tools: composer:v${{ matrix.composer-version }}

- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache
key: php-${{ matrix.php-version }}-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php-version }}-
key: php-${{ matrix.php-version }}-${{ matrix.composer-version }}-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php-version }}-${{ matrix.composer-version }}-

- name: Install Composer dependencies
env:
Expand All @@ -65,7 +68,7 @@ jobs:
run: vendor/bin/phpunit

molecule:
name: Provision ubuntu/${{ matrix.molecule-platform }} with Molecule
name: Molecule ubuntu/${{ matrix.molecule-platform }}

needs: php

Expand Down Expand Up @@ -130,7 +133,7 @@ jobs:
MOLECULE_PLATFORM: ${{ matrix.molecule-platform }}

vagrant:
name: Provision VM with Vagrantfile
name: Vagrant up

needs: php

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Draft Environment (Unreleased)

Updates:
- [GH-219](https://github.com/lemberg/draft-environment/issues/219) - Add support for Composer 2
- [GH-226](https://github.com/lemberg/draft-environment/issues/226) - Migrate from Travis to Github Actions

Fixes:
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"license": "GPL-2.0-or-later",
"require": {
"php": "^7.2",
"composer-plugin-api": "^1.1",
"composer-plugin-api": "^1.1 || ^2.0",
"nette/robot-loader": "^3.2",
"symfony/filesystem": "^3.4 || ^4.4 || ^5",
"symfony/yaml": "^3.4 || ^4.4 || ^5",
"t2l/comments": "^1.1.0"
},
"require-dev": {
"composer/composer": "^1.10",
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.0",
"dg/bypass-finals": "^1.1",
"composer/composer": "^1.10 || ^2.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"dg/bypass-finals": "^1.2.2",
"drupal/coder": "^8.3.8",
"ergebnis/composer-normalize": "^2.3",
"ergebnis/phpstan-rules": "^0.14",
Expand Down Expand Up @@ -47,6 +47,7 @@
"psr-4": {
"Lemberg\\Tests\\Extensions\\Draft\\Environment\\": "tests/Extensions",
"Lemberg\\Tests\\Functional\\Draft\\Environment\\": "tests/Functional",
"Lemberg\\Tests\\Traits\\Draft\\Environment\\": "tests/Traits",
"Lemberg\\Tests\\Unit\\Draft\\Environment\\": "tests/Unit"
}
},
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ parameters:
paths:
- src
- tests
excludes_analyse:
- tests/Traits/ComposerPackageEventFactoryTrait.php
dynamicConstantNames:
- Composer\Plugin\PluginInterface::PLUGIN_API_VERSION
ergebnis:
classesAllowedToBeExtended:
- Lemberg\Draft\Environment\Config\Manager\AbstractConfigManager
Expand Down
14 changes: 14 additions & 0 deletions src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function activate(Composer $composer, IOInterface $io): void {
$this->setApp(new App($composer, $io, $configInstallManager, $configUpdateManager));
}

/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io): void {
// This method is part of the Composer 2 Plugin API.
}

/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io): void {
// This method is part of the Composer 2 Plugin API.
}

/**
* Returns an array of event names this subscriber wants to listen to.
*
Expand Down
1 change: 0 additions & 1 deletion tests/Functional/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function testComposerInstallAndRemove(): void {
'vendor/bin/composer', 'install',
'--prefer-dist',
'--no-interaction',
'--no-suggest',
'--working-dir', $this->workingDir,
]))
->mustRun();
Expand Down
19 changes: 8 additions & 11 deletions tests/Functional/Config/Manager/ConfigManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function testComposerUpdate(string $directory): void {

$this->fs->mirror("$this->basePath/$directory", $this->workingDir, NULL, ['override' => TRUE]);

// Uncompress vendor packages.
$zip = new \ZipArchive();
if ($zip->open("$this->basePath/$directory/vendor.zip") === TRUE) {
$zip->extractTo($this->workingDir);
$zip->close();
}
self::assertDirectoryExists("$this->workingDir/vendor");

// Link package working directory, so tests runs against the proper
// source code.
(new Process([
Expand All @@ -41,16 +49,6 @@ public function testComposerUpdate(string $directory): void {
]))
->mustRun();

// Run composer install.
(new Process([
'vendor/bin/composer', 'install',
'--prefer-dist',
'--no-interaction',
'--no-suggest',
'--working-dir', $this->workingDir,
]))
->mustRun();

// Get the current working branch to test against.
$working_branch = (new Process([
'git', 'rev-parse', '--abbrev-ref', 'HEAD',
Expand All @@ -62,7 +60,6 @@ public function testComposerUpdate(string $directory): void {
(new Process([
'vendor/bin/composer', 'require',
'--dev', App::PACKAGE_NAME . ':' . rtrim($working_branch) . '-dev',
'--no-suggest',
'--working-dir', $this->workingDir,
]))
->mustRun();
Expand Down
1 change: 0 additions & 1 deletion tests/Functional/Config/Manager/InstallManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function testComposerInstall(): void {
'vendor/bin/composer', 'install',
'--prefer-dist',
'--no-interaction',
'--no-suggest',
'--working-dir', $this->workingDir,
]))
->mustRun();
Expand Down
57 changes: 57 additions & 0 deletions tests/Traits/ComposerPackageEventFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Lemberg\Tests\Traits\Draft\Environment;

use Composer\Composer;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\PolicyInterface;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Repository\RepositoryInterface;

/**
* Create an instance of the PackageEvent.
*/
trait ComposerPackageEventFactoryTrait {

/**
* Create an instance of the PackageEvent based on the Composer version it is
* being used with.
*
* @param string $eventName
* Package event name.
* @param \Composer\Composer $composer
* Composer instance.
* @param \Composer\IO\IOInterface $io
* IO instance.
* @param bool $devMode
* Boolean indicating whether dev mode is enabled or not.
* @param \Composer\DependencyResolver\PolicyInterface $policy
* Composer policy.
* @param \Composer\DependencyResolver\Pool $pool
* Composer dependency pool.
* @param \Composer\Repository\RepositoryInterface $repository
* Composer repository.
* @param \Composer\DependencyResolver\Request $request
* Current request.
* @param \Composer\DependencyResolver\Operation\OperationInterface[] $operations
* Array of operations being run.
* @param \Composer\DependencyResolver\Operation\OperationInterface $operation
* Operation triggering the event.
*
* @return \Composer\Installer\PackageEvent
* Instance of the PackageEvent.
*/
private function createPackageEvent(string $eventName, Composer $composer, IOInterface $io, bool $devMode, PolicyInterface $policy, Pool $pool, RepositoryInterface $repository, Request $request, array $operations, OperationInterface $operation): PackageEvent {
if (version_compare(PluginInterface::PLUGIN_API_VERSION, '2.0.0', '>=')) {
return new PackageEvent($eventName, $composer, $io, $devMode, $repository, $operations, $operation);
}
return new PackageEvent($eventName, $composer, $io, $devMode, $policy, $pool, $repository, $request, $operations, $operation);
}

}
26 changes: 14 additions & 12 deletions tests/Unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Composer\DependencyResolver\PolicyInterface;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\Package;
Expand All @@ -24,6 +23,7 @@
use Lemberg\Draft\Environment\App;
use Lemberg\Draft\Environment\Config\Manager\InstallManagerInterface;
use Lemberg\Draft\Environment\Config\Manager\UpdateManagerInterface;
use Lemberg\Tests\Traits\Draft\Environment\ComposerPackageEventFactoryTrait;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -33,6 +33,8 @@
*/
final class AppTest extends TestCase {

use ComposerPackageEventFactoryTrait;

/**
* @var \Composer\Composer
*/
Expand Down Expand Up @@ -127,7 +129,7 @@ public function testComposerPrePackageUninstallEventHandlerDoesNotRunWithOtherPa
// "lemberg/draft-environment" is being uninstalled.
$package = new Package('dummy', '1.0.0.0', '^1.0');
$operation = new UninstallOperation($package);
$event = new PackageEvent(PackageEvents::PRE_PACKAGE_UNINSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = $this->createPackageEvent(PackageEvents::PRE_PACKAGE_UNINSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$this->configInstallManager
->expects(self::never())
->method('uninstall');
Expand All @@ -142,7 +144,7 @@ public function testComposerPrePackageUninstallEventHandlerDoesNotRunWithOtherEv
// PackageEvents::PRE_PACKAGE_UNINSTALL event is dispatched.
$package = new Package('dummy', '1.0.0.0', '^1.0');
$operation = new InstallOperation($package);
$event = new PackageEvent(PackageEvents::PRE_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = $this->createPackageEvent(PackageEvents::PRE_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$this->configInstallManager
->expects(self::never())
->method('uninstall');
Expand All @@ -156,7 +158,7 @@ public function testComposerPrePackageUninstallEventHandlerDoesRun(): void {
// Clean up must run when "lemberg/draft-environment" is being uninstalled.
$package = new Package(App::PACKAGE_NAME, '1.0.0.0', '^1.0');
$operation = new UninstallOperation($package);
$event = new PackageEvent(PackageEvents::PRE_PACKAGE_UNINSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = $this->createPackageEvent(PackageEvents::PRE_PACKAGE_UNINSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$this->configInstallManager
->expects(self::once())
->method('uninstall');
Expand All @@ -172,7 +174,7 @@ public function testComposerPostPackageUpdateEventHandlerDoesNotRunWithOtherPack
$initial = new Package('dummy', '1.0.0.0', '^1.0');
$target = new Package('dummy', '1.2.0.0', '^1.0');
$operation = new UpdateOperation($initial, $target);
$packageEvent = new PackageEvent(PackageEvents::POST_PACKAGE_UPDATE, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::POST_PACKAGE_UPDATE, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

$this->configUpdateManager
Expand All @@ -191,7 +193,7 @@ public function testComposerPostPackageUpdateEventHandlerDoesNotRunWithOtherEven
// PackageEvents::PRE_PACKAGE_UNINSTALL event is dispatched.
$initial = new Package(App::PACKAGE_NAME, '1.0.0.0', '^1.0');
$operation = new InstallOperation($initial);
$packageEvent = new PackageEvent(PackageEvents::PRE_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::PRE_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

$this->configUpdateManager
Expand All @@ -212,7 +214,7 @@ public function testComposerPostPackageUpdateEventHandlerDoesNotRunWhenDowngradi
$target = new Package(App::PACKAGE_NAME, '1.2.0.0', '^1.0');
$target->setReleaseDate(new \DateTime('yesterday'));
$operation = new UpdateOperation($initial, $target);
$packageEvent = new PackageEvent(PackageEvents::POST_PACKAGE_UPDATE, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::POST_PACKAGE_UPDATE, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

$this->configUpdateManager
Expand All @@ -231,7 +233,7 @@ public function testComposerPostPackageUpdateEventHandlerDoesRun(): void {
$initial = new Package(App::PACKAGE_NAME, '1.0.0.0', '^1.0');
$target = new Package(App::PACKAGE_NAME, '1.2.0.0', '^1.0');
$operation = new UpdateOperation($initial, $target);
$packageEvent = new PackageEvent(PackageEvents::POST_PACKAGE_UPDATE, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::POST_PACKAGE_UPDATE, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

$this->configUpdateManager
Expand Down Expand Up @@ -265,7 +267,7 @@ public function testComposerPostDumpAutoloadCommandEventHandlerDoesNotRunWithOth
// itself is being installed.
$package = new Package(App::PACKAGE_NAME, '1.0.0.0', '^1.0');
$operation = new InstallOperation($package);
$packageEvent = new PackageEvent(PackageEvents::POST_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::POST_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_ARCHIVE_CMD, $this->composer, $this->io);

$this->configInstallManager
Expand All @@ -284,7 +286,7 @@ public function testComposerPostDumpAutoloadCommandEventHandlerDoesNotRunWithOth
// itself is being installed.
$package = new Package(App::PACKAGE_NAME, '1.0.0.0', '^1.0');
$operation = new InstallOperation($package);
$packageEvent = new PackageEvent(PackageEvents::PRE_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::PRE_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

$this->configInstallManager
Expand All @@ -302,7 +304,7 @@ public function testComposerPostDumpAutoloadCommandEventHandlerDoesNotRunWithOth
// Install should not run if any other package is being installed.
$package = new Package('dummy', '1.0.0.0', '^1.0');
$operation = new InstallOperation($package);
$packageEvent = new PackageEvent(PackageEvents::POST_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::POST_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

$this->configInstallManager
Expand All @@ -319,7 +321,7 @@ public function testComposerPostDumpAutoloadCommandEventHandlerDoesNotRunWithOth
public function testComposerPostDumpAutoloadCommandEventHandlerDoesRun(): void {
$package = new Package(App::PACKAGE_NAME, '1.0.0.0', '^1.0');
$operation = new InstallOperation($package);
$packageEvent = new PackageEvent(PackageEvents::POST_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$packageEvent = $this->createPackageEvent(PackageEvents::POST_PACKAGE_INSTALL, $this->composer, $this->io, FALSE, $this->policy, $this->pool, $this->installedRepo, $this->request, [$operation], $operation);
$event = new ScriptEvent(ScriptEvents::POST_AUTOLOAD_DUMP, $this->composer, $this->io);

// Install should run.
Expand Down

0 comments on commit 2e384d5

Please sign in to comment.