InstallerTest.php 12 KB

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