InstallerTest.php 10 KB

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