DownloadManagerTest.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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\Downloader;
  12. use Composer\Downloader\DownloadManager;
  13. use Composer\Test\TestCase;
  14. class DownloadManagerTest extends TestCase
  15. {
  16. protected $filesystem;
  17. protected $io;
  18. public function setUp()
  19. {
  20. $this->filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  21. $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  22. }
  23. public function testSetGetDownloader()
  24. {
  25. $downloader = $this->createDownloaderMock();
  26. $manager = new DownloadManager($this->io, false, $this->filesystem);
  27. $manager->setDownloader('test', $downloader);
  28. $this->assertSame($downloader, $manager->getDownloader('test'));
  29. $this->setExpectedException('InvalidArgumentException');
  30. $manager->getDownloader('unregistered');
  31. }
  32. public function testGetDownloaderForIncorrectlyInstalledPackage()
  33. {
  34. $package = $this->createPackageMock();
  35. $package
  36. ->expects($this->once())
  37. ->method('getInstallationSource')
  38. ->will($this->returnValue(null));
  39. $manager = new DownloadManager($this->io, false, $this->filesystem);
  40. $this->setExpectedException('InvalidArgumentException');
  41. $manager->getDownloaderForPackage($package);
  42. }
  43. public function testGetDownloaderForCorrectlyInstalledDistPackage()
  44. {
  45. $package = $this->createPackageMock();
  46. $package
  47. ->expects($this->once())
  48. ->method('getInstallationSource')
  49. ->will($this->returnValue('dist'));
  50. $package
  51. ->expects($this->once())
  52. ->method('getDistType')
  53. ->will($this->returnValue('pear'));
  54. $downloader = $this->createDownloaderMock();
  55. $downloader
  56. ->expects($this->once())
  57. ->method('getInstallationSource')
  58. ->will($this->returnValue('dist'));
  59. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  60. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  61. ->setMethods(array('getDownloader'))
  62. ->getMock();
  63. $manager
  64. ->expects($this->once())
  65. ->method('getDownloader')
  66. ->with('pear')
  67. ->will($this->returnValue($downloader));
  68. $this->assertSame($downloader, $manager->getDownloaderForPackage($package));
  69. }
  70. public function testGetDownloaderForIncorrectlyInstalledDistPackage()
  71. {
  72. $package = $this->createPackageMock();
  73. $package
  74. ->expects($this->once())
  75. ->method('getInstallationSource')
  76. ->will($this->returnValue('dist'));
  77. $package
  78. ->expects($this->once())
  79. ->method('getDistType')
  80. ->will($this->returnValue('git'));
  81. $downloader = $this->createDownloaderMock();
  82. $downloader
  83. ->expects($this->exactly(2))
  84. ->method('getInstallationSource')
  85. ->will($this->returnValue('source'));
  86. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  87. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  88. ->setMethods(array('getDownloader'))
  89. ->getMock();
  90. $manager
  91. ->expects($this->once())
  92. ->method('getDownloader')
  93. ->with('git')
  94. ->will($this->returnValue($downloader));
  95. $this->setExpectedException('LogicException');
  96. $manager->getDownloaderForPackage($package);
  97. }
  98. public function testGetDownloaderForCorrectlyInstalledSourcePackage()
  99. {
  100. $package = $this->createPackageMock();
  101. $package
  102. ->expects($this->once())
  103. ->method('getInstallationSource')
  104. ->will($this->returnValue('source'));
  105. $package
  106. ->expects($this->once())
  107. ->method('getSourceType')
  108. ->will($this->returnValue('git'));
  109. $downloader = $this->createDownloaderMock();
  110. $downloader
  111. ->expects($this->once())
  112. ->method('getInstallationSource')
  113. ->will($this->returnValue('source'));
  114. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  115. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  116. ->setMethods(array('getDownloader'))
  117. ->getMock();
  118. $manager
  119. ->expects($this->once())
  120. ->method('getDownloader')
  121. ->with('git')
  122. ->will($this->returnValue($downloader));
  123. $this->assertSame($downloader, $manager->getDownloaderForPackage($package));
  124. }
  125. public function testGetDownloaderForIncorrectlyInstalledSourcePackage()
  126. {
  127. $package = $this->createPackageMock();
  128. $package
  129. ->expects($this->once())
  130. ->method('getInstallationSource')
  131. ->will($this->returnValue('source'));
  132. $package
  133. ->expects($this->once())
  134. ->method('getSourceType')
  135. ->will($this->returnValue('pear'));
  136. $downloader = $this->createDownloaderMock();
  137. $downloader
  138. ->expects($this->exactly(2))
  139. ->method('getInstallationSource')
  140. ->will($this->returnValue('dist'));
  141. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  142. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  143. ->setMethods(array('getDownloader'))
  144. ->getMock();
  145. $manager
  146. ->expects($this->once())
  147. ->method('getDownloader')
  148. ->with('pear')
  149. ->will($this->returnValue($downloader));
  150. $this->setExpectedException('LogicException');
  151. $manager->getDownloaderForPackage($package);
  152. }
  153. public function testGetDownloaderForMetapackage()
  154. {
  155. $package = $this->createPackageMock();
  156. $package
  157. ->expects($this->once())
  158. ->method('getType')
  159. ->will($this->returnValue('metapackage'));
  160. $manager = new DownloadManager($this->io, false, $this->filesystem);
  161. $this->assertNull($manager->getDownloaderForPackage($package));
  162. }
  163. public function testFullPackageDownload()
  164. {
  165. $package = $this->createPackageMock();
  166. $package
  167. ->expects($this->once())
  168. ->method('getSourceType')
  169. ->will($this->returnValue('git'));
  170. $package
  171. ->expects($this->once())
  172. ->method('getDistType')
  173. ->will($this->returnValue('pear'));
  174. $package
  175. ->expects($this->once())
  176. ->method('setInstallationSource')
  177. ->with('dist');
  178. $downloader = $this->createDownloaderMock();
  179. $downloader
  180. ->expects($this->once())
  181. ->method('download')
  182. ->with($package, 'target_dir');
  183. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  184. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  185. ->setMethods(array('getDownloaderForPackage'))
  186. ->getMock();
  187. $manager
  188. ->expects($this->once())
  189. ->method('getDownloaderForPackage')
  190. ->with($package)
  191. ->will($this->returnValue($downloader));
  192. $manager->download($package, 'target_dir');
  193. }
  194. public function testFullPackageDownloadFailover()
  195. {
  196. $package = $this->createPackageMock();
  197. $package
  198. ->expects($this->once())
  199. ->method('getSourceType')
  200. ->will($this->returnValue('git'));
  201. $package
  202. ->expects($this->once())
  203. ->method('getDistType')
  204. ->will($this->returnValue('pear'));
  205. $package
  206. ->expects($this->any())
  207. ->method('getPrettyString')
  208. ->will($this->returnValue('prettyPackage'));
  209. $package
  210. ->expects($this->at(3))
  211. ->method('setInstallationSource')
  212. ->with('dist');
  213. $package
  214. ->expects($this->at(5))
  215. ->method('setInstallationSource')
  216. ->with('source');
  217. $downloaderFail = $this->createDownloaderMock();
  218. $downloaderFail
  219. ->expects($this->once())
  220. ->method('download')
  221. ->with($package, 'target_dir')
  222. ->will($this->throwException(new \RuntimeException("Foo")));
  223. $downloaderSuccess = $this->createDownloaderMock();
  224. $downloaderSuccess
  225. ->expects($this->once())
  226. ->method('download')
  227. ->with($package, 'target_dir');
  228. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  229. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  230. ->setMethods(array('getDownloaderForPackage'))
  231. ->getMock();
  232. $manager
  233. ->expects($this->at(0))
  234. ->method('getDownloaderForPackage')
  235. ->with($package)
  236. ->will($this->returnValue($downloaderFail));
  237. $manager
  238. ->expects($this->at(1))
  239. ->method('getDownloaderForPackage')
  240. ->with($package)
  241. ->will($this->returnValue($downloaderSuccess));
  242. $manager->download($package, 'target_dir');
  243. }
  244. public function testBadPackageDownload()
  245. {
  246. $package = $this->createPackageMock();
  247. $package
  248. ->expects($this->once())
  249. ->method('getSourceType')
  250. ->will($this->returnValue(null));
  251. $package
  252. ->expects($this->once())
  253. ->method('getDistType')
  254. ->will($this->returnValue(null));
  255. $manager = new DownloadManager($this->io, false, $this->filesystem);
  256. $this->setExpectedException('InvalidArgumentException');
  257. $manager->download($package, 'target_dir');
  258. }
  259. public function testDistOnlyPackageDownload()
  260. {
  261. $package = $this->createPackageMock();
  262. $package
  263. ->expects($this->once())
  264. ->method('getSourceType')
  265. ->will($this->returnValue(null));
  266. $package
  267. ->expects($this->once())
  268. ->method('getDistType')
  269. ->will($this->returnValue('pear'));
  270. $package
  271. ->expects($this->once())
  272. ->method('setInstallationSource')
  273. ->with('dist');
  274. $downloader = $this->createDownloaderMock();
  275. $downloader
  276. ->expects($this->once())
  277. ->method('download')
  278. ->with($package, 'target_dir');
  279. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  280. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  281. ->setMethods(array('getDownloaderForPackage'))
  282. ->getMock();
  283. $manager
  284. ->expects($this->once())
  285. ->method('getDownloaderForPackage')
  286. ->with($package)
  287. ->will($this->returnValue($downloader));
  288. $manager->download($package, 'target_dir');
  289. }
  290. public function testSourceOnlyPackageDownload()
  291. {
  292. $package = $this->createPackageMock();
  293. $package
  294. ->expects($this->once())
  295. ->method('getSourceType')
  296. ->will($this->returnValue('git'));
  297. $package
  298. ->expects($this->once())
  299. ->method('getDistType')
  300. ->will($this->returnValue(null));
  301. $package
  302. ->expects($this->once())
  303. ->method('setInstallationSource')
  304. ->with('source');
  305. $downloader = $this->createDownloaderMock();
  306. $downloader
  307. ->expects($this->once())
  308. ->method('download')
  309. ->with($package, 'target_dir');
  310. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  311. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  312. ->setMethods(array('getDownloaderForPackage'))
  313. ->getMock();
  314. $manager
  315. ->expects($this->once())
  316. ->method('getDownloaderForPackage')
  317. ->with($package)
  318. ->will($this->returnValue($downloader));
  319. $manager->download($package, 'target_dir');
  320. }
  321. public function testMetapackagePackageDownload()
  322. {
  323. $package = $this->createPackageMock();
  324. $package
  325. ->expects($this->once())
  326. ->method('getSourceType')
  327. ->will($this->returnValue('git'));
  328. $package
  329. ->expects($this->once())
  330. ->method('getDistType')
  331. ->will($this->returnValue(null));
  332. $package
  333. ->expects($this->once())
  334. ->method('setInstallationSource')
  335. ->with('source');
  336. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  337. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  338. ->setMethods(array('getDownloaderForPackage'))
  339. ->getMock();
  340. $manager
  341. ->expects($this->once())
  342. ->method('getDownloaderForPackage')
  343. ->with($package)
  344. ->will($this->returnValue(null)); // There is no downloader for Metapackages.
  345. $manager->download($package, 'target_dir');
  346. }
  347. public function testFullPackageDownloadWithSourcePreferred()
  348. {
  349. $package = $this->createPackageMock();
  350. $package
  351. ->expects($this->once())
  352. ->method('getSourceType')
  353. ->will($this->returnValue('git'));
  354. $package
  355. ->expects($this->once())
  356. ->method('getDistType')
  357. ->will($this->returnValue('pear'));
  358. $package
  359. ->expects($this->once())
  360. ->method('setInstallationSource')
  361. ->with('source');
  362. $downloader = $this->createDownloaderMock();
  363. $downloader
  364. ->expects($this->once())
  365. ->method('download')
  366. ->with($package, 'target_dir');
  367. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  368. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  369. ->setMethods(array('getDownloaderForPackage'))
  370. ->getMock();
  371. $manager
  372. ->expects($this->once())
  373. ->method('getDownloaderForPackage')
  374. ->with($package)
  375. ->will($this->returnValue($downloader));
  376. $manager->setPreferSource(true);
  377. $manager->download($package, 'target_dir');
  378. }
  379. public function testDistOnlyPackageDownloadWithSourcePreferred()
  380. {
  381. $package = $this->createPackageMock();
  382. $package
  383. ->expects($this->once())
  384. ->method('getSourceType')
  385. ->will($this->returnValue(null));
  386. $package
  387. ->expects($this->once())
  388. ->method('getDistType')
  389. ->will($this->returnValue('pear'));
  390. $package
  391. ->expects($this->once())
  392. ->method('setInstallationSource')
  393. ->with('dist');
  394. $downloader = $this->createDownloaderMock();
  395. $downloader
  396. ->expects($this->once())
  397. ->method('download')
  398. ->with($package, 'target_dir');
  399. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  400. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  401. ->setMethods(array('getDownloaderForPackage'))
  402. ->getMock();
  403. $manager
  404. ->expects($this->once())
  405. ->method('getDownloaderForPackage')
  406. ->with($package)
  407. ->will($this->returnValue($downloader));
  408. $manager->setPreferSource(true);
  409. $manager->download($package, 'target_dir');
  410. }
  411. public function testSourceOnlyPackageDownloadWithSourcePreferred()
  412. {
  413. $package = $this->createPackageMock();
  414. $package
  415. ->expects($this->once())
  416. ->method('getSourceType')
  417. ->will($this->returnValue('git'));
  418. $package
  419. ->expects($this->once())
  420. ->method('getDistType')
  421. ->will($this->returnValue(null));
  422. $package
  423. ->expects($this->once())
  424. ->method('setInstallationSource')
  425. ->with('source');
  426. $downloader = $this->createDownloaderMock();
  427. $downloader
  428. ->expects($this->once())
  429. ->method('download')
  430. ->with($package, 'target_dir');
  431. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  432. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  433. ->setMethods(array('getDownloaderForPackage'))
  434. ->getMock();
  435. $manager
  436. ->expects($this->once())
  437. ->method('getDownloaderForPackage')
  438. ->with($package)
  439. ->will($this->returnValue($downloader));
  440. $manager->setPreferSource(true);
  441. $manager->download($package, 'target_dir');
  442. }
  443. public function testBadPackageDownloadWithSourcePreferred()
  444. {
  445. $package = $this->createPackageMock();
  446. $package
  447. ->expects($this->once())
  448. ->method('getSourceType')
  449. ->will($this->returnValue(null));
  450. $package
  451. ->expects($this->once())
  452. ->method('getDistType')
  453. ->will($this->returnValue(null));
  454. $manager = new DownloadManager($this->io, false, $this->filesystem);
  455. $manager->setPreferSource(true);
  456. $this->setExpectedException('InvalidArgumentException');
  457. $manager->download($package, 'target_dir');
  458. }
  459. public function testUpdateDistWithEqualTypes()
  460. {
  461. $initial = $this->createPackageMock();
  462. $initial
  463. ->expects($this->once())
  464. ->method('getInstallationSource')
  465. ->will($this->returnValue('dist'));
  466. $initial
  467. ->expects($this->once())
  468. ->method('getDistType')
  469. ->will($this->returnValue('zip'));
  470. $target = $this->createPackageMock();
  471. $target
  472. ->expects($this->once())
  473. ->method('getInstallationSource')
  474. ->will($this->returnValue('dist'));
  475. $target
  476. ->expects($this->once())
  477. ->method('getDistType')
  478. ->will($this->returnValue('zip'));
  479. $zipDownloader = $this->createDownloaderMock();
  480. $zipDownloader
  481. ->expects($this->once())
  482. ->method('update')
  483. ->with($initial, $target, 'vendor/bundles/FOS/UserBundle');
  484. $zipDownloader
  485. ->expects($this->any())
  486. ->method('getInstallationSource')
  487. ->will($this->returnValue('dist'));
  488. $manager = new DownloadManager($this->io, false, $this->filesystem);
  489. $manager->setDownloader('zip', $zipDownloader);
  490. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  491. }
  492. public function testUpdateDistWithNotEqualTypes()
  493. {
  494. $initial = $this->createPackageMock();
  495. $initial
  496. ->expects($this->once())
  497. ->method('getInstallationSource')
  498. ->will($this->returnValue('dist'));
  499. $initial
  500. ->expects($this->once())
  501. ->method('getDistType')
  502. ->will($this->returnValue('xz'));
  503. $target = $this->createPackageMock();
  504. $target
  505. ->expects($this->any())
  506. ->method('getInstallationSource')
  507. ->will($this->returnValue('dist'));
  508. $target
  509. ->expects($this->any())
  510. ->method('getDistType')
  511. ->will($this->returnValue('zip'));
  512. $xzDownloader = $this->createDownloaderMock();
  513. $xzDownloader
  514. ->expects($this->once())
  515. ->method('remove')
  516. ->with($initial, 'vendor/bundles/FOS/UserBundle');
  517. $xzDownloader
  518. ->expects($this->any())
  519. ->method('getInstallationSource')
  520. ->will($this->returnValue('dist'));
  521. $zipDownloader = $this->createDownloaderMock();
  522. $zipDownloader
  523. ->expects($this->once())
  524. ->method('install')
  525. ->with($target, 'vendor/bundles/FOS/UserBundle');
  526. $zipDownloader
  527. ->expects($this->any())
  528. ->method('getInstallationSource')
  529. ->will($this->returnValue('dist'));
  530. $manager = new DownloadManager($this->io, false, $this->filesystem);
  531. $manager->setDownloader('xz', $xzDownloader);
  532. $manager->setDownloader('zip', $zipDownloader);
  533. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  534. }
  535. /**
  536. * @dataProvider updatesProvider
  537. */
  538. public function testGetAvailableSourcesUpdateSticksToSameSource($prevPkgSource, $prevPkgIsDev, $targetAvailable, $targetIsDev, $expected)
  539. {
  540. $initial = null;
  541. if ($prevPkgSource) {
  542. $initial = $this->prophesize('Composer\Package\PackageInterface');
  543. $initial->getInstallationSource()->willReturn($prevPkgSource);
  544. $initial->isDev()->willReturn($prevPkgIsDev);
  545. }
  546. $target = $this->prophesize('Composer\Package\PackageInterface');
  547. $target->getSourceType()->willReturn(in_array('source', $targetAvailable, true) ? 'git' : null);
  548. $target->getDistType()->willReturn(in_array('dist', $targetAvailable, true) ? 'zip' : null);
  549. $target->isDev()->willReturn($targetIsDev);
  550. $manager = new DownloadManager($this->io, false, $this->filesystem);
  551. $method = new \ReflectionMethod($manager, 'getAvailableSources');
  552. $method->setAccessible(true);
  553. $this->assertEquals($expected, $method->invoke($manager, $target->reveal(), $initial ? $initial->reveal() : null));
  554. }
  555. public static function updatesProvider()
  556. {
  557. return array(
  558. // prevPkg source, prevPkg isDev, pkg available, pkg isDev, expected
  559. // updates keep previous source as preference
  560. array('source', false, array('source', 'dist'), false, array('source', 'dist')),
  561. array('dist', false, array('source', 'dist'), false, array('dist', 'source')),
  562. // updates do not keep previous source if target package does not have it
  563. array('source', false, array('dist'), false, array('dist')),
  564. array('dist', false, array('source'), false, array('source')),
  565. // updates do not keep previous source if target is dev and prev wasn't dev and installed from dist
  566. array('source', false, array('source', 'dist'), true, array('source', 'dist')),
  567. array('dist', false, array('source', 'dist'), true, array('source', 'dist')),
  568. // install picks the right default
  569. array(null, null, array('source', 'dist'), true, array('source', 'dist')),
  570. array(null, null, array('dist'), true, array('dist')),
  571. array(null, null, array('source'), true, array('source')),
  572. array(null, null, array('source', 'dist'), false, array('dist', 'source')),
  573. array(null, null, array('dist'), false, array('dist')),
  574. array(null, null, array('source'), false, array('source')),
  575. );
  576. }
  577. public function testUpdateMetapackage()
  578. {
  579. $initial = $this->createPackageMock();
  580. $target = $this->createPackageMock();
  581. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  582. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  583. ->setMethods(array('getDownloaderForPackage'))
  584. ->getMock();
  585. $manager
  586. ->expects($this->exactly(2))
  587. ->method('getDownloaderForPackage')
  588. ->with($initial)
  589. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  590. $manager->update($initial, $target, 'vendor/pkg');
  591. }
  592. public function testRemove()
  593. {
  594. $package = $this->createPackageMock();
  595. $pearDownloader = $this->createDownloaderMock();
  596. $pearDownloader
  597. ->expects($this->once())
  598. ->method('remove')
  599. ->with($package, 'vendor/bundles/FOS/UserBundle');
  600. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  601. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  602. ->setMethods(array('getDownloaderForPackage'))
  603. ->getMock();
  604. $manager
  605. ->expects($this->once())
  606. ->method('getDownloaderForPackage')
  607. ->with($package)
  608. ->will($this->returnValue($pearDownloader));
  609. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  610. }
  611. public function testMetapackageRemove()
  612. {
  613. $package = $this->createPackageMock();
  614. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  615. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  616. ->setMethods(array('getDownloaderForPackage'))
  617. ->getMock();
  618. $manager
  619. ->expects($this->once())
  620. ->method('getDownloaderForPackage')
  621. ->with($package)
  622. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  623. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  624. }
  625. /**
  626. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  627. */
  628. public function testInstallPreferenceWithoutPreferenceDev()
  629. {
  630. $package = $this->createPackageMock();
  631. $package
  632. ->expects($this->once())
  633. ->method('getSourceType')
  634. ->will($this->returnValue('git'));
  635. $package
  636. ->expects($this->once())
  637. ->method('getDistType')
  638. ->will($this->returnValue('pear'));
  639. $package
  640. ->expects($this->once())
  641. ->method('isDev')
  642. ->will($this->returnValue(true));
  643. $package
  644. ->expects($this->once())
  645. ->method('setInstallationSource')
  646. ->with('source');
  647. $downloader = $this->createDownloaderMock();
  648. $downloader
  649. ->expects($this->once())
  650. ->method('download')
  651. ->with($package, 'target_dir');
  652. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  653. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  654. ->setMethods(array('getDownloaderForPackage'))
  655. ->getMock();
  656. $manager
  657. ->expects($this->once())
  658. ->method('getDownloaderForPackage')
  659. ->with($package)
  660. ->will($this->returnValue($downloader));
  661. $manager->download($package, 'target_dir');
  662. }
  663. /**
  664. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  665. */
  666. public function testInstallPreferenceWithoutPreferenceNoDev()
  667. {
  668. $package = $this->createPackageMock();
  669. $package
  670. ->expects($this->once())
  671. ->method('getSourceType')
  672. ->will($this->returnValue('git'));
  673. $package
  674. ->expects($this->once())
  675. ->method('getDistType')
  676. ->will($this->returnValue('pear'));
  677. $package
  678. ->expects($this->once())
  679. ->method('isDev')
  680. ->will($this->returnValue(false));
  681. $package
  682. ->expects($this->once())
  683. ->method('setInstallationSource')
  684. ->with('dist');
  685. $downloader = $this->createDownloaderMock();
  686. $downloader
  687. ->expects($this->once())
  688. ->method('download')
  689. ->with($package, 'target_dir');
  690. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  691. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  692. ->setMethods(array('getDownloaderForPackage'))
  693. ->getMock();
  694. $manager
  695. ->expects($this->once())
  696. ->method('getDownloaderForPackage')
  697. ->with($package)
  698. ->will($this->returnValue($downloader));
  699. $manager->download($package, 'target_dir');
  700. }
  701. /**
  702. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  703. */
  704. public function testInstallPreferenceWithoutMatchDev()
  705. {
  706. $package = $this->createPackageMock();
  707. $package
  708. ->expects($this->once())
  709. ->method('getSourceType')
  710. ->will($this->returnValue('git'));
  711. $package
  712. ->expects($this->once())
  713. ->method('getDistType')
  714. ->will($this->returnValue('pear'));
  715. $package
  716. ->expects($this->once())
  717. ->method('isDev')
  718. ->will($this->returnValue(true));
  719. $package
  720. ->expects($this->once())
  721. ->method('getName')
  722. ->will($this->returnValue('bar/package'));
  723. $package
  724. ->expects($this->once())
  725. ->method('setInstallationSource')
  726. ->with('source');
  727. $downloader = $this->createDownloaderMock();
  728. $downloader
  729. ->expects($this->once())
  730. ->method('download')
  731. ->with($package, 'target_dir');
  732. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  733. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  734. ->setMethods(array('getDownloaderForPackage'))
  735. ->getMock();
  736. $manager
  737. ->expects($this->once())
  738. ->method('getDownloaderForPackage')
  739. ->with($package)
  740. ->will($this->returnValue($downloader));
  741. $manager->setPreferences(array('foo/*' => 'source'));
  742. $manager->download($package, 'target_dir');
  743. }
  744. /**
  745. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  746. */
  747. public function testInstallPreferenceWithoutMatchNoDev()
  748. {
  749. $package = $this->createPackageMock();
  750. $package
  751. ->expects($this->once())
  752. ->method('getSourceType')
  753. ->will($this->returnValue('git'));
  754. $package
  755. ->expects($this->once())
  756. ->method('getDistType')
  757. ->will($this->returnValue('pear'));
  758. $package
  759. ->expects($this->once())
  760. ->method('isDev')
  761. ->will($this->returnValue(false));
  762. $package
  763. ->expects($this->once())
  764. ->method('getName')
  765. ->will($this->returnValue('bar/package'));
  766. $package
  767. ->expects($this->once())
  768. ->method('setInstallationSource')
  769. ->with('dist');
  770. $downloader = $this->createDownloaderMock();
  771. $downloader
  772. ->expects($this->once())
  773. ->method('download')
  774. ->with($package, 'target_dir');
  775. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  776. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  777. ->setMethods(array('getDownloaderForPackage'))
  778. ->getMock();
  779. $manager
  780. ->expects($this->once())
  781. ->method('getDownloaderForPackage')
  782. ->with($package)
  783. ->will($this->returnValue($downloader));
  784. $manager->setPreferences(array('foo/*' => 'source'));
  785. $manager->download($package, 'target_dir');
  786. }
  787. /**
  788. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  789. */
  790. public function testInstallPreferenceWithMatchAutoDev()
  791. {
  792. $package = $this->createPackageMock();
  793. $package
  794. ->expects($this->once())
  795. ->method('getSourceType')
  796. ->will($this->returnValue('git'));
  797. $package
  798. ->expects($this->once())
  799. ->method('getDistType')
  800. ->will($this->returnValue('pear'));
  801. $package
  802. ->expects($this->once())
  803. ->method('isDev')
  804. ->will($this->returnValue(true));
  805. $package
  806. ->expects($this->once())
  807. ->method('getName')
  808. ->will($this->returnValue('foo/package'));
  809. $package
  810. ->expects($this->once())
  811. ->method('setInstallationSource')
  812. ->with('source');
  813. $downloader = $this->createDownloaderMock();
  814. $downloader
  815. ->expects($this->once())
  816. ->method('download')
  817. ->with($package, 'target_dir');
  818. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  819. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  820. ->setMethods(array('getDownloaderForPackage'))
  821. ->getMock();
  822. $manager
  823. ->expects($this->once())
  824. ->method('getDownloaderForPackage')
  825. ->with($package)
  826. ->will($this->returnValue($downloader));
  827. $manager->setPreferences(array('foo/*' => 'auto'));
  828. $manager->download($package, 'target_dir');
  829. }
  830. /**
  831. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  832. */
  833. public function testInstallPreferenceWithMatchAutoNoDev()
  834. {
  835. $package = $this->createPackageMock();
  836. $package
  837. ->expects($this->once())
  838. ->method('getSourceType')
  839. ->will($this->returnValue('git'));
  840. $package
  841. ->expects($this->once())
  842. ->method('getDistType')
  843. ->will($this->returnValue('pear'));
  844. $package
  845. ->expects($this->once())
  846. ->method('isDev')
  847. ->will($this->returnValue(false));
  848. $package
  849. ->expects($this->once())
  850. ->method('getName')
  851. ->will($this->returnValue('foo/package'));
  852. $package
  853. ->expects($this->once())
  854. ->method('setInstallationSource')
  855. ->with('dist');
  856. $downloader = $this->createDownloaderMock();
  857. $downloader
  858. ->expects($this->once())
  859. ->method('download')
  860. ->with($package, 'target_dir');
  861. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  862. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  863. ->setMethods(array('getDownloaderForPackage'))
  864. ->getMock();
  865. $manager
  866. ->expects($this->once())
  867. ->method('getDownloaderForPackage')
  868. ->with($package)
  869. ->will($this->returnValue($downloader));
  870. $manager->setPreferences(array('foo/*' => 'auto'));
  871. $manager->download($package, 'target_dir');
  872. }
  873. /**
  874. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  875. */
  876. public function testInstallPreferenceWithMatchSource()
  877. {
  878. $package = $this->createPackageMock();
  879. $package
  880. ->expects($this->once())
  881. ->method('getSourceType')
  882. ->will($this->returnValue('git'));
  883. $package
  884. ->expects($this->once())
  885. ->method('getDistType')
  886. ->will($this->returnValue('pear'));
  887. $package
  888. ->expects($this->once())
  889. ->method('getName')
  890. ->will($this->returnValue('foo/package'));
  891. $package
  892. ->expects($this->once())
  893. ->method('setInstallationSource')
  894. ->with('source');
  895. $downloader = $this->createDownloaderMock();
  896. $downloader
  897. ->expects($this->once())
  898. ->method('download')
  899. ->with($package, 'target_dir');
  900. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  901. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  902. ->setMethods(array('getDownloaderForPackage'))
  903. ->getMock();
  904. $manager
  905. ->expects($this->once())
  906. ->method('getDownloaderForPackage')
  907. ->with($package)
  908. ->will($this->returnValue($downloader));
  909. $manager->setPreferences(array('foo/*' => 'source'));
  910. $manager->download($package, 'target_dir');
  911. }
  912. /**
  913. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  914. */
  915. public function testInstallPreferenceWithMatchDist()
  916. {
  917. $package = $this->createPackageMock();
  918. $package
  919. ->expects($this->once())
  920. ->method('getSourceType')
  921. ->will($this->returnValue('git'));
  922. $package
  923. ->expects($this->once())
  924. ->method('getDistType')
  925. ->will($this->returnValue('pear'));
  926. $package
  927. ->expects($this->once())
  928. ->method('getName')
  929. ->will($this->returnValue('foo/package'));
  930. $package
  931. ->expects($this->once())
  932. ->method('setInstallationSource')
  933. ->with('dist');
  934. $downloader = $this->createDownloaderMock();
  935. $downloader
  936. ->expects($this->once())
  937. ->method('download')
  938. ->with($package, 'target_dir');
  939. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  940. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  941. ->setMethods(array('getDownloaderForPackage'))
  942. ->getMock();
  943. $manager
  944. ->expects($this->once())
  945. ->method('getDownloaderForPackage')
  946. ->with($package)
  947. ->will($this->returnValue($downloader));
  948. $manager->setPreferences(array('foo/*' => 'dist'));
  949. $manager->download($package, 'target_dir');
  950. }
  951. private function createDownloaderMock()
  952. {
  953. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  954. ->getMock();
  955. }
  956. private function createPackageMock()
  957. {
  958. return $this->getMockBuilder('Composer\Package\PackageInterface')
  959. ->getMock();
  960. }
  961. }