PluginInstallerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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\Plugin;
  12. use Composer\Composer;
  13. use Composer\Config;
  14. use Composer\Installer\PluginInstaller;
  15. use Composer\Package\CompletePackage;
  16. use Composer\Package\Loader\JsonLoader;
  17. use Composer\Package\Loader\ArrayLoader;
  18. use Composer\Plugin\PluginManager;
  19. use Composer\Autoload\AutoloadGenerator;
  20. use Composer\Test\TestCase;
  21. use Composer\Util\Filesystem;
  22. class PluginInstallerTest extends TestCase
  23. {
  24. /**
  25. * @var Composer
  26. */
  27. protected $composer;
  28. /**
  29. * @var PluginManager
  30. */
  31. protected $pm;
  32. /**
  33. * @var AutoloadGenerator
  34. */
  35. protected $autoloadGenerator;
  36. /**
  37. * @var CompletePackage[]
  38. */
  39. protected $packages;
  40. /**
  41. * @var string
  42. */
  43. protected $directory;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $im;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $repository;
  52. /**
  53. * @var \PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $io;
  56. protected function setUp()
  57. {
  58. $loader = new JsonLoader(new ArrayLoader());
  59. $this->packages = array();
  60. $this->directory = $this->getUniqueTmpDirectory();
  61. for ($i = 1; $i <= 8; $i++) {
  62. $filename = '/Fixtures/plugin-v'.$i.'/composer.json';
  63. mkdir(dirname($this->directory . $filename), 0777, true);
  64. $this->packages[] = $loader->load(__DIR__ . $filename);
  65. }
  66. $dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
  70. $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $rm->expects($this->any())
  74. ->method('getLocalRepository')
  75. ->will($this->returnValue($this->repository));
  76. $im = $this->getMockBuilder('Composer\Installer\InstallationManager')->getMock();
  77. $im->expects($this->any())
  78. ->method('getInstallPath')
  79. ->will($this->returnCallback(function ($package) {
  80. return __DIR__.'/Fixtures/'.$package->getPrettyName();
  81. }));
  82. $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  83. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
  84. $this->autoloadGenerator = new AutoloadGenerator($dispatcher);
  85. $this->composer = new Composer();
  86. $config = new Config(false);
  87. $this->composer->setConfig($config);
  88. $this->composer->setDownloadManager($dm);
  89. $this->composer->setRepositoryManager($rm);
  90. $this->composer->setInstallationManager($im);
  91. $this->composer->setAutoloadGenerator($this->autoloadGenerator);
  92. $this->pm = new PluginManager($this->io, $this->composer);
  93. $this->composer->setPluginManager($this->pm);
  94. $config->merge(array(
  95. 'config' => array(
  96. 'vendor-dir' => $this->directory.'/Fixtures/',
  97. 'home' => $this->directory.'/Fixtures',
  98. 'bin-dir' => $this->directory.'/Fixtures/bin',
  99. ),
  100. ));
  101. }
  102. protected function tearDown()
  103. {
  104. $filesystem = new Filesystem();
  105. $filesystem->removeDirectory($this->directory);
  106. }
  107. public function testInstallNewPlugin()
  108. {
  109. $this->repository
  110. ->expects($this->exactly(2))
  111. ->method('getPackages')
  112. ->will($this->returnValue(array()));
  113. $installer = new PluginInstaller($this->io, $this->composer);
  114. $this->pm->loadInstalledPlugins();
  115. $installer->install($this->repository, $this->packages[0]);
  116. $plugins = $this->pm->getPlugins();
  117. $this->assertEquals('installer-v1', $plugins[0]->version);
  118. }
  119. public function testInstallMultiplePlugins()
  120. {
  121. $this->repository
  122. ->expects($this->exactly(2))
  123. ->method('getPackages')
  124. ->will($this->returnValue(array($this->packages[3])));
  125. $installer = new PluginInstaller($this->io, $this->composer);
  126. $this->pm->loadInstalledPlugins();
  127. $installer->install($this->repository, $this->packages[3]);
  128. $plugins = $this->pm->getPlugins();
  129. $this->assertEquals('plugin1', $plugins[0]->name);
  130. $this->assertEquals('installer-v4', $plugins[0]->version);
  131. $this->assertEquals('plugin2', $plugins[1]->name);
  132. $this->assertEquals('installer-v4', $plugins[1]->version);
  133. }
  134. public function testUpgradeWithNewClassName()
  135. {
  136. $this->repository
  137. ->expects($this->exactly(3))
  138. ->method('getPackages')
  139. ->will($this->returnValue(array($this->packages[0])));
  140. $this->repository
  141. ->expects($this->exactly(2))
  142. ->method('hasPackage')
  143. ->will($this->onConsecutiveCalls(true, false));
  144. $installer = new PluginInstaller($this->io, $this->composer);
  145. $this->pm->loadInstalledPlugins();
  146. $installer->update($this->repository, $this->packages[0], $this->packages[1]);
  147. $plugins = $this->pm->getPlugins();
  148. $this->assertEquals('installer-v2', $plugins[1]->version);
  149. }
  150. public function testUpgradeWithSameClassName()
  151. {
  152. $this->repository
  153. ->expects($this->exactly(3))
  154. ->method('getPackages')
  155. ->will($this->returnValue(array($this->packages[1])));
  156. $this->repository
  157. ->expects($this->exactly(2))
  158. ->method('hasPackage')
  159. ->will($this->onConsecutiveCalls(true, false));
  160. $installer = new PluginInstaller($this->io, $this->composer);
  161. $this->pm->loadInstalledPlugins();
  162. $installer->update($this->repository, $this->packages[1], $this->packages[2]);
  163. $plugins = $this->pm->getPlugins();
  164. $this->assertEquals('installer-v3', $plugins[1]->version);
  165. }
  166. public function testRegisterPluginOnlyOneTime()
  167. {
  168. $this->repository
  169. ->expects($this->exactly(2))
  170. ->method('getPackages')
  171. ->will($this->returnValue(array()));
  172. $installer = new PluginInstaller($this->io, $this->composer);
  173. $this->pm->loadInstalledPlugins();
  174. $installer->install($this->repository, $this->packages[0]);
  175. $installer->install($this->repository, clone $this->packages[0]);
  176. $plugins = $this->pm->getPlugins();
  177. $this->assertCount(1, $plugins);
  178. $this->assertEquals('installer-v1', $plugins[0]->version);
  179. }
  180. /**
  181. * @param string $newPluginApiVersion
  182. * @param CompletePackage[] $plugins
  183. */
  184. private function setPluginApiVersionWithPlugins($newPluginApiVersion, array $plugins = array())
  185. {
  186. // reset the plugin manager's installed plugins
  187. $this->pm = $this->getMockBuilder('Composer\Plugin\PluginManager')
  188. ->setMethods(array('getPluginApiVersion'))
  189. ->setConstructorArgs(array($this->io, $this->composer))
  190. ->getMock();
  191. // mock the Plugin API version
  192. $this->pm->expects($this->any())
  193. ->method('getPluginApiVersion')
  194. ->will($this->returnValue($newPluginApiVersion));
  195. $plugApiInternalPackage = $this->getPackage(
  196. 'composer-plugin-api',
  197. $newPluginApiVersion,
  198. 'Composer\Package\CompletePackage'
  199. );
  200. // Add the plugins to the repo along with the internal Plugin package on which they all rely.
  201. $this->repository
  202. ->expects($this->any())
  203. ->method('getPackages')
  204. ->will($this->returnCallback(function () use ($plugApiInternalPackage, $plugins) {
  205. return array_merge(array($plugApiInternalPackage), $plugins);
  206. }));
  207. $this->pm->loadInstalledPlugins();
  208. }
  209. public function testStarPluginVersionWorksWithAnyAPIVersion()
  210. {
  211. $starVersionPlugin = array($this->packages[4]);
  212. $this->setPluginApiVersionWithPlugins('1.0.0', $starVersionPlugin);
  213. $this->assertCount(1, $this->pm->getPlugins());
  214. $this->setPluginApiVersionWithPlugins('1.9.9', $starVersionPlugin);
  215. $this->assertCount(1, $this->pm->getPlugins());
  216. $this->setPluginApiVersionWithPlugins('2.0.0-dev', $starVersionPlugin);
  217. $this->assertCount(1, $this->pm->getPlugins());
  218. $this->setPluginApiVersionWithPlugins('100.0.0-stable', $starVersionPlugin);
  219. $this->assertCount(1, $this->pm->getPlugins());
  220. }
  221. public function testPluginConstraintWorksOnlyWithCertainAPIVersion()
  222. {
  223. $pluginWithApiConstraint = array($this->packages[5]);
  224. $this->setPluginApiVersionWithPlugins('1.0.0', $pluginWithApiConstraint);
  225. $this->assertCount(0, $this->pm->getPlugins());
  226. $this->setPluginApiVersionWithPlugins('1.1.9', $pluginWithApiConstraint);
  227. $this->assertCount(0, $this->pm->getPlugins());
  228. $this->setPluginApiVersionWithPlugins('1.2.0', $pluginWithApiConstraint);
  229. $this->assertCount(1, $this->pm->getPlugins());
  230. $this->setPluginApiVersionWithPlugins('1.9.9', $pluginWithApiConstraint);
  231. $this->assertCount(1, $this->pm->getPlugins());
  232. }
  233. public function testPluginRangeConstraintsWorkOnlyWithCertainAPIVersion()
  234. {
  235. $pluginWithApiConstraint = array($this->packages[6]);
  236. $this->setPluginApiVersionWithPlugins('1.0.0', $pluginWithApiConstraint);
  237. $this->assertCount(0, $this->pm->getPlugins());
  238. $this->setPluginApiVersionWithPlugins('3.0.0', $pluginWithApiConstraint);
  239. $this->assertCount(1, $this->pm->getPlugins());
  240. $this->setPluginApiVersionWithPlugins('5.5.0', $pluginWithApiConstraint);
  241. $this->assertCount(0, $this->pm->getPlugins());
  242. }
  243. public function testCommandProviderCapability()
  244. {
  245. $this->repository
  246. ->expects($this->exactly(2))
  247. ->method('getPackages')
  248. ->will($this->returnValue(array($this->packages[7])));
  249. $installer = new PluginInstaller($this->io, $this->composer);
  250. $this->pm->loadInstalledPlugins();
  251. $caps = $this->pm->getPluginCapabilities('Composer\Plugin\Capability\CommandProvider', array('composer' => $this->composer, 'io' => $this->io));
  252. $this->assertCount(1, $caps);
  253. $this->assertInstanceOf('Composer\Plugin\Capability\CommandProvider', $caps[0]);
  254. $commands = $caps[0]->getCommands();
  255. $this->assertCount(1, $commands);
  256. $this->assertInstanceOf('Composer\Command\BaseCommand', $commands[0]);
  257. }
  258. public function testIncapablePluginIsCorrectlyDetected()
  259. {
  260. $plugin = $this->getMockBuilder('Composer\Plugin\PluginInterface')
  261. ->getMock();
  262. $this->assertNull($this->pm->getPluginCapability($plugin, 'Fake\Ability'));
  263. }
  264. public function testCapabilityImplementsComposerPluginApiClassAndIsConstructedWithArgs()
  265. {
  266. $capabilityApi = 'Composer\Plugin\Capability\Capability';
  267. $capabilityImplementation = 'Composer\Test\Plugin\Mock\Capability';
  268. $plugin = $this->getMockBuilder('Composer\Test\Plugin\Mock\CapablePluginInterface')
  269. ->getMock();
  270. $plugin->expects($this->once())
  271. ->method('getCapabilities')
  272. ->will($this->returnCallback(function () use ($capabilityImplementation, $capabilityApi) {
  273. return array($capabilityApi => $capabilityImplementation);
  274. }));
  275. $capability = $this->pm->getPluginCapability($plugin, $capabilityApi, array('a' => 1, 'b' => 2));
  276. $this->assertInstanceOf($capabilityApi, $capability);
  277. $this->assertInstanceOf($capabilityImplementation, $capability);
  278. $this->assertSame(array('a' => 1, 'b' => 2, 'plugin' => $plugin), $capability->args);
  279. }
  280. public function invalidImplementationClassNames()
  281. {
  282. return array(
  283. array(null),
  284. array(""),
  285. array(0),
  286. array(1000),
  287. array(" "),
  288. array(array(1)),
  289. array(array()),
  290. array(new \stdClass()),
  291. );
  292. }
  293. public function nonExistingOrInvalidImplementationClassTypes()
  294. {
  295. return array(
  296. array('\stdClass'),
  297. array('NonExistentClassLikeMiddleClass'),
  298. );
  299. }
  300. /**
  301. * @dataProvider invalidImplementationClassNames
  302. * @expectedException \UnexpectedValueException
  303. */
  304. public function testQueryingWithInvalidCapabilityClassNameThrows($invalidImplementationClassNames)
  305. {
  306. $capabilityApi = 'Composer\Plugin\Capability\Capability';
  307. $plugin = $this->getMockBuilder('Composer\Test\Plugin\Mock\CapablePluginInterface')
  308. ->getMock();
  309. $plugin->expects($this->once())
  310. ->method('getCapabilities')
  311. ->will($this->returnCallback(function () use ($invalidImplementationClassNames, $capabilityApi) {
  312. return array($capabilityApi => $invalidImplementationClassNames);
  313. }));
  314. $this->pm->getPluginCapability($plugin, $capabilityApi);
  315. }
  316. public function testQueryingNonProvidedCapabilityReturnsNullSafely()
  317. {
  318. $capabilityApi = 'Composer\Plugin\Capability\MadeUpCapability';
  319. $plugin = $this->getMockBuilder('Composer\Test\Plugin\Mock\CapablePluginInterface')
  320. ->getMock();
  321. $plugin->expects($this->once())
  322. ->method('getCapabilities')
  323. ->will($this->returnCallback(function () {
  324. return array();
  325. }));
  326. $this->assertNull($this->pm->getPluginCapability($plugin, $capabilityApi));
  327. }
  328. /**
  329. * @dataProvider nonExistingOrInvalidImplementationClassTypes
  330. * @expectedException \RuntimeException
  331. */
  332. public function testQueryingWithNonExistingOrWrongCapabilityClassTypesThrows($wrongImplementationClassTypes)
  333. {
  334. $this->testQueryingWithInvalidCapabilityClassNameThrows($wrongImplementationClassTypes);
  335. }
  336. }