Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/47'
Browse files Browse the repository at this point in the history
Close #47
Fixes #39
  • Loading branch information
weierophinney committed Jan 11, 2018
2 parents ba635ff + 81651e4 commit 238dbb0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 4 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.1.1 - TBD
## 1.1.1 - 2018-01-11

### Added

Expand All @@ -22,7 +22,11 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#47](https://github.com/zendframework/zend-component-installer/pull/47) fixes
an issue during package removal when a package defines multiple targets (e.g.,
both "component" and "config-provider") and a `ConfigInjectorChain` is thus
used by the plugin; previously, an error was raised due to an attempt to call
a method the `ConfigInjectorChain` does not define.

## 1.1.0 - 2017-11-06

Expand Down
46 changes: 44 additions & 2 deletions src/ComponentInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use DirectoryIterator;
use Zend\ComponentInstaller\Injector\AbstractInjector;
use Zend\ComponentInstaller\Injector\ConfigInjectorChain;
use Zend\ComponentInstaller\Injector\InjectorInterface;
use Zend\ComponentInstaller\Injector\NoopInjector;

/**
* If a package represents a component module, update the application configuration.
Expand Down Expand Up @@ -535,18 +539,56 @@ private function removePackageFromConfig($package, array $metadata, Collection $
*/
private function removeModuleFromConfig($module, $package, Collection $injectors)
{
$injectors->each(function ($injector) use ($module, $package) {
$injectors->each(function (InjectorInterface $injector) use ($module, $package) {
$this->io->write(sprintf('<info>Removing %s from package %s</info>', $module, $package));

if ($injector->remove($module)) {
$this->io->write(sprintf(
'<info> Removed package from %s</info>',
$injector->getConfigFile()
$this->getInjectorConfigFileName($injector)
));
}
});
}

/**
* @param InjectorInterface $injector
* @return string
* @todo remove after InjectorInterface has getConfigName defined
*/
private function getInjectorConfigFileName(InjectorInterface $injector)
{
if ($injector instanceof ConfigInjectorChain) {
return $this->getInjectorChainConfigFileName($injector);
} elseif ($injector instanceof AbstractInjector) {
return $this->getAbstractInjectorConfigFileName($injector);
}

return '';
}

/**
* @param ConfigInjectorChain $injector
* @return string
* @todo remove after InjectorInterface has getConfigName defined
*/
private function getInjectorChainConfigFileName(ConfigInjectorChain $injector)
{
return implode(', ', array_map(function ($item) {
return $this->getInjectorConfigFileName($item);
}, $injector->getCollection()->toArray()));
}

/**
* @param AbstractInjector $injector
* @return string
* @todo remove after InjectorInterface has getConfigName defined
*/
private function getAbstractInjectorConfigFileName(AbstractInjector $injector)
{
return $injector->getConfigFile();
}

/**
* Is a given module name valid?
*
Expand Down
3 changes: 3 additions & 0 deletions src/Injector/InjectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use Zend\ComponentInstaller\Exception;

/**
* @todo add getConfigFile() method in 2.0
*/
interface InjectorInterface
{
const TYPE_CONFIG_PROVIDER = 0;
Expand Down
89 changes: 89 additions & 0 deletions test/ComponentInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1629,4 +1629,93 @@ public function getModuleDependencies()
CONTENT
);
}

/**
* @dataProvider injectorConfigProvider
*
* @param string $configContents
* @param array $configNames
* @param string $expectedName
*/
public function testUninstallMessageWithDifferentInjectors($configContents, array $configNames, $expectedName)
{
foreach ($configNames as $configName) {
$this->createConfigFile($configName, $configContents);
}

$package = $this->prophesize(PackageInterface::class);
$package->getName()->willReturn('some/component');
$package->getExtra()->willReturn([
'zf' => [
'component' => [
'Some\\Component',
],
]
]);
$package->getAutoload()->willReturn([]);

$operation = $this->prophesize(InstallOperation::class);
$operation->getPackage()->willReturn($package->reveal());

$event = $this->prophesize(PackageEvent::class);
$event->isDevMode()->willReturn(true);
$event->getOperation()->willReturn($operation->reveal());

$this->io
->write('<info>Removing Some\Component from package some/component</info>')
->shouldBeCalled();

// assertion
$this->io
->write(Argument::that(function ($argument) use ($expectedName) {
return (bool)preg_match(
sprintf('#Removed package from %s#', $expectedName),
$argument
);
}))
->shouldBeCalled();

$this->installer->onPostPackageUninstall($event->reveal());
}

/**
* @return array
*/
public function injectorConfigProvider()
{
$config = <<<'CONFIG'
<?php
return [
'modules' => [
'Some\Component'
]
];
CONFIG;

return [
'application.config.php' => [
$config,
['application.config.php'],
'.*?config/application.config.php'
],
'development.config.php' => [
$config,
['development.config.php.dist', 'development.config.php'],
'.*?config/development.config.php.dist.*?config/development.config.php'
],
];
}

/**
* Create config on demand
*
* @param string $name
* @param string $contents
*/
private function createConfigFile($name, $contents)
{
vfsStream::newFile('config/' . $name)
->at($this->projectRoot)
->setContent($contents);
}
}

0 comments on commit 238dbb0

Please sign in to comment.