Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "EUPL-1.2",
"require": {
"composer-plugin-api": "^2.0",
"cweagans/composer-patches": "^2.0@beta"
"cweagans/composer-patches": "^2.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function getPatchesLockFilePath(): string
*/
public function resolvePatches(): PatchCollection
{
$resolver = new Resolver($this->composer, $this->io, []);
$resolver = new Resolver($this->composer, $this->io, [], $this);
return $resolver->loadFromResolvers();
}

Expand Down
38 changes: 19 additions & 19 deletions src/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@

namespace OpenEuropa\ComposerDependentPatches;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use cweagans\Composer\Resolver as ComposerPatchesResolver;
use OpenEuropa\ComposerDependentPatches\Resolver\Dependencies;
use OpenEuropa\ComposerDependentPatches\Resolver\RootComposer;

class Resolver extends ComposerPatchesResolver
{
/**
* {@inheritdoc}
* @var PluginInterface
*/
protected function getPatchResolvers(): array
protected PluginInterface $plugin;

/**
* Constructor.
*/
public function __construct(Composer $composer, IOInterface $io, array $disabledResolvers, PluginInterface $plugin)
{
// Make sure that this plugin only uses the resolvers returned here.
return [
new RootComposer($this->composer, $this->io, $this->getPluginInstance()),
new Dependencies($this->composer, $this->io, $this->getPluginInstance()),
];
parent::__construct($composer, $io, $disabledResolvers);
$this->plugin = $plugin;
}

/**
* Get instance of this plugin.
*
* @return Plugin
* @throws \RuntimeException If plugin instance cannot be found.
* {@inheritdoc}
*/
protected function getPluginInstance(): Plugin
protected function getPatchResolvers(): array
{
foreach ($this->composer->getPluginManager()->getPlugins() as $plugin) {
if ($plugin instanceof Plugin) {
return $plugin;
}
}

throw new \RuntimeException('Plugin instance not found. Make sure the plugin is properly activated.');
// Make sure that this plugin only uses the resolvers returned here.
return [
new RootComposer($this->composer, $this->io, $this->plugin),
new Dependencies($this->composer, $this->io, $this->plugin),
];
}
}
71 changes: 11 additions & 60 deletions tests/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PluginManager;
use cweagans\Composer\Resolver as ComposerPatchesResolver;
use Mockery;
use OpenEuropa\ComposerDependentPatches\Plugin;
Expand All @@ -25,6 +24,7 @@ class ResolverTest extends TestCase
private Resolver $resolver;
private Composer $composer;
private IOInterface $io;
private Plugin $plugin;

/**
* Set up test fixtures.
Expand All @@ -33,8 +33,9 @@ protected function setUp(): void
{
$this->composer = Mockery::mock(Composer::class);
$this->io = Mockery::mock(IOInterface::class);
$this->plugin = Mockery::mock(Plugin::class);

$this->resolver = new Resolver($this->composer, $this->io, []);
$this->resolver = new Resolver($this->composer, $this->io, [], $this->plugin);
}

/**
Expand All @@ -50,17 +51,6 @@ protected function tearDown(): void
*/
public function testGetPatchResolversReturnsCorrectResolvers(): void
{
$mockPluginManager = Mockery::mock(PluginManager::class);
$mockPlugin = Mockery::mock(Plugin::class);

$this->composer->shouldReceive('getPluginManager')
->twice() // Called twice: once for RootComposer, once for Dependencies.
->andReturn($mockPluginManager);

$mockPluginManager->shouldReceive('getPlugins')
->twice()
->andReturn([$mockPlugin]);

// Use reflection to access protected method.
$reflection = new ReflectionClass($this->resolver);
$method = $reflection->getMethod('getPatchResolvers');
Expand All @@ -75,58 +65,19 @@ public function testGetPatchResolversReturnsCorrectResolvers(): void
}

/**
* Test that getPluginInstance returns the correct plugin instance.
* Test that the plugin instance is properly stored.
*/
public function testGetPluginInstanceReturnsPlugin(): void
public function testPluginInstanceIsStored(): void
{
$mockPluginManager = Mockery::mock(PluginManager::class);
$mockPlugin = Mockery::mock(Plugin::class);
$mockOtherPlugin = Mockery::mock(PluginInterface::class);

$this->composer->shouldReceive('getPluginManager')
->once()
->andReturn($mockPluginManager);

$mockPluginManager->shouldReceive('getPlugins')
->once()
->andReturn([$mockOtherPlugin, $mockPlugin]);

// Use reflection to access protected method.
// Use reflection to access protected property.
$reflection = new ReflectionClass($this->resolver);
$method = $reflection->getMethod('getPluginInstance');
$method->setAccessible(true);

$result = $method->invoke($this->resolver);

$this->assertInstanceOf(Plugin::class, $result);
$this->assertSame($mockPlugin, $result);
}

/**
* Test that getPluginInstance throws exception when no plugin is found.
*/
public function testGetPluginInstanceThrowsExceptionWhenNoPluginFound(): void
{
$mockPluginManager = Mockery::mock(PluginManager::class);
$mockOtherPlugin = Mockery::mock(PluginInterface::class);

$this->composer->shouldReceive('getPluginManager')
->once()
->andReturn($mockPluginManager);

$mockPluginManager->shouldReceive('getPlugins')
->once()
->andReturn([$mockOtherPlugin]);

// Use reflection to access protected method.
$reflection = new ReflectionClass($this->resolver);
$method = $reflection->getMethod('getPluginInstance');
$method->setAccessible(true);
$property = $reflection->getProperty('plugin');
$property->setAccessible(true);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Plugin instance not found. Make sure the plugin is properly activated.');
$result = $property->getValue($this->resolver);

$method->invoke($this->resolver);
$this->assertInstanceOf(PluginInterface::class, $result);
$this->assertSame($this->plugin, $result);
}

/**
Expand Down