2012-04-27 09:42:58 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test;
|
|
|
|
|
|
|
|
use Composer\Installer;
|
2012-05-27 22:11:18 +00:00
|
|
|
use Composer\Console\Application;
|
2012-05-13 19:40:42 +00:00
|
|
|
use Composer\Config;
|
|
|
|
use Composer\Json\JsonFile;
|
2012-04-27 09:42:58 +00:00
|
|
|
use Composer\Repository\ArrayRepository;
|
|
|
|
use Composer\Repository\RepositoryManager;
|
2012-08-23 13:52:40 +00:00
|
|
|
use Composer\Package\RootPackageInterface;
|
2012-04-27 09:42:58 +00:00
|
|
|
use Composer\Package\Link;
|
2012-05-13 19:40:42 +00:00
|
|
|
use Composer\Package\Locker;
|
|
|
|
use Composer\Test\Mock\FactoryMock;
|
|
|
|
use Composer\Test\Mock\InstalledFilesystemRepositoryMock;
|
2012-04-27 09:42:58 +00:00
|
|
|
use Composer\Test\Mock\InstallationManagerMock;
|
2012-05-13 19:40:42 +00:00
|
|
|
use Composer\Test\Mock\WritableRepositoryMock;
|
2012-05-27 22:11:18 +00:00
|
|
|
use Symfony\Component\Console\Input\StringInput;
|
|
|
|
use Symfony\Component\Console\Output\StreamOutput;
|
2012-04-27 09:42:58 +00:00
|
|
|
|
|
|
|
class InstallerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider provideInstaller
|
|
|
|
*/
|
2012-08-23 13:52:40 +00:00
|
|
|
public function testInstaller(RootPackageInterface $rootPackage, $repositories, array $options)
|
2012-04-27 09:42:58 +00:00
|
|
|
{
|
|
|
|
$io = $this->getMock('Composer\IO\IOInterface');
|
|
|
|
|
|
|
|
$downloadManager = $this->getMock('Composer\Downloader\DownloadManager');
|
|
|
|
$config = $this->getMock('Composer\Config');
|
|
|
|
|
|
|
|
$repositoryManager = new RepositoryManager($io, $config);
|
|
|
|
$repositoryManager->setLocalRepository(new WritableRepositoryMock());
|
|
|
|
$repositoryManager->setLocalDevRepository(new WritableRepositoryMock());
|
2012-04-29 15:29:06 +00:00
|
|
|
|
|
|
|
if (!is_array($repositories)) {
|
|
|
|
$repositories = array($repositories);
|
|
|
|
}
|
|
|
|
foreach ($repositories as $repository) {
|
|
|
|
$repositoryManager->addRepository($repository);
|
|
|
|
}
|
2012-04-27 09:42:58 +00:00
|
|
|
|
|
|
|
$locker = $this->getMockBuilder('Composer\Package\Locker')->disableOriginalConstructor()->getMock();
|
|
|
|
$installationManager = new InstallationManagerMock();
|
2013-01-06 19:34:52 +00:00
|
|
|
|
2012-04-27 09:42:58 +00:00
|
|
|
$eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock();
|
2013-01-06 19:34:52 +00:00
|
|
|
$autoloadGenerator = $this->getMockBuilder('Composer\Autoload\AutoloadGenerator')->disableOriginalConstructor()->getMock();
|
2012-04-27 09:42:58 +00:00
|
|
|
|
2012-06-24 19:58:51 +00:00
|
|
|
$installer = new Installer($io, $config, clone $rootPackage, $downloadManager, $repositoryManager, $locker, $installationManager, $eventDispatcher, $autoloadGenerator);
|
2012-04-27 09:42:58 +00:00
|
|
|
$result = $installer->run();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2012-04-29 15:29:06 +00:00
|
|
|
$expectedInstalled = isset($options['install']) ? $options['install'] : array();
|
|
|
|
$expectedUpdated = isset($options['update']) ? $options['update'] : array();
|
|
|
|
$expectedUninstalled = isset($options['uninstall']) ? $options['uninstall'] : array();
|
|
|
|
|
2012-04-27 09:42:58 +00:00
|
|
|
$installed = $installationManager->getInstalledPackages();
|
2012-04-29 15:29:06 +00:00
|
|
|
$this->assertSame($expectedInstalled, $installed);
|
2012-04-27 09:42:58 +00:00
|
|
|
|
|
|
|
$updated = $installationManager->getUpdatedPackages();
|
2012-04-29 15:29:06 +00:00
|
|
|
$this->assertSame($expectedUpdated, $updated);
|
2012-04-27 09:42:58 +00:00
|
|
|
|
|
|
|
$uninstalled = $installationManager->getUninstalledPackages();
|
2012-04-29 15:29:06 +00:00
|
|
|
$this->assertSame($expectedUninstalled, $uninstalled);
|
2012-04-27 09:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInstaller()
|
|
|
|
{
|
|
|
|
$cases = array();
|
|
|
|
|
|
|
|
// when A requires B and B requires A, and A is a non-published root package
|
|
|
|
// the install of B should succeed
|
|
|
|
|
2012-08-23 13:52:40 +00:00
|
|
|
$a = $this->getPackage('A', '1.0.0', 'Composer\Package\RootPackage');
|
2012-04-27 09:42:58 +00:00
|
|
|
$a->setRequires(array(
|
|
|
|
new Link('A', 'B', $this->getVersionConstraint('=', '1.0.0')),
|
|
|
|
));
|
|
|
|
$b = $this->getPackage('B', '1.0.0');
|
|
|
|
$b->setRequires(array(
|
|
|
|
new Link('B', 'A', $this->getVersionConstraint('=', '1.0.0')),
|
|
|
|
));
|
|
|
|
|
|
|
|
$cases[] = array(
|
|
|
|
$a,
|
|
|
|
new ArrayRepository(array($b)),
|
2012-04-29 15:29:06 +00:00
|
|
|
array(
|
|
|
|
'install' => array($b)
|
|
|
|
),
|
2012-04-27 09:42:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// #480: when A requires B and B requires A, and A is a published root package
|
|
|
|
// only B should be installed, as A is the root
|
|
|
|
|
2012-08-23 13:52:40 +00:00
|
|
|
$a = $this->getPackage('A', '1.0.0', 'Composer\Package\RootPackage');
|
2012-04-27 09:42:58 +00:00
|
|
|
$a->setRequires(array(
|
|
|
|
new Link('A', 'B', $this->getVersionConstraint('=', '1.0.0')),
|
|
|
|
));
|
|
|
|
$b = $this->getPackage('B', '1.0.0');
|
|
|
|
$b->setRequires(array(
|
|
|
|
new Link('B', 'A', $this->getVersionConstraint('=', '1.0.0')),
|
|
|
|
));
|
|
|
|
|
|
|
|
$cases[] = array(
|
|
|
|
$a,
|
|
|
|
new ArrayRepository(array($a, $b)),
|
2012-04-29 15:29:06 +00:00
|
|
|
array(
|
|
|
|
'install' => array($b)
|
|
|
|
),
|
2012-04-27 09:42:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $cases;
|
|
|
|
}
|
2012-05-13 19:40:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getIntegrationTests
|
|
|
|
*/
|
2012-09-05 18:39:45 +00:00
|
|
|
public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $installedDev, $run, $expectLock, $expectOutput, $expect)
|
2012-05-13 19:40:42 +00:00
|
|
|
{
|
|
|
|
if ($condition) {
|
|
|
|
eval('$res = '.$condition.';');
|
|
|
|
if (!$res) {
|
|
|
|
$this->markTestSkipped($condition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 09:07:04 +00:00
|
|
|
$output = null;
|
2012-05-13 19:40:42 +00:00
|
|
|
$io = $this->getMock('Composer\IO\IOInterface');
|
2012-05-23 09:07:04 +00:00
|
|
|
$io->expects($this->any())
|
|
|
|
->method('write')
|
|
|
|
->will($this->returnCallback(function ($text, $newline) use (&$output) {
|
|
|
|
$output .= $text . ($newline ? "\n":"");
|
|
|
|
}));
|
2012-05-13 19:40:42 +00:00
|
|
|
|
2012-05-28 16:57:59 +00:00
|
|
|
$composer = FactoryMock::create($io, $composerConfig);
|
2012-05-13 19:40:42 +00:00
|
|
|
|
|
|
|
$jsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
|
|
|
|
$jsonMock->expects($this->any())
|
|
|
|
->method('read')
|
|
|
|
->will($this->returnValue($installed));
|
|
|
|
$jsonMock->expects($this->any())
|
|
|
|
->method('exists')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$devJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
|
|
|
|
$devJsonMock->expects($this->any())
|
|
|
|
->method('read')
|
|
|
|
->will($this->returnValue($installedDev));
|
|
|
|
$devJsonMock->expects($this->any())
|
|
|
|
->method('exists')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$repositoryManager = $composer->getRepositoryManager();
|
|
|
|
$repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock));
|
|
|
|
$repositoryManager->setLocalDevRepository(new InstalledFilesystemRepositoryMock($devJsonMock));
|
|
|
|
|
|
|
|
$lockJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
|
|
|
|
$lockJsonMock->expects($this->any())
|
|
|
|
->method('read')
|
|
|
|
->will($this->returnValue($lock));
|
2012-05-28 16:57:59 +00:00
|
|
|
$lockJsonMock->expects($this->any())
|
|
|
|
->method('exists')
|
|
|
|
->will($this->returnValue(true));
|
2012-05-13 19:40:42 +00:00
|
|
|
|
2012-07-01 16:27:16 +00:00
|
|
|
if ($expectLock) {
|
|
|
|
$actualLock = array();
|
|
|
|
$lockJsonMock->expects($this->atLeastOnce())
|
|
|
|
->method('write')
|
|
|
|
->will($this->returnCallback(function ($hash, $options) use (&$actualLock) {
|
|
|
|
// need to do assertion outside of mock for nice phpunit output
|
|
|
|
// so store value temporarily in reference for later assetion
|
|
|
|
$actualLock = $hash;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2012-06-20 10:05:18 +00:00
|
|
|
$locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
|
2012-05-13 19:40:42 +00:00
|
|
|
$composer->setLocker($locker);
|
|
|
|
|
2013-01-06 19:34:52 +00:00
|
|
|
$eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock();
|
2013-02-24 17:21:16 +00:00
|
|
|
$autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator', array(), array($eventDispatcher));
|
|
|
|
$composer->setAutoloadGenerator($autoloadGenerator);
|
|
|
|
$composer->setEventDispatcher($eventDispatcher);
|
2012-05-13 19:40:42 +00:00
|
|
|
|
|
|
|
$installer = Installer::create(
|
|
|
|
$io,
|
2013-02-24 17:21:16 +00:00
|
|
|
$composer
|
2012-05-13 19:40:42 +00:00
|
|
|
);
|
|
|
|
|
2012-05-27 22:11:18 +00:00
|
|
|
$application = new Application;
|
|
|
|
$application->get('install')->setCode(function ($input, $output) use ($installer) {
|
|
|
|
$installer->setDevMode($input->getOption('dev'));
|
2012-05-13 19:40:42 +00:00
|
|
|
|
2012-08-05 05:55:54 +00:00
|
|
|
return $installer->run() ? 0 : 1;
|
2012-05-27 22:11:18 +00:00
|
|
|
});
|
2012-05-13 19:40:42 +00:00
|
|
|
|
2012-05-27 22:11:18 +00:00
|
|
|
$application->get('update')->setCode(function ($input, $output) use ($installer) {
|
|
|
|
$installer
|
|
|
|
->setDevMode($input->getOption('dev'))
|
|
|
|
->setUpdate(true)
|
|
|
|
->setUpdateWhitelist($input->getArgument('packages'));
|
|
|
|
|
2012-08-05 05:55:54 +00:00
|
|
|
return $installer->run() ? 0 : 1;
|
2012-05-27 22:11:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!preg_match('{^(install|update)\b}', $run)) {
|
|
|
|
throw new \UnexpectedValueException('The run command only supports install and update');
|
|
|
|
}
|
|
|
|
|
|
|
|
$application->setAutoExit(false);
|
|
|
|
$appOutput = fopen('php://memory', 'w+');
|
|
|
|
$result = $application->run(new StringInput($run), new StreamOutput($appOutput));
|
|
|
|
fseek($appOutput, 0);
|
|
|
|
$this->assertEquals(0, $result, $output . stream_get_contents($appOutput));
|
2012-05-13 19:40:42 +00:00
|
|
|
|
2012-07-01 16:27:16 +00:00
|
|
|
if ($expectLock) {
|
|
|
|
unset($actualLock['hash']);
|
|
|
|
$this->assertEquals($expectLock, $actualLock);
|
|
|
|
}
|
|
|
|
|
2012-05-13 19:40:42 +00:00
|
|
|
$installationManager = $composer->getInstallationManager();
|
|
|
|
$this->assertSame($expect, implode("\n", $installationManager->getTrace()));
|
2012-09-05 18:39:45 +00:00
|
|
|
|
|
|
|
if ($expectOutput) {
|
|
|
|
$this->assertEquals($expectOutput, $output);
|
|
|
|
}
|
2012-05-13 19:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getIntegrationTests()
|
|
|
|
{
|
|
|
|
$fixturesDir = realpath(__DIR__.'/Fixtures/installer/');
|
|
|
|
$tests = array();
|
|
|
|
|
|
|
|
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
|
|
|
|
if (!preg_match('/\.test$/', $file)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$test = file_get_contents($file->getRealpath());
|
|
|
|
|
2012-05-13 20:36:32 +00:00
|
|
|
$content = '(?:.(?!--[A-Z]))+';
|
2012-05-13 19:40:42 +00:00
|
|
|
$pattern = '{^
|
|
|
|
--TEST--\s*(?P<test>.*?)\s*
|
2012-05-13 20:36:32 +00:00
|
|
|
(?:--CONDITION--\s*(?P<condition>'.$content.'))?\s*
|
|
|
|
--COMPOSER--\s*(?P<composer>'.$content.')\s*
|
|
|
|
(?:--LOCK--\s*(?P<lock>'.$content.'))?\s*
|
|
|
|
(?:--INSTALLED--\s*(?P<installed>'.$content.'))?\s*
|
2012-08-13 23:16:40 +00:00
|
|
|
(?:--INSTALLED-DEV--\s*(?P<installedDev>'.$content.'))?\s*
|
2012-05-27 22:11:18 +00:00
|
|
|
--RUN--\s*(?P<run>.*?)\s*
|
2012-07-01 16:27:16 +00:00
|
|
|
(?:--EXPECT-LOCK--\s*(?P<expectLock>'.$content.'))?\s*
|
2012-09-05 18:39:45 +00:00
|
|
|
(?:--EXPECT-OUTPUT--\s*(?P<expectOutput>'.$content.'))?\s*
|
2012-05-27 22:11:18 +00:00
|
|
|
--EXPECT--\s*(?P<expect>.*?)\s*
|
2012-05-13 19:40:42 +00:00
|
|
|
$}xs';
|
|
|
|
|
|
|
|
$installed = array();
|
|
|
|
$installedDev = array();
|
|
|
|
$lock = array();
|
2012-07-01 16:27:16 +00:00
|
|
|
$expectLock = array();
|
2012-05-13 19:40:42 +00:00
|
|
|
|
|
|
|
if (preg_match($pattern, $test, $match)) {
|
|
|
|
try {
|
|
|
|
$message = $match['test'];
|
|
|
|
$condition = !empty($match['condition']) ? $match['condition'] : null;
|
|
|
|
$composer = JsonFile::parseJson($match['composer']);
|
|
|
|
if (!empty($match['lock'])) {
|
|
|
|
$lock = JsonFile::parseJson($match['lock']);
|
2012-05-28 16:57:59 +00:00
|
|
|
if (!isset($lock['hash'])) {
|
|
|
|
$lock['hash'] = md5(json_encode($composer));
|
|
|
|
}
|
2012-05-13 19:40:42 +00:00
|
|
|
}
|
|
|
|
if (!empty($match['installed'])) {
|
|
|
|
$installed = JsonFile::parseJson($match['installed']);
|
|
|
|
}
|
|
|
|
if (!empty($match['installedDev'])) {
|
|
|
|
$installedDev = JsonFile::parseJson($match['installedDev']);
|
|
|
|
}
|
2012-05-27 22:11:18 +00:00
|
|
|
$run = $match['run'];
|
2012-07-01 16:27:16 +00:00
|
|
|
if (!empty($match['expectLock'])) {
|
|
|
|
$expectLock = JsonFile::parseJson($match['expectLock']);
|
|
|
|
}
|
2012-09-05 18:39:45 +00:00
|
|
|
$expectOutput = $match['expectOutput'];
|
2012-05-13 19:40:42 +00:00
|
|
|
$expect = $match['expect'];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
die(sprintf('Test "%s" is not valid, did not match the expected format.', str_replace($fixturesDir.'/', '', $file)));
|
|
|
|
}
|
|
|
|
|
2012-09-05 18:39:45 +00:00
|
|
|
$tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $installedDev, $run, $expectLock, $expectOutput, $expect);
|
2012-05-13 19:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
2012-04-27 09:42:58 +00:00
|
|
|
}
|