Skip to content

Commit

Permalink
Check composer minimum version (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed Apr 19, 2016
1 parent 419b5f4 commit f7351b6
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -18,9 +18,12 @@ matrix:
include:
- php: 5.3
env: COMPOSER_FLAGS="--prefer-lowest"
# Special case for https://github.com/Soullivaneuh/composer-versions-check/issues/8
- php: 5.6
env: COMPOSER_VERSION=dev-master
# Incompatibility message test check. Must be kept
- php: 5.6
env: COMPOSER_VERSION=1.0.0-beta1
# Special case for https://github.com/Soullivaneuh/composer-versions-check/issues/8
- php: 5.6
env: WITHOUT_SEMVER=1
allow_failures:
Expand Down
34 changes: 34 additions & 0 deletions src/VersionsCheckPlugin.php
Expand Up @@ -12,12 +12,15 @@
use Composer\Repository\RepositoryManager;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\Semver\Semver;

/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*/
final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInterface
{
const COMPOSER_MIN_VERSION = '1.0.0-stable';

/**
* @var Composer
*/
Expand Down Expand Up @@ -48,6 +51,19 @@ final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInter
*/
public function activate(Composer $composer, IOInterface $io)
{
if (!static::satisfiesComposerVersion()) {
$io->writeError(sprintf(
'<error>Composer v%s is not supported by sllh/composer-versions-check plugin,'
.' please upgrade to v%s or higher.</error>',
Composer::VERSION,
self::COMPOSER_MIN_VERSION
));
}
if ('@package_version@' === Composer::VERSION) {
$io->write('<warning>You are running an unstable version of composer.'
.' The sllh/composer-versions-check plugin might not works as expected.</warning>');
}

$this->composer = $composer;
$this->io = $io;
$this->versionsCheck = new VersionsCheck();
Expand All @@ -59,6 +75,11 @@ public function activate(Composer $composer, IOInterface $io)
*/
public static function getSubscribedEvents()
{
// Do not subscribe the plugin if not compatible.
if (!static::satisfiesComposerVersion()) {
return array();
}

return array(
PluginEvents::COMMAND => array(
array('command'),
Expand All @@ -69,6 +90,19 @@ public static function getSubscribedEvents()
);
}

/**
* @return bool
*/
public static function satisfiesComposerVersion()
{
// Can't determine version. Assuming it satisfies.
if ('@package_version@' === Composer::VERSION) {
return true;
}

return Semver::satisfies(Composer::VERSION, sprintf('>=%s', static::COMPOSER_MIN_VERSION));
}

/**
* @param CommandEvent $event
*/
Expand Down
95 changes: 95 additions & 0 deletions tests/VersionsCheckPluginComposerVersionTest.php
@@ -0,0 +1,95 @@
<?php

namespace SLLH\ComposerVersionsCheck\Tests;

use Composer\Command\UpdateCommand;
use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventDispatcher;
use Composer\IO\BufferIO;
use Composer\Package\RootPackage;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PluginManager;
use Composer\Repository\ArrayRepository;
use Composer\Repository\RepositoryManager;
use Composer\Repository\WritableArrayRepository;
use Composer\Script\ScriptEvents;
use SLLH\ComposerVersionsCheck\VersionsCheckPlugin;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
* The only goal of this class is to test composer version check.
*
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*/
class VersionsCheckPluginComposerVersionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var BufferIO
*/
private $io;

/**
* @var Composer|\PHPUnit_Framework_MockObject_MockObject
*/
private $composer;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->io = new BufferIO();
$this->composer = $this->getMock('Composer\Composer');

$this->composer->expects($this->any())->method('getConfig')
->willReturn(new Config(false));
$this->composer->expects($this->any())->method('getPackage')
->willReturn(new RootPackage('my/project', '1.0.0', '1.0.0'));
$this->composer->expects($this->any())->method('getPluginManager')
->willReturn(new PluginManager($this->io, $this->composer));
$this->composer->expects($this->any())->method('getEventDispatcher')
->willReturn(new EventDispatcher($this->composer, $this->io));
$this->composer->expects($this->any())->method('getRepositoryManager')
->willReturn(new RepositoryManager($this->io, new Config()));
}

public function testComposerVersionMessage()
{
$this->addComposerPlugin(new VersionsCheckPlugin());

$this->composer->getRepositoryManager()->setLocalRepository(new WritableArrayRepository());
$this->composer->getRepositoryManager()->addRepository(new ArrayRepository());

$updateCommand = new UpdateCommand();
$input = new ArrayInput(array('update'), $updateCommand->getDefinition());
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, new NullOutput());
$this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);

if (VersionsCheckPlugin::satisfiesComposerVersion() && '@package_version@' === Composer::VERSION) {
$this->assertSame(
"<warning>You are running an unstable version of composer. The sllh/composer-versions-check plugin might not works as expected.</warning>\n"
."All packages are up to date.\n",
$this->io->getOutput());
} elseif (VersionsCheckPlugin::satisfiesComposerVersion()) {
$this->assertSame("All packages are up to date.\n", $this->io->getOutput());
} else {
$this->assertSame(
'Composer v'.Composer::VERSION.' is not supported by sllh/composer-versions-check plugin, please upgrade to v'.VersionsCheckPlugin::COMPOSER_MIN_VERSION." or higher.\n",
$this->io->getOutput()
);
}
}

private function addComposerPlugin(PluginInterface $plugin)
{
$pluginManagerReflection = new \ReflectionClass($this->composer->getPluginManager());
$addPluginReflection = $pluginManagerReflection->getMethod('addPlugin');
$addPluginReflection->setAccessible(true);
$addPluginReflection->invoke($this->composer->getPluginManager(), $plugin);
}
}
25 changes: 20 additions & 5 deletions tests/VersionsCheckPluginTest.php
Expand Up @@ -46,6 +46,10 @@ class VersionsCheckPluginTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
if (!VersionsCheckPlugin::satisfiesComposerVersion()) {
$this->markTestSkipped('Composer version not compatible.');
}

$this->io = new BufferIO();
$this->composer = $this->getMock('Composer\Composer');
$this->config = new Config(false);
Expand Down Expand Up @@ -182,14 +186,14 @@ public function testUpdateCommand()

$this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);

$this->assertSame(<<<'EOF'
$this->assertSameOutput(<<<'EOF'
<warning>1 package is not up to date:</warning>
- foo/bar (1.0.0) latest is 2.0.0
EOF
, $this->io->getOutput());
);
}

public function testPreferLowest()
Expand All @@ -212,7 +216,7 @@ public function testPreferLowest()
$this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);

$this->assertSame('', $this->io->getOutput(), 'Plugin should not be runned.');
$this->assertSameOutput('', 'Plugin should not be runned.');
}

public function testPreferLowestNotExists()
Expand All @@ -232,14 +236,14 @@ public function testPreferLowestNotExists()
$this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);

$this->assertSame(<<<'EOF'
$this->assertSameOutput(<<<'EOF'
<warning>1 package is not up to date:</warning>
- foo/bar (1.0.0) latest is 2.0.0
EOF
, $this->io->getOutput());
);
}

private function addComposerPlugin(PluginInterface $plugin)
Expand All @@ -249,4 +253,15 @@ private function addComposerPlugin(PluginInterface $plugin)
$addPluginReflection->setAccessible(true);
$addPluginReflection->invoke($this->composer->getPluginManager(), $plugin);
}

private function assertSameOutput($expectedOutput, $message = '')
{
if ('@package_version@' === Composer::VERSION) {
$expectedOutput = '<warning>You are running an unstable version of composer.'
." The sllh/composer-versions-check plugin might not works as expected.</warning>\n"
.$expectedOutput;
}

$this->assertSame($expectedOutput, $this->io->getOutput(), $message);
}
}

0 comments on commit f7351b6

Please sign in to comment.