InstallerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\RootPackageInterface;
  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(RootPackageInterface $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, $config, 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', 'Composer\Package\RootPackage');
  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', 'Composer\Package\RootPackage');
  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, $expectLock, $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. if ($expectLock) {
  146. $actualLock = array();
  147. $lockJsonMock->expects($this->atLeastOnce())
  148. ->method('write')
  149. ->will($this->returnCallback(function ($hash, $options) use (&$actualLock) {
  150. // need to do assertion outside of mock for nice phpunit output
  151. // so store value temporarily in reference for later assetion
  152. $actualLock = $hash;
  153. }));
  154. }
  155. $locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
  156. $composer->setLocker($locker);
  157. $autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator');
  158. $installer = Installer::create(
  159. $io,
  160. $composer,
  161. null,
  162. $autoloadGenerator
  163. );
  164. $application = new Application;
  165. $application->get('install')->setCode(function ($input, $output) use ($installer) {
  166. $installer->setDevMode($input->getOption('dev'));
  167. return $installer->run() ? 0 : 1;
  168. });
  169. $application->get('update')->setCode(function ($input, $output) use ($installer) {
  170. $installer
  171. ->setDevMode($input->getOption('dev'))
  172. ->setUpdate(true)
  173. ->setUpdateWhitelist($input->getArgument('packages'));
  174. return $installer->run() ? 0 : 1;
  175. });
  176. if (!preg_match('{^(install|update)\b}', $run)) {
  177. throw new \UnexpectedValueException('The run command only supports install and update');
  178. }
  179. $application->setAutoExit(false);
  180. $appOutput = fopen('php://memory', 'w+');
  181. $result = $application->run(new StringInput($run), new StreamOutput($appOutput));
  182. fseek($appOutput, 0);
  183. $this->assertEquals(0, $result, $output . stream_get_contents($appOutput));
  184. if ($expectLock) {
  185. unset($actualLock['hash']);
  186. $this->assertEquals($expectLock, $actualLock);
  187. }
  188. $installationManager = $composer->getInstallationManager();
  189. $this->assertSame($expect, implode("\n", $installationManager->getTrace()));
  190. }
  191. public function getIntegrationTests()
  192. {
  193. $fixturesDir = realpath(__DIR__.'/Fixtures/installer/');
  194. $tests = array();
  195. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  196. if (!preg_match('/\.test$/', $file)) {
  197. continue;
  198. }
  199. $test = file_get_contents($file->getRealpath());
  200. $content = '(?:.(?!--[A-Z]))+';
  201. $pattern = '{^
  202. --TEST--\s*(?P<test>.*?)\s*
  203. (?:--CONDITION--\s*(?P<condition>'.$content.'))?\s*
  204. --COMPOSER--\s*(?P<composer>'.$content.')\s*
  205. (?:--LOCK--\s*(?P<lock>'.$content.'))?\s*
  206. (?:--INSTALLED--\s*(?P<installed>'.$content.'))?\s*
  207. (?:--INSTALLED-DEV--\s*(?P<installedDev>'.$content.'))?\s*
  208. --RUN--\s*(?P<run>.*?)\s*
  209. (?:--EXPECT-LOCK--\s*(?P<expectLock>'.$content.'))?\s*
  210. --EXPECT--\s*(?P<expect>.*?)\s*
  211. $}xs';
  212. $installed = array();
  213. $installedDev = array();
  214. $lock = array();
  215. $expectLock = array();
  216. if (preg_match($pattern, $test, $match)) {
  217. try {
  218. $message = $match['test'];
  219. $condition = !empty($match['condition']) ? $match['condition'] : null;
  220. $composer = JsonFile::parseJson($match['composer']);
  221. if (!empty($match['lock'])) {
  222. $lock = JsonFile::parseJson($match['lock']);
  223. if (!isset($lock['hash'])) {
  224. $lock['hash'] = md5(json_encode($composer));
  225. }
  226. }
  227. if (!empty($match['installed'])) {
  228. $installed = JsonFile::parseJson($match['installed']);
  229. }
  230. if (!empty($match['installedDev'])) {
  231. $installedDev = JsonFile::parseJson($match['installedDev']);
  232. }
  233. $run = $match['run'];
  234. if (!empty($match['expectLock'])) {
  235. $expectLock = JsonFile::parseJson($match['expectLock']);
  236. }
  237. $expect = $match['expect'];
  238. } catch (\Exception $e) {
  239. die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
  240. }
  241. } else {
  242. die(sprintf('Test "%s" is not valid, did not match the expected format.', str_replace($fixturesDir.'/', '', $file)));
  243. }
  244. $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $installedDev, $run, $expectLock, $expect);
  245. }
  246. return $tests;
  247. }
  248. }