InstallerTest.php 11 KB

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