InstallerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. return $installer->run();
  173. });
  174. $application->get('update')->setCode(function ($input, $output) use ($installer) {
  175. $installer
  176. ->setDevMode(!$input->getOption('no-dev'))
  177. ->setUpdate(true)
  178. ->setDryRun($input->getOption('dry-run'))
  179. ->setUpdateWhitelist($input->getArgument('packages'))
  180. ->setWhitelistDependencies($input->getOption('with-dependencies'));
  181. return $installer->run();
  182. });
  183. if (!preg_match('{^(install|update)\b}', $run)) {
  184. throw new \UnexpectedValueException('The run command only supports install and update');
  185. }
  186. $application->setAutoExit(false);
  187. $appOutput = fopen('php://memory', 'w+');
  188. $result = $application->run(new StringInput($run), new StreamOutput($appOutput));
  189. fseek($appOutput, 0);
  190. $this->assertEquals($expectExitCode, $result, $output . stream_get_contents($appOutput));
  191. if ($expectLock) {
  192. unset($actualLock['hash']);
  193. unset($actualLock['_readme']);
  194. $this->assertEquals($expectLock, $actualLock);
  195. }
  196. $installationManager = $composer->getInstallationManager();
  197. $this->assertSame($expect, implode("\n", $installationManager->getTrace()));
  198. if ($expectOutput) {
  199. $this->assertEquals($expectOutput, $output);
  200. }
  201. }
  202. public function getIntegrationTests()
  203. {
  204. $fixturesDir = realpath(__DIR__.'/Fixtures/installer/');
  205. $tests = array();
  206. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  207. if (!preg_match('/\.test$/', $file)) {
  208. continue;
  209. }
  210. $test = file_get_contents($file->getRealpath());
  211. $content = '(?:.(?!--[A-Z]))+';
  212. $pattern = '{^
  213. --TEST--\s*(?P<test>.*?)\s*
  214. (?:--CONDITION--\s*(?P<condition>'.$content.'))?\s*
  215. --COMPOSER--\s*(?P<composer>'.$content.')\s*
  216. (?:--LOCK--\s*(?P<lock>'.$content.'))?\s*
  217. (?:--INSTALLED--\s*(?P<installed>'.$content.'))?\s*
  218. --RUN--\s*(?P<run>.*?)\s*
  219. (?:--EXPECT-LOCK--\s*(?P<expectLock>'.$content.'))?\s*
  220. (?:--EXPECT-OUTPUT--\s*(?P<expectOutput>'.$content.'))?\s*
  221. (?:--EXPECT-EXIT-CODE--\s*(?P<expectExitCode>\d+))?\s*
  222. --EXPECT--\s*(?P<expect>.*?)\s*
  223. $}xs';
  224. $installed = array();
  225. $installedDev = array();
  226. $lock = array();
  227. $expectLock = array();
  228. $expectExitCode = 0;
  229. if (preg_match($pattern, $test, $match)) {
  230. try {
  231. $message = $match['test'];
  232. $condition = !empty($match['condition']) ? $match['condition'] : null;
  233. $composer = JsonFile::parseJson($match['composer']);
  234. if (!empty($match['lock'])) {
  235. $lock = JsonFile::parseJson($match['lock']);
  236. if (!isset($lock['hash'])) {
  237. $lock['hash'] = md5(json_encode($composer));
  238. }
  239. }
  240. if (!empty($match['installed'])) {
  241. $installed = JsonFile::parseJson($match['installed']);
  242. }
  243. $run = $match['run'];
  244. if (!empty($match['expectLock'])) {
  245. $expectLock = JsonFile::parseJson($match['expectLock']);
  246. }
  247. $expectOutput = $match['expectOutput'];
  248. $expect = $match['expect'];
  249. $expectExitCode = (int) $match['expectExitCode'];
  250. } catch (\Exception $e) {
  251. die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
  252. }
  253. } else {
  254. die(sprintf('Test "%s" is not valid, did not match the expected format.', str_replace($fixturesDir.'/', '', $file)));
  255. }
  256. $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode);
  257. }
  258. return $tests;
  259. }
  260. }