InstallerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test;
  12. use Composer\Installer;
  13. use Composer\Console\Application;
  14. use Composer\Config;
  15. use Composer\Json\JsonFile;
  16. use Composer\Repository\ArrayRepository;
  17. use Composer\Repository\RepositoryManager;
  18. use Composer\Repository\InstalledArrayRepository;
  19. use Composer\Package\RootPackageInterface;
  20. use Composer\Package\Link;
  21. use Composer\Package\Locker;
  22. use Composer\Test\Mock\FactoryMock;
  23. use Composer\Test\Mock\InstalledFilesystemRepositoryMock;
  24. use Composer\Test\Mock\InstallationManagerMock;
  25. use Symfony\Component\Console\Input\StringInput;
  26. use Symfony\Component\Console\Output\StreamOutput;
  27. use Composer\TestCase;
  28. class InstallerTest extends TestCase
  29. {
  30. protected $prevCwd;
  31. public function setUp()
  32. {
  33. $this->prevCwd = getcwd();
  34. chdir(__DIR__);
  35. }
  36. public function tearDown()
  37. {
  38. chdir($this->prevCwd);
  39. }
  40. /**
  41. * @dataProvider provideInstaller
  42. */
  43. public function testInstaller(RootPackageInterface $rootPackage, $repositories, array $options)
  44. {
  45. $io = $this->getMock('Composer\IO\IOInterface');
  46. $downloadManager = $this->getMock('Composer\Downloader\DownloadManager', array(), array($io));
  47. $config = $this->getMock('Composer\Config');
  48. $repositoryManager = new RepositoryManager($io, $config);
  49. $repositoryManager->setLocalRepository(new InstalledArrayRepository());
  50. if (!is_array($repositories)) {
  51. $repositories = array($repositories);
  52. }
  53. foreach ($repositories as $repository) {
  54. $repositoryManager->addRepository($repository);
  55. }
  56. $locker = $this->getMockBuilder('Composer\Package\Locker')->disableOriginalConstructor()->getMock();
  57. $installationManager = new InstallationManagerMock();
  58. $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
  59. $autoloadGenerator = $this->getMockBuilder('Composer\Autoload\AutoloadGenerator')->disableOriginalConstructor()->getMock();
  60. $installer = new Installer($io, $config, clone $rootPackage, $downloadManager, $repositoryManager, $locker, $installationManager, $eventDispatcher, $autoloadGenerator);
  61. $result = $installer->run();
  62. $this->assertSame(0, $result);
  63. $expectedInstalled = isset($options['install']) ? $options['install'] : array();
  64. $expectedUpdated = isset($options['update']) ? $options['update'] : array();
  65. $expectedUninstalled = isset($options['uninstall']) ? $options['uninstall'] : array();
  66. $installed = $installationManager->getInstalledPackages();
  67. $this->assertSame($expectedInstalled, $installed);
  68. $updated = $installationManager->getUpdatedPackages();
  69. $this->assertSame($expectedUpdated, $updated);
  70. $uninstalled = $installationManager->getUninstalledPackages();
  71. $this->assertSame($expectedUninstalled, $uninstalled);
  72. }
  73. public function provideInstaller()
  74. {
  75. $cases = array();
  76. // when A requires B and B requires A, and A is a non-published root package
  77. // the install of B should succeed
  78. $a = $this->getPackage('A', '1.0.0', 'Composer\Package\RootPackage');
  79. $a->setRequires(array(
  80. new Link('A', 'B', $this->getVersionConstraint('=', '1.0.0')),
  81. ));
  82. $b = $this->getPackage('B', '1.0.0');
  83. $b->setRequires(array(
  84. new Link('B', 'A', $this->getVersionConstraint('=', '1.0.0')),
  85. ));
  86. $cases[] = array(
  87. $a,
  88. new ArrayRepository(array($b)),
  89. array(
  90. 'install' => array($b)
  91. ),
  92. );
  93. // #480: when A requires B and B requires A, and A is a published root package
  94. // only B should be installed, as A is the root
  95. $a = $this->getPackage('A', '1.0.0', 'Composer\Package\RootPackage');
  96. $a->setRequires(array(
  97. new Link('A', 'B', $this->getVersionConstraint('=', '1.0.0')),
  98. ));
  99. $b = $this->getPackage('B', '1.0.0');
  100. $b->setRequires(array(
  101. new Link('B', 'A', $this->getVersionConstraint('=', '1.0.0')),
  102. ));
  103. $cases[] = array(
  104. $a,
  105. new ArrayRepository(array($a, $b)),
  106. array(
  107. 'install' => array($b)
  108. ),
  109. );
  110. return $cases;
  111. }
  112. /**
  113. * @dataProvider getIntegrationTests
  114. */
  115. public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode)
  116. {
  117. if ($condition) {
  118. eval('$res = '.$condition.';');
  119. if (!$res) {
  120. $this->markTestSkipped($condition);
  121. }
  122. }
  123. $output = null;
  124. $io = $this->getMock('Composer\IO\IOInterface');
  125. $io->expects($this->any())
  126. ->method('write')
  127. ->will($this->returnCallback(function ($text, $newline) use (&$output) {
  128. $output .= $text . ($newline ? "\n" : "");
  129. }));
  130. $composer = FactoryMock::create($io, $composerConfig);
  131. $jsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
  132. $jsonMock->expects($this->any())
  133. ->method('read')
  134. ->will($this->returnValue($installed));
  135. $jsonMock->expects($this->any())
  136. ->method('exists')
  137. ->will($this->returnValue(true));
  138. $repositoryManager = $composer->getRepositoryManager();
  139. $repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock));
  140. $lockJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
  141. $lockJsonMock->expects($this->any())
  142. ->method('read')
  143. ->will($this->returnValue($lock));
  144. $lockJsonMock->expects($this->any())
  145. ->method('exists')
  146. ->will($this->returnValue(true));
  147. if ($expectLock) {
  148. $actualLock = array();
  149. $lockJsonMock->expects($this->atLeastOnce())
  150. ->method('write')
  151. ->will($this->returnCallback(function ($hash, $options) use (&$actualLock) {
  152. // need to do assertion outside of mock for nice phpunit output
  153. // so store value temporarily in reference for later assetion
  154. $actualLock = $hash;
  155. }));
  156. }
  157. $locker = new Locker($io, $lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
  158. $composer->setLocker($locker);
  159. $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
  160. $autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator', array(), array($eventDispatcher));
  161. $composer->setAutoloadGenerator($autoloadGenerator);
  162. $composer->setEventDispatcher($eventDispatcher);
  163. $installer = Installer::create(
  164. $io,
  165. $composer
  166. );
  167. $application = new Application;
  168. $application->get('install')->setCode(function ($input, $output) use ($installer) {
  169. $installer
  170. ->setDevMode(!$input->getOption('no-dev'))
  171. ->setDryRun($input->getOption('dry-run'))
  172. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
  173. return $installer->run();
  174. });
  175. $application->get('update')->setCode(function ($input, $output) use ($installer) {
  176. $installer
  177. ->setDevMode(!$input->getOption('no-dev'))
  178. ->setUpdate(true)
  179. ->setDryRun($input->getOption('dry-run'))
  180. ->setUpdateWhitelist($input->getArgument('packages'))
  181. ->setWhitelistDependencies($input->getOption('with-dependencies'))
  182. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
  183. return $installer->run();
  184. });
  185. if (!preg_match('{^(install|update)\b}', $run)) {
  186. throw new \UnexpectedValueException('The run command only supports install and update');
  187. }
  188. $application->setAutoExit(false);
  189. $appOutput = fopen('php://memory', 'w+');
  190. $result = $application->run(new StringInput($run), new StreamOutput($appOutput));
  191. fseek($appOutput, 0);
  192. $this->assertEquals($expectExitCode, $result, $output . stream_get_contents($appOutput));
  193. if ($expectLock) {
  194. unset($actualLock['hash']);
  195. unset($actualLock['_readme']);
  196. $this->assertEquals($expectLock, $actualLock);
  197. }
  198. $installationManager = $composer->getInstallationManager();
  199. $this->assertSame($expect, implode("\n", $installationManager->getTrace()));
  200. if ($expectOutput) {
  201. $this->assertEquals($expectOutput, $output);
  202. }
  203. }
  204. public function getIntegrationTests()
  205. {
  206. $fixturesDir = realpath(__DIR__.'/Fixtures/installer/');
  207. $tests = array();
  208. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  209. if (!preg_match('/\.test$/', $file)) {
  210. continue;
  211. }
  212. $test = file_get_contents($file->getRealpath());
  213. $content = '(?:.(?!--[A-Z]))+';
  214. $pattern = '{^
  215. --TEST--\s*(?P<test>.*?)\s*
  216. (?:--CONDITION--\s*(?P<condition>'.$content.'))?\s*
  217. --COMPOSER--\s*(?P<composer>'.$content.')\s*
  218. (?:--LOCK--\s*(?P<lock>'.$content.'))?\s*
  219. (?:--INSTALLED--\s*(?P<installed>'.$content.'))?\s*
  220. --RUN--\s*(?P<run>.*?)\s*
  221. (?:--EXPECT-LOCK--\s*(?P<expectLock>'.$content.'))?\s*
  222. (?:--EXPECT-OUTPUT--\s*(?P<expectOutput>'.$content.'))?\s*
  223. (?:--EXPECT-EXIT-CODE--\s*(?P<expectExitCode>\d+))?\s*
  224. --EXPECT--\s*(?P<expect>.*?)\s*
  225. $}xs';
  226. $installed = array();
  227. $installedDev = array();
  228. $lock = array();
  229. $expectLock = array();
  230. $expectExitCode = 0;
  231. if (preg_match($pattern, $test, $match)) {
  232. try {
  233. $message = $match['test'];
  234. $condition = !empty($match['condition']) ? $match['condition'] : null;
  235. $composer = JsonFile::parseJson($match['composer']);
  236. if (isset($composer['repositories'])) {
  237. foreach ($composer['repositories'] as &$repo) {
  238. if ($repo['type'] !== 'composer') {
  239. continue;
  240. }
  241. // Change paths like file://foobar to file:///path/to/fixtures
  242. if (preg_match('{^file://[^/]}', $repo['url'])) {
  243. $repo['url'] = "file://${fixturesDir}/" . substr($repo['url'], 7);
  244. }
  245. unset($repo);
  246. }
  247. }
  248. if (!empty($match['lock'])) {
  249. $lock = JsonFile::parseJson($match['lock']);
  250. if (!isset($lock['hash'])) {
  251. $lock['hash'] = md5(json_encode($composer));
  252. }
  253. }
  254. if (!empty($match['installed'])) {
  255. $installed = JsonFile::parseJson($match['installed']);
  256. }
  257. $run = $match['run'];
  258. if (!empty($match['expectLock'])) {
  259. $expectLock = JsonFile::parseJson($match['expectLock']);
  260. }
  261. $expectOutput = $match['expectOutput'];
  262. $expect = $match['expect'];
  263. $expectExitCode = (int) $match['expectExitCode'];
  264. } catch (\Exception $e) {
  265. die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
  266. }
  267. } else {
  268. die(sprintf('Test "%s" is not valid, did not match the expected format.', str_replace($fixturesDir.'/', '', $file)));
  269. }
  270. $tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode);
  271. }
  272. return $tests;
  273. }
  274. }