From d3b1295e499e2f18880553800eed8f478fac44a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Sat, 18 Feb 2017 21:22:12 +0100 Subject: [PATCH] Optimize the config of plugin --- Composer/ScriptHandler.php | 22 ++++++- Config/Config.php | 63 +++++++++++++++++++++ Util/Config.php => Config/ConfigBuilder.php | 60 +++++++------------- FxpAssetPlugin.php | 29 ++++++++-- Installer/AssetInstaller.php | 19 +++++-- Installer/IgnoreFactory.php | 7 ++- Repository/BowerPrivateRegistryFactory.php | 13 ++--- Repository/DefaultRegistryFactory.php | 8 +-- Repository/FilterUtil.php | 10 ++-- Repository/RegistryFactoryInterface.php | 10 ++-- Repository/VcsPackageFilter.php | 17 ++++-- Tests/Composer/ScriptHandlerTest.php | 62 +++++++++++++++++++- Tests/{Util => Config}/ConfigTest.php | 27 ++++++--- Tests/FxpAssetPluginTest.php | 8 +++ Tests/Installer/AssetInstallerTest.php | 8 ++- Tests/Installer/BowerInstallerTest.php | 20 ++++--- Tests/Installer/IgnoreFactoryTest.php | 19 +++++-- Tests/Repository/VcsPackageFilterTest.php | 18 +++++- Util/AssetPlugin.php | 29 +++++----- 19 files changed, 324 insertions(+), 125 deletions(-) create mode 100644 Config/Config.php rename Util/Config.php => Config/ConfigBuilder.php (62%) rename Tests/{Util => Config}/ConfigTest.php (79%) diff --git a/Composer/ScriptHandler.php b/Composer/ScriptHandler.php index bffc4d99..1a2ca951 100644 --- a/Composer/ScriptHandler.php +++ b/Composer/ScriptHandler.php @@ -17,6 +17,8 @@ use Composer\Installer\PackageEvent; use Composer\Package\PackageInterface; use Fxp\Composer\AssetPlugin\Assets; +use Fxp\Composer\AssetPlugin\Config\Config; +use Fxp\Composer\AssetPlugin\FxpAssetPlugin; use Fxp\Composer\AssetPlugin\Installer\IgnoreFactory; /** @@ -39,10 +41,28 @@ public static function deleteIgnoredFiles(PackageEvent $event) } $section = static::getIgnoreConfigSection(); - $manager = IgnoreFactory::create($event->getComposer(), $package, null, $section); + $manager = IgnoreFactory::create(static::getConfig($event), $event->getComposer(), $package, null, $section); $manager->cleanup(); } + /** + * Get the plugin config. + * + * @param PackageEvent $event + * + * @return Config + */ + public static function getConfig(PackageEvent $event) + { + foreach ($event->getComposer()->getPluginManager()->getPlugins() as $plugin) { + if ($plugin instanceof FxpAssetPlugin) { + return $plugin->getConfig(); + } + } + + throw new \RuntimeException('The fxp composer asset plugin is not found'); + } + /** * Get the root config section of igore file patterns for each package. * diff --git a/Config/Config.php b/Config/Config.php new file mode 100644 index 00000000..c74282c2 --- /dev/null +++ b/Config/Config.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fxp\Composer\AssetPlugin\Config; + +/** + * Helper of package config. + * + * @author François Pluchino + */ +final class Config +{ + /** + * @var array + */ + private $config; + + /** + * Constructor. + * + * @param array $config The config + */ + public function __construct(array $config) + { + $this->config = $config; + } + + /** + * Get the array config value. + * + * @param string $key The config key + * @param array $default The default value + * + * @return array + */ + public function getArray($key, array $default = array()) + { + return $this->get($key, $default); + } + + /** + * Get the config value. + * + * @param string $key The config key + * @param mixed|null $default The default value + * + * @return mixed|null + */ + public function get($key, $default = null) + { + return array_key_exists($key, $this->config) + ? $this->config[$key] + : $default; + } +} diff --git a/Util/Config.php b/Config/ConfigBuilder.php similarity index 62% rename from Util/Config.php rename to Config/ConfigBuilder.php index f6017365..a4bba51f 100644 --- a/Util/Config.php +++ b/Config/ConfigBuilder.php @@ -9,17 +9,18 @@ * file that was distributed with this source code. */ -namespace Fxp\Composer\AssetPlugin\Util; +namespace Fxp\Composer\AssetPlugin\Config; +use Composer\Composer; use Composer\IO\IOInterface; use Composer\Package\RootPackageInterface; /** - * Helper of package config. + * Plugin Config builder. * * @author François Pluchino */ -abstract class Config +abstract class ConfigBuilder { /** * List of the deprecated options. @@ -60,54 +61,33 @@ public static function validate(IOInterface $io, RootPackageInterface $package, } /** - * Get the array config value. + * Build the config of plugin. * - * @param RootPackageInterface $package The root package - * @param string $key The config key - * @param array $default The default value + * @param Composer $composer The composer * - * @return array - */ - public static function getArray(RootPackageInterface $package, $key, array $default = array()) - { - return (array) static::get($package, $key, $default); - } - - /** - * Get the config value. - * - * @param RootPackageInterface $package The root package - * @param string $key The config key - * @param mixed|null $default The default value - * - * @return mixed|null + * @return Config */ - public static function get(RootPackageInterface $package, $key, $default = null) + public static function build(Composer $composer) { - $config = self::injectDeprecatedConfig(self::getConfigBase($package), (array) $package->getExtra(), $key); + $config = self::injectDeprecatedConfig(self::getConfigBase($composer), (array) $composer->getPackage()->getExtra()); - return array_key_exists($key, $config) - ? $config[$key] - : $default; + return new Config($config); } /** * Inject the deprecated keys in config if the config keys are not present. * - * @param array $config The - * @param array $extra - * @param string $key + * @param array $config The config + * @param array $extra The root package extra section * * @return array */ - private static function injectDeprecatedConfig(array $config, array $extra, $key) + private static function injectDeprecatedConfig(array $config, array $extra) { - $deprecatedKey = isset(self::$deprecatedOptions[$key]) - ? self::$deprecatedOptions[$key] - : 'asset-'.$key; - - if (array_key_exists($deprecatedKey, $extra) && !array_key_exists($key, $config)) { - $config[$key] = $extra[$deprecatedKey]; + foreach (self::$deprecatedOptions as $key => $deprecatedKey) { + if (array_key_exists($deprecatedKey, $extra) && !array_key_exists($key, $config)) { + $config[$key] = $extra[$deprecatedKey]; + } } return $config; @@ -116,13 +96,13 @@ private static function injectDeprecatedConfig(array $config, array $extra, $key /** * Get the base of data. * - * @param RootPackageInterface $package The config data + * @param Composer $composer The compser * * @return array */ - private static function getConfigBase($package) + private static function getConfigBase(Composer $composer) { - $config = $package->getConfig(); + $config = $composer->getPackage()->getConfig(); return isset($config['fxp-asset']) && is_array($config['fxp-asset']) ? $config['fxp-asset'] diff --git a/FxpAssetPlugin.php b/FxpAssetPlugin.php index 2b41c9f1..4183dc6d 100644 --- a/FxpAssetPlugin.php +++ b/FxpAssetPlugin.php @@ -20,10 +20,11 @@ use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginInterface; use Composer\Repository\InstalledFilesystemRepository; +use Fxp\Composer\AssetPlugin\Config\Config; +use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager; use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter; use Fxp\Composer\AssetPlugin\Util\AssetPlugin; -use Fxp\Composer\AssetPlugin\Util\Config; /** * Composer plugin. @@ -32,6 +33,11 @@ */ class FxpAssetPlugin implements PluginInterface, EventSubscriberInterface { + /** + * @var Config + */ + protected $config; + /** * @var Composer */ @@ -74,17 +80,18 @@ public function activate(Composer $composer, IOInterface $io) { /* @var InstalledFilesystemRepository $installedRepository */ $installedRepository = $composer->getRepositoryManager()->getLocalRepository(); + $this->config = ConfigBuilder::build($composer); $this->composer = $composer; $this->io = $io; - $this->packageFilter = new VcsPackageFilter($composer->getPackage(), $composer->getInstallationManager(), $installedRepository); + $this->packageFilter = new VcsPackageFilter($this->config, $composer->getPackage(), $composer->getInstallationManager(), $installedRepository); $this->assetRepositoryManager = new AssetRepositoryManager($io, $composer->getRepositoryManager(), $this->packageFilter); - AssetPlugin::addRegistryRepositories($this->assetRepositoryManager, $this->packageFilter, $composer->getPackage()); + AssetPlugin::addRegistryRepositories($this->assetRepositoryManager, $this->packageFilter, $this->config); AssetPlugin::setVcsTypeRepositories($composer->getRepositoryManager()); - $this->assetRepositoryManager->addRepositories(Config::getArray($composer->getPackage(), 'repositories')); + $this->assetRepositoryManager->addRepositories($this->config->getArray('repositories')); - AssetPlugin::addInstallers($composer, $io); + AssetPlugin::addInstallers($this->config, $composer, $io); } /** @@ -94,7 +101,7 @@ public function activate(Composer $composer, IOInterface $io) */ public function onPluginCommand(CommandEvent $event) { - Config::validate($this->io, $this->composer->getPackage(), $event->getCommandName()); + ConfigBuilder::validate($this->io, $this->composer->getPackage(), $event->getCommandName()); if (!in_array($event->getCommandName(), array('install', 'update'))) { $this->packageFilter->setEnabled(false); @@ -110,4 +117,14 @@ public function onPreDependenciesSolving(InstallerEvent $event) { $this->assetRepositoryManager->setPool($event->getPool()); } + + /** + * Get the plugin config. + * + * @return Config + */ + public function getConfig() + { + return $this->config; + } } diff --git a/Installer/AssetInstaller.php b/Installer/AssetInstaller.php index 40296515..3773426c 100644 --- a/Installer/AssetInstaller.php +++ b/Installer/AssetInstaller.php @@ -16,9 +16,9 @@ use Composer\IO\IOInterface; use Composer\Package\PackageInterface; use Composer\Util\Filesystem; +use Fxp\Composer\AssetPlugin\Config\Config; use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface; use Fxp\Composer\AssetPlugin\Util\AssetPlugin; -use Fxp\Composer\AssetPlugin\Util\Config; /** * Installer for asset packages. @@ -28,19 +28,26 @@ */ class AssetInstaller extends LibraryInstaller { + /** + * @var Config + */ + private $config; + /** * Constructor. * + * @param Config $config * @param IOInterface $io * @param Composer $composer * @param AssetTypeInterface $assetType * @param Filesystem $filesystem */ - public function __construct(IOInterface $io, Composer $composer, AssetTypeInterface $assetType, Filesystem $filesystem = null) + public function __construct(Config $config, IOInterface $io, Composer $composer, AssetTypeInterface $assetType, Filesystem $filesystem = null) { parent::__construct($io, $composer, $assetType->getComposerType(), $filesystem); - $paths = Config::getArray($composer->getPackage(), 'installer-paths'); + $this->config = $config; + $paths = $this->config->getArray('installer-paths'); if (!empty($paths[$this->type])) { $this->vendorDir = rtrim($paths[$this->type], '/'); @@ -84,7 +91,7 @@ protected function getPackageBasePath(PackageInterface $package) */ protected function installCode(PackageInterface $package) { - $package = AssetPlugin::addMainFiles($this->composer, $package); + $package = AssetPlugin::addMainFiles($this->config, $package); parent::installCode($package); @@ -96,7 +103,7 @@ protected function installCode(PackageInterface $package) */ protected function updateCode(PackageInterface $initial, PackageInterface $target) { - $target = AssetPlugin::addMainFiles($this->composer, $target); + $target = AssetPlugin::addMainFiles($this->config, $target); parent::updateCode($initial, $target); @@ -110,7 +117,7 @@ protected function updateCode(PackageInterface $initial, PackageInterface $targe */ protected function deleteIgnoredFiles(PackageInterface $package) { - $manager = IgnoreFactory::create($this->composer, $package, $this->getInstallPath($package)); + $manager = IgnoreFactory::create($this->config, $this->composer, $package, $this->getInstallPath($package)); if ($manager->isEnabled() && !$manager->hasPattern()) { $this->addIgnorePatterns($manager, $package); diff --git a/Installer/IgnoreFactory.php b/Installer/IgnoreFactory.php index 56413dae..9b213a51 100644 --- a/Installer/IgnoreFactory.php +++ b/Installer/IgnoreFactory.php @@ -13,7 +13,7 @@ use Composer\Composer; use Composer\Package\PackageInterface; -use Fxp\Composer\AssetPlugin\Util\Config; +use Fxp\Composer\AssetPlugin\Config\Config; /** * Factory of ignore manager patterns. @@ -25,6 +25,7 @@ class IgnoreFactory /** * Create a ignore manager. * + * @param Config $config The plugin config * @param Composer $composer The composer instance * @param PackageInterface $package The package instance * @param string|null $installDir The custom installation directory @@ -32,11 +33,11 @@ class IgnoreFactory * * @return IgnoreManager */ - public static function create(Composer $composer, PackageInterface $package, $installDir = null, $section = 'ignore-files') + public static function create(Config $config, Composer $composer, PackageInterface $package, $installDir = null, $section = 'ignore-files') { $installDir = static::getInstallDir($composer, $package, $installDir); $manager = new IgnoreManager($installDir); - $config = Config::getArray($composer->getPackage(), $section); + $config = $config->getArray($section); foreach ($config as $packageName => $patterns) { if ($packageName === $package->getName()) { diff --git a/Repository/BowerPrivateRegistryFactory.php b/Repository/BowerPrivateRegistryFactory.php index d8f6f3b2..e6f838d3 100644 --- a/Repository/BowerPrivateRegistryFactory.php +++ b/Repository/BowerPrivateRegistryFactory.php @@ -11,9 +11,8 @@ namespace Fxp\Composer\AssetPlugin\Repository; -use Composer\Package\RootPackageInterface; +use Fxp\Composer\AssetPlugin\Config\Config; use Fxp\Composer\AssetPlugin\Util\AssetPlugin; -use Fxp\Composer\AssetPlugin\Util\Config; /** * Factory of bower private repository registries. @@ -25,17 +24,17 @@ class BowerPrivateRegistryFactory implements RegistryFactoryInterface /** * {@inheritdoc} */ - public static function create(AssetRepositoryManager $arm, VcsPackageFilter $filter, RootPackageInterface $package) + public static function create(AssetRepositoryManager $arm, VcsPackageFilter $filter, Config $config) { $rm = $arm->getRepositoryManager(); - $registries = Config::getArray($package, 'private-bower-registries'); + $registries = $config->getArray('private-bower-registries'); foreach ($registries as $registryName => $registryUrl) { - $config = AssetPlugin::createRepositoryConfig($arm, $filter, $package, $registryName); - $config['private-registry-url'] = $registryUrl; + $repoConfig = AssetPlugin::createRepositoryConfig($arm, $filter, $config, $registryName); + $repoConfig['private-registry-url'] = $registryUrl; $rm->setRepositoryClass($registryName, 'Fxp\Composer\AssetPlugin\Repository\BowerPrivateRepository'); - $rm->addRepository($rm->createRepository($registryName, $config)); + $rm->addRepository($rm->createRepository($registryName, $repoConfig)); } } } diff --git a/Repository/DefaultRegistryFactory.php b/Repository/DefaultRegistryFactory.php index dc865297..d3a8549b 100644 --- a/Repository/DefaultRegistryFactory.php +++ b/Repository/DefaultRegistryFactory.php @@ -11,8 +11,8 @@ namespace Fxp\Composer\AssetPlugin\Repository; -use Composer\Package\RootPackageInterface; use Fxp\Composer\AssetPlugin\Assets; +use Fxp\Composer\AssetPlugin\Config\Config; use Fxp\Composer\AssetPlugin\Util\AssetPlugin; /** @@ -25,15 +25,15 @@ class DefaultRegistryFactory implements RegistryFactoryInterface /** * {@inheritdoc} */ - public static function create(AssetRepositoryManager $arm, VcsPackageFilter $filter, RootPackageInterface $package) + public static function create(AssetRepositoryManager $arm, VcsPackageFilter $filter, Config $config) { $rm = $arm->getRepositoryManager(); foreach (Assets::getDefaultRegistries() as $assetType => $registryClass) { - $config = AssetPlugin::createRepositoryConfig($arm, $filter, $package, $assetType); + $repoConfig = AssetPlugin::createRepositoryConfig($arm, $filter, $config, $assetType); $rm->setRepositoryClass($assetType, $registryClass); - $rm->addRepository($rm->createRepository($assetType, $config)); + $rm->addRepository($rm->createRepository($assetType, $repoConfig)); } } } diff --git a/Repository/FilterUtil.php b/Repository/FilterUtil.php index 5d2d4572..c81da736 100644 --- a/Repository/FilterUtil.php +++ b/Repository/FilterUtil.php @@ -15,8 +15,8 @@ use Composer\Package\Package; use Composer\Package\RootPackageInterface; use Composer\Semver\Constraint\ConstraintInterface; +use Fxp\Composer\AssetPlugin\Config\Config; use Fxp\Composer\AssetPlugin\Package\Version\VersionParser; -use Fxp\Composer\AssetPlugin\Util\Config; /** * Helper for Filter Package of Repository. @@ -113,13 +113,13 @@ public static function getMinimumStabilityFlag(RootPackageInterface $package, Li /** * Check the config option. * - * @param RootPackageInterface $package The root package - * @param string $name The extra option name + * @param Config $config The plugin config + * @param string $name The extra option name * * @return bool */ - public static function checkConfigOption(RootPackageInterface $package, $name) + public static function checkConfigOption(Config $config, $name) { - return true === Config::get($package, $name, true); + return true === $config->get($name, true); } } diff --git a/Repository/RegistryFactoryInterface.php b/Repository/RegistryFactoryInterface.php index 8f22561a..1a83fa84 100644 --- a/Repository/RegistryFactoryInterface.php +++ b/Repository/RegistryFactoryInterface.php @@ -11,7 +11,7 @@ namespace Fxp\Composer\AssetPlugin\Repository; -use Composer\Package\RootPackageInterface; +use Fxp\Composer\AssetPlugin\Config\Config; /** * Interface of repository registry factory. @@ -23,9 +23,9 @@ interface RegistryFactoryInterface /** * Create the repository registries. * - * @param AssetRepositoryManager $arm The asset repository manager - * @param VcsPackageFilter $filter The vcs package filter - * @param RootPackageInterface $package The root package + * @param AssetRepositoryManager $arm The asset repository manager + * @param VcsPackageFilter $filter The vcs package filter + * @param Config $config The plugin config */ - public static function create(AssetRepositoryManager $arm, VcsPackageFilter $filter, RootPackageInterface $package); + public static function create(AssetRepositoryManager $arm, VcsPackageFilter $filter, Config $config); } diff --git a/Repository/VcsPackageFilter.php b/Repository/VcsPackageFilter.php index 1656343b..51cbc12a 100644 --- a/Repository/VcsPackageFilter.php +++ b/Repository/VcsPackageFilter.php @@ -19,9 +19,9 @@ use Composer\Package\RootPackageInterface; use Composer\Repository\InstalledFilesystemRepository; use Composer\Semver\Constraint\MultiConstraint; +use Fxp\Composer\AssetPlugin\Config\Config; use Fxp\Composer\AssetPlugin\Package\Version\VersionParser; use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface; -use Fxp\Composer\AssetPlugin\Util\Config; /** * Filters the asset packages imported into VCS repository to optimize @@ -31,6 +31,11 @@ */ class VcsPackageFilter { + /** + * @var Config + */ + protected $config; + /** * @var RootPackageInterface */ @@ -69,12 +74,14 @@ class VcsPackageFilter /** * Constructor. * + * @param Config $config The plugin config * @param RootPackageInterface $package The root package * @param InstallationManager $installationManager The installation manager * @param InstalledFilesystemRepository|null $installedRepository The installed repository */ - public function __construct(RootPackageInterface $package, InstallationManager $installationManager, InstalledFilesystemRepository $installedRepository = null) + public function __construct(Config $config, RootPackageInterface $package, InstallationManager $installationManager, InstalledFilesystemRepository $installedRepository = null) { + $this->config = $config; $this->package = $package; $this->installationManager = $installationManager; $this->installedRepository = $installedRepository; @@ -173,7 +180,7 @@ protected function satisfy(Link $require, $normalizedVersion) */ protected function skipByPattern() { - $skip = Config::get($this->package, 'pattern-skip-version', false); + $skip = $this->config->get('pattern-skip-version', false); return is_string($skip) ? trim($skip, '/') @@ -257,7 +264,7 @@ protected function initialize() ); if (null !== $this->installedRepository - && FilterUtil::checkConfigOption($this->package, 'optimize-with-installed-packages')) { + && FilterUtil::checkConfigOption($this->config, 'optimize-with-installed-packages')) { $this->initInstalledPackages(); } } @@ -292,7 +299,7 @@ private function includeRootConstraint(PackageInterface $package, Link $link) if (isset($this->requires[$package->getName()])) { /* @var Link $rLink */ $rLink = $this->requires[$package->getName()]; - $useConjunctive = FilterUtil::checkConfigOption($this->package, 'optimize-with-conjunctive'); + $useConjunctive = FilterUtil::checkConfigOption($this->config, 'optimize-with-conjunctive'); $constraint = new MultiConstraint(array($rLink->getConstraint(), $link->getConstraint()), $useConjunctive); $link = new Link($rLink->getSource(), $rLink->getTarget(), $constraint, 'installed', $constraint->getPrettyString()); } diff --git a/Tests/Composer/ScriptHandlerTest.php b/Tests/Composer/ScriptHandlerTest.php index d5558557..f32d6295 100644 --- a/Tests/Composer/ScriptHandlerTest.php +++ b/Tests/Composer/ScriptHandlerTest.php @@ -21,8 +21,11 @@ use Composer\Installer\PackageEvent; use Composer\IO\IOInterface; use Composer\Package\PackageInterface; +use Composer\Plugin\PluginManager; use Composer\Repository\CompositeRepository; use Fxp\Composer\AssetPlugin\Composer\ScriptHandler; +use Fxp\Composer\AssetPlugin\Config\Config; +use Fxp\Composer\AssetPlugin\FxpAssetPlugin; /** * Tests for the composer script handler. @@ -36,6 +39,11 @@ class ScriptHandlerTest extends \PHPUnit_Framework_TestCase */ protected $composer; + /** + * @var Config + */ + protected $config; + /** * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -57,8 +65,8 @@ public function setUp() $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $this->package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $config = $this->getMockBuilder('Composer\Config')->getMock(); - $config->expects($this->any()) + $this->config = $this->getMockBuilder('Composer\Config')->getMock(); + $this->config->expects($this->any()) ->method('get') ->will($this->returnCallback(function ($key) { $val = null; @@ -77,10 +85,24 @@ public function setUp() $this->composer->expects($this->any()) ->method('getConfig') - ->will($this->returnValue($config)); + ->will($this->returnValue($this->config)); $this->composer->expects($this->any()) ->method('getPackage') ->will($this->returnValue($rootPackage)); + + $plugin = $this->getMockBuilder(FxpAssetPlugin::class)->disableOriginalConstructor()->getMock(); + $plugin->expects($this->any()) + ->method('getConfig') + ->willReturn(new Config(array())); + + $pm = $this->getMockBuilder(PluginManager::class)->disableOriginalConstructor()->getMock(); + $pm->expects($this->any()) + ->method('getPlugins') + ->willReturn(array($plugin)); + + $this->composer->expects($this->any()) + ->method('getPluginManager') + ->will($this->returnValue($pm)); } public function tearDown() @@ -140,6 +162,40 @@ public function testDeleteIgnoreFilesWithUpdateOperation($composerType) ScriptHandler::deleteIgnoredFiles($this->createEvent($composerType)); } + /** + * @dataProvider getPackageComposerTypes + * + * @param string $composerType + * + * @expectedException \RuntimeException + * @expectedExceptionMessage The fxp composer asset plugin is not found + */ + public function testGetConfig($composerType) + { + $rootPackage = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); + + $this->composer = $this->getMockBuilder('Composer\Composer')->getMock(); + $this->composer->expects($this->any()) + ->method('getConfig') + ->will($this->returnValue($this->config)); + $this->composer->expects($this->any()) + ->method('getPackage') + ->will($this->returnValue($rootPackage)); + + $pm = $this->getMockBuilder(PluginManager::class)->disableOriginalConstructor()->getMock(); + $pm->expects($this->any()) + ->method('getPlugins') + ->willReturn(array()); + + $this->composer->expects($this->any()) + ->method('getPluginManager') + ->will($this->returnValue($pm)); + + $this->operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\OperationInterface')->getMock(); + + ScriptHandler::getConfig($this->createEvent($composerType)); + } + /** * @param string $composerType * diff --git a/Tests/Util/ConfigTest.php b/Tests/Config/ConfigTest.php similarity index 79% rename from Tests/Util/ConfigTest.php rename to Tests/Config/ConfigTest.php index fdd4b326..d65b269d 100644 --- a/Tests/Util/ConfigTest.php +++ b/Tests/Config/ConfigTest.php @@ -9,19 +9,25 @@ * file that was distributed with this source code. */ -namespace Fxp\Composer\AssetPlugin\Tests\Util; +namespace Fxp\Composer\AssetPlugin\Tests\Composer; +use Composer\Composer; use Composer\IO\IOInterface; use Composer\Package\RootPackageInterface; -use Fxp\Composer\AssetPlugin\Util\Config; +use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; /** - * Tests for config. + * Tests for the plugin config. * * @author François Pluchino */ class ConfigTest extends \PHPUnit_Framework_TestCase { + /** + * @var Composer|\PHPUnit_Framework_MockObject_MockObject + */ + protected $composer; + /** * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -34,8 +40,13 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->composer = $this->getMockBuilder(Composer::class)->disableOriginalConstructor()->getMock(); $this->io = $this->getMockBuilder(IOInterface::class)->getMock(); $this->package = $this->getMockBuilder(RootPackageInterface::class)->getMock(); + + $this->composer->expects($this->any()) + ->method('getPackage') + ->willReturn($this->package); } public function getDataForGetConfig() @@ -44,7 +55,7 @@ public function getDataForGetConfig() array('foo', 42, 42), array('bar', 'foo', 'empty'), array('baz', false, true), - array('old', 42, 0), + array('repositories', 42, 0), ); } @@ -61,7 +72,7 @@ public function testGetConfig($key, $expected, $default = null) ->method('getExtra') ->willReturn(array( 'asset-baz' => false, - 'asset-old' => 42, + 'asset-repositories' => 42, )); $this->package->expects($this->any()) @@ -73,7 +84,9 @@ public function testGetConfig($key, $expected, $default = null) ), )); - $this->assertSame($expected, Config::get($this->package, $key, $default)); + $config = ConfigBuilder::build($this->composer); + + $this->assertSame($expected, $config->get($key, $default)); } public function testValidateConfig() @@ -101,6 +114,6 @@ public function testValidateConfig() ->with('The "extra.'.$option.'" option is deprecated, use the "config.fxp-asset.'.substr($option, 6).'" option'); } - Config::validate($this->io, $this->package); + ConfigBuilder::validate($this->io, $this->package); } } diff --git a/Tests/FxpAssetPluginTest.php b/Tests/FxpAssetPluginTest.php index 5f1fc9c8..5290d56b 100644 --- a/Tests/FxpAssetPluginTest.php +++ b/Tests/FxpAssetPluginTest.php @@ -367,4 +367,12 @@ public function testAssetInstallers() $this->assertInstanceOf('Fxp\Composer\AssetPlugin\Installer\BowerInstaller', $im->getInstaller('bower-asset-library')); $this->assertInstanceOf('Fxp\Composer\AssetPlugin\Installer\AssetInstaller', $im->getInstaller('npm-asset-library')); } + + public function testGetConfig() + { + $this->plugin->activate($this->composer, $this->io); + + $config = $this->plugin->getConfig(); + $this->assertInstanceOf(\Fxp\Composer\AssetPlugin\Config\Config::class, $config); + } } diff --git a/Tests/Installer/AssetInstallerTest.php b/Tests/Installer/AssetInstallerTest.php index b8914158..eebeccb9 100644 --- a/Tests/Installer/AssetInstallerTest.php +++ b/Tests/Installer/AssetInstallerTest.php @@ -17,6 +17,7 @@ use Composer\Package\RootPackageInterface; use Composer\Repository\InstalledRepositoryInterface; use Composer\Util\Filesystem; +use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; use Fxp\Composer\AssetPlugin\Installer\AssetInstaller; use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface; @@ -171,7 +172,6 @@ public function testInstall() ->method('getDownloadManager') ->will($this->returnValue($dm)); - $library = new AssetInstaller($io, $this->composer, $type); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ $package = $this->createPackageMock('foo-asset/package'); @@ -187,6 +187,9 @@ public function testInstall() ->method('addPackage') ->with($package); + $config = ConfigBuilder::build($this->composer); + $library = new AssetInstaller($config, $io, $this->composer, $type); + /* @var InstalledRepositoryInterface $repository */ $library->install($repository, $package); $this->assertFileExists($vendorDir, 'Vendor dir should be created'); @@ -207,8 +210,9 @@ protected function createInstaller() $composer = $this->composer; /* @var AssetTypeInterface $type */ $type = $this->type; + $config = ConfigBuilder::build($composer); - return new AssetInstaller($io, $composer, $type); + return new AssetInstaller($config, $io, $composer, $type); } /** diff --git a/Tests/Installer/BowerInstallerTest.php b/Tests/Installer/BowerInstallerTest.php index 95a3baca..e6a4030c 100644 --- a/Tests/Installer/BowerInstallerTest.php +++ b/Tests/Installer/BowerInstallerTest.php @@ -21,6 +21,7 @@ use Composer\Repository\InstalledRepositoryInterface; use Composer\TestCase; use Composer\Util\Filesystem; +use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; use Fxp\Composer\AssetPlugin\Installer\BowerInstaller; use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface; use Fxp\Composer\AssetPlugin\Util\AssetPlugin; @@ -147,7 +148,7 @@ public function testInstallerCreationShouldNotCreateVendorDirectory() $this->fs->removeDirectory($this->vendorDir); $this->composer->setPackage($rootPackage); - new BowerInstaller($io, $this->composer, $type); + new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); $this->assertFileNotExists($this->vendorDir); } @@ -163,7 +164,7 @@ public function testInstallerCreationShouldNotCreateBinDirectory() $this->fs->removeDirectory($this->binDir); $this->composer->setPackage($rootPackage); - new BowerInstaller($io, $this->composer, $type); + new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); $this->assertFileNotExists($this->binDir); } @@ -178,7 +179,7 @@ public function testIsInstalled() $this->composer->setPackage($rootPackage); - $library = new BowerInstaller($io, $this->composer, $type); + $library = new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ $package = $this->createPackageMock(); $package @@ -246,7 +247,7 @@ public function testInstall(array $ignoreFiles) $this->composer->setPackage($rootPackage); - $library = new BowerInstaller($io, $this->composer, $type); + $library = new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ $package = $this->createPackageMock($ignoreFiles); $package @@ -296,7 +297,7 @@ public function testUpdate(array $ignoreFiles) $this->composer->setPackage($rootPackage); - $library = new BowerInstaller($io, $this->composer, $type); + $library = new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ $package = $this->createPackageMock($ignoreFiles); $package @@ -339,7 +340,7 @@ public function testUninstall() $this->composer->setPackage($rootPackage); - $library = new BowerInstaller($io, $this->composer, $type); + $library = new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); $package = $this->createPackageMock(); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ @@ -386,7 +387,7 @@ public function testGetInstallPath() $this->composer->setPackage($rootPackage); - $library = new BowerInstaller($io, $this->composer, $type); + $library = new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); $package = $this->createPackageMock(); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ @@ -423,7 +424,7 @@ public function testGetInstallPathWithTargetDir() $this->composer->setPackage($rootPackage); - $library = new BowerInstaller($io, $this->composer, $type); + $library = new BowerInstaller(ConfigBuilder::build($this->composer), $io, $this->composer, $type); $package = $this->createPackageMock(); /* @var \PHPUnit_Framework_MockObject_MockObject $package */ @@ -455,9 +456,10 @@ public function testMainFiles(array $mainFiles) /* @var RootPackageInterface $rootPackage */ $rootPackage = $this->createRootPackageMock($mainFiles); $this->composer->setPackage($rootPackage); + $config = ConfigBuilder::build($this->composer); $package = new Package('foo-asset/bar', '1.0.0', '1.0.0'); - $package = AssetPlugin::addMainFiles($this->composer, $package); + $package = AssetPlugin::addMainFiles($config, $package); $extra = $package->getExtra(); if (isset($mainFiles['fxp-asset']['main-files'])) { diff --git a/Tests/Installer/IgnoreFactoryTest.php b/Tests/Installer/IgnoreFactoryTest.php index 7c96bc79..0f7d2828 100644 --- a/Tests/Installer/IgnoreFactoryTest.php +++ b/Tests/Installer/IgnoreFactoryTest.php @@ -15,6 +15,7 @@ use Composer\Config; use Composer\Package\PackageInterface; use Composer\Package\RootPackageInterface; +use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; use Fxp\Composer\AssetPlugin\Installer\IgnoreFactory; use Fxp\Composer\AssetPlugin\Installer\IgnoreManager; @@ -90,7 +91,8 @@ public function tearDown() public function testCreateWithoutIgnoreFiles() { - $manager = IgnoreFactory::create($this->composer, $this->package); + $config = ConfigBuilder::build($this->composer); + $manager = IgnoreFactory::create($config, $this->composer, $this->package); $this->assertTrue($manager->isEnabled()); $this->assertFalse($manager->hasPattern()); @@ -114,7 +116,8 @@ public function testCreateWithIgnoreFiles() ->method('getConfig') ->will($this->returnValue($config)); - $manager = IgnoreFactory::create($this->composer, $this->package); + $config = ConfigBuilder::build($this->composer); + $manager = IgnoreFactory::create($config, $this->composer, $this->package); $this->assertTrue($manager->isEnabled()); $this->assertTrue($manager->hasPattern()); @@ -124,7 +127,8 @@ public function testCreateWithIgnoreFiles() public function testCreateWithCustomInstallDir() { $installDir = 'web/assets/'; - $manager = IgnoreFactory::create($this->composer, $this->package, $installDir); + $config = ConfigBuilder::build($this->composer); + $manager = IgnoreFactory::create($config, $this->composer, $this->package, $installDir); $this->assertTrue($manager->isEnabled()); $this->assertFalse($manager->hasPattern()); @@ -146,7 +150,8 @@ public function testCreateWithEnablingOfIgnoreFiles() ->method('getConfig') ->will($this->returnValue($config)); - $manager = IgnoreFactory::create($this->composer, $this->package); + $config = ConfigBuilder::build($this->composer); + $manager = IgnoreFactory::create($config, $this->composer, $this->package); $this->assertTrue($manager->isEnabled()); $this->assertFalse($manager->hasPattern()); @@ -168,7 +173,8 @@ public function testCreateWithDisablingOfIgnoreFiles() ->method('getConfig') ->will($this->returnValue($config)); - $manager = IgnoreFactory::create($this->composer, $this->package); + $config = ConfigBuilder::build($this->composer); + $manager = IgnoreFactory::create($config, $this->composer, $this->package); $this->assertFalse($manager->isEnabled()); $this->assertFalse($manager->hasPattern()); @@ -192,7 +198,8 @@ public function testCreateWithCustomIgnoreSection() ->method('getConfig') ->will($this->returnValue($config)); - $manager = IgnoreFactory::create($this->composer, $this->package, null, 'custom-ignore-files'); + $config = ConfigBuilder::build($this->composer); + $manager = IgnoreFactory::create($config, $this->composer, $this->package, null, 'custom-ignore-files'); $this->assertTrue($manager->isEnabled()); $this->assertTrue($manager->hasPattern()); diff --git a/Tests/Repository/VcsPackageFilterTest.php b/Tests/Repository/VcsPackageFilterTest.php index 2f71e610..2bf9b77a 100644 --- a/Tests/Repository/VcsPackageFilterTest.php +++ b/Tests/Repository/VcsPackageFilterTest.php @@ -11,11 +11,13 @@ namespace Fxp\Composer\AssetPlugin\Tests\Repository; +use Composer\Composer; use Composer\Installer\InstallationManager; use Composer\Package\Loader\ArrayLoader; use Composer\Package\Package; use Composer\Package\RootPackageInterface; use Composer\Repository\InstalledFilesystemRepository; +use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; use Fxp\Composer\AssetPlugin\Package\Version\VersionParser; use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter; use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface; @@ -28,7 +30,12 @@ class VcsPackageFilterTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var Composer|\PHPUnit_Framework_MockObject_MockObject + */ + protected $composer; + + /** + * @var RootPackageInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $package; @@ -54,6 +61,7 @@ class VcsPackageFilterTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->composer = $this->getMockBuilder('Composer\Composer')->disableOriginalConstructor()->getMock(); $this->package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); $this->assetType = $this->getMockBuilder('Fxp\Composer\AssetPlugin\Type\AssetTypeInterface')->getMock(); @@ -74,6 +82,10 @@ protected function setUp() $this->installationManager->expects($this->any()) ->method('isPackageInstalled') ->will($this->returnValue(true)); + + $this->composer->expects($this->any()) + ->method('getPackage') + ->willReturn($this->package); } protected function tearDown() @@ -623,7 +635,9 @@ protected function init(array $requires = array(), $minimumStability = 'stable', /* @var RootPackageInterface $package */ $package = $this->package; - $this->filter = new VcsPackageFilter($package, $this->installationManager, $this->installedRepository); + $config = ConfigBuilder::build($this->composer); + + $this->filter = new VcsPackageFilter($config, $package, $this->installationManager, $this->installedRepository); } /** diff --git a/Util/AssetPlugin.php b/Util/AssetPlugin.php index 67dc5428..5288e016 100644 --- a/Util/AssetPlugin.php +++ b/Util/AssetPlugin.php @@ -15,9 +15,9 @@ use Composer\IO\IOInterface; use Composer\Package\Package; use Composer\Package\PackageInterface; -use Composer\Package\RootPackageInterface; use Composer\Repository\RepositoryManager; use Fxp\Composer\AssetPlugin\Assets; +use Fxp\Composer\AssetPlugin\Config\Config; use Fxp\Composer\AssetPlugin\Installer\AssetInstaller; use Fxp\Composer\AssetPlugin\Installer\BowerInstaller; use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager; @@ -33,15 +33,16 @@ class AssetPlugin /** * Adds asset installers. * + * @param Config $config * @param Composer $composer * @param IOInterface $io */ - public static function addInstallers(Composer $composer, IOInterface $io) + public static function addInstallers(Config $config, Composer $composer, IOInterface $io) { $im = $composer->getInstallationManager(); - $im->addInstaller(new BowerInstaller($io, $composer, Assets::createType('bower'))); - $im->addInstaller(new AssetInstaller($io, $composer, Assets::createType('npm'))); + $im->addInstaller(new BowerInstaller($config, $io, $composer, Assets::createType('bower'))); + $im->addInstaller(new AssetInstaller($config, $io, $composer, Assets::createType('npm'))); } /** @@ -71,18 +72,18 @@ public static function createAssetOptions(array $config, $assetType) * * @param AssetRepositoryManager $arm The asset repository manager * @param VcsPackageFilter $filter The vcs package filter - * @param RootPackageInterface $package The root package + * @param Config $config The plugin config * @param string $assetType The asset type * * @return array */ - public static function createRepositoryConfig(AssetRepositoryManager $arm, VcsPackageFilter $filter, RootPackageInterface $package, $assetType) + public static function createRepositoryConfig(AssetRepositoryManager $arm, VcsPackageFilter $filter, Config $config, $assetType) { return array( 'asset-repository-manager' => $arm, 'vcs-package-filter' => $filter, - 'asset-options' => static::createAssetOptions(Config::getArray($package, 'registry-options'), $assetType), - 'vcs-driver-options' => Config::getArray($package, 'vcs-driver-options'), + 'asset-options' => static::createAssetOptions($config->getArray('registry-options'), $assetType), + 'vcs-driver-options' => $config->getArray('vcs-driver-options'), ); } @@ -91,15 +92,15 @@ public static function createRepositoryConfig(AssetRepositoryManager $arm, VcsPa * * @param AssetRepositoryManager $arm * @param VcsPackageFilter $filter - * @param RootPackageInterface $package + * @param Config $config */ - public static function addRegistryRepositories(AssetRepositoryManager $arm, VcsPackageFilter $filter, RootPackageInterface $package) + public static function addRegistryRepositories(AssetRepositoryManager $arm, VcsPackageFilter $filter, Config $config) { foreach (Assets::getRegistryFactories() as $registryType => $factoryClass) { $ref = new \ReflectionClass($factoryClass); if ($ref->implementsInterface('Fxp\Composer\AssetPlugin\Repository\RegistryFactoryInterface')) { - call_user_func(array($factoryClass, 'create'), $arm, $filter, $package); + call_user_func(array($factoryClass, 'create'), $arm, $filter, $config); } } } @@ -121,17 +122,17 @@ public static function setVcsTypeRepositories(RepositoryManager $rm) /** * Adds the main file definitions from the root package. * - * @param Composer $composer + * @param Config $config * @param PackageInterface $package * @param string $section * * @return PackageInterface */ - public static function addMainFiles(Composer $composer, PackageInterface $package, $section = 'main-files') + public static function addMainFiles(Config $config, PackageInterface $package, $section = 'main-files') { if ($package instanceof Package) { $packageExtra = $package->getExtra(); - $rootMainFiles = Config::getArray($composer->getPackage(), $section); + $rootMainFiles = $config->getArray($section); foreach ($rootMainFiles as $packageName => $files) { if ($packageName === $package->getName()) {