DownloadManagerTest.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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->getDownloaderForInstalledPackage($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->getDownloaderForInstalledPackage($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->getDownloaderForInstalledPackage($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->getDownloaderForInstalledPackage($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->getDownloaderForInstalledPackage($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->getDownloaderForInstalledPackage($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('getDownloaderForInstalledPackage'))
  186. ->getMock();
  187. $manager
  188. ->expects($this->once())
  189. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  231. ->getMock();
  232. $manager
  233. ->expects($this->at(0))
  234. ->method('getDownloaderForInstalledPackage')
  235. ->with($package)
  236. ->will($this->returnValue($downloaderFail));
  237. $manager
  238. ->expects($this->at(1))
  239. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  282. ->getMock();
  283. $manager
  284. ->expects($this->once())
  285. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  313. ->getMock();
  314. $manager
  315. ->expects($this->once())
  316. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  339. ->getMock();
  340. $manager
  341. ->expects($this->once())
  342. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  370. ->getMock();
  371. $manager
  372. ->expects($this->once())
  373. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  402. ->getMock();
  403. $manager
  404. ->expects($this->once())
  405. ->method('getDownloaderForInstalledPackage')
  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('getDownloaderForInstalledPackage'))
  434. ->getMock();
  435. $manager
  436. ->expects($this->once())
  437. ->method('getDownloaderForInstalledPackage')
  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('pear'));
  470. $target = $this->createPackageMock();
  471. $target
  472. ->expects($this->once())
  473. ->method('getDistType')
  474. ->will($this->returnValue('pear'));
  475. $target
  476. ->expects($this->once())
  477. ->method('setInstallationSource')
  478. ->with('dist');
  479. $pearDownloader = $this->createDownloaderMock();
  480. $pearDownloader
  481. ->expects($this->once())
  482. ->method('update')
  483. ->with($initial, $target, 'vendor/bundles/FOS/UserBundle');
  484. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  485. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  486. ->setMethods(array('getDownloaderForInstalledPackage'))
  487. ->getMock();
  488. $manager
  489. ->expects($this->once())
  490. ->method('getDownloaderForInstalledPackage')
  491. ->with($initial)
  492. ->will($this->returnValue($pearDownloader));
  493. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  494. }
  495. public function testUpdateDistWithNotEqualTypes()
  496. {
  497. $initial = $this->createPackageMock();
  498. $initial
  499. ->expects($this->once())
  500. ->method('getInstallationSource')
  501. ->will($this->returnValue('dist'));
  502. $initial
  503. ->expects($this->once())
  504. ->method('getDistType')
  505. ->will($this->returnValue('pear'));
  506. $target = $this->createPackageMock();
  507. $target
  508. ->expects($this->once())
  509. ->method('getDistType')
  510. ->will($this->returnValue('composer'));
  511. $pearDownloader = $this->createDownloaderMock();
  512. $pearDownloader
  513. ->expects($this->once())
  514. ->method('remove')
  515. ->with($initial, 'vendor/bundles/FOS/UserBundle');
  516. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  517. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  518. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  519. ->getMock();
  520. $manager
  521. ->expects($this->once())
  522. ->method('getDownloaderForInstalledPackage')
  523. ->with($initial)
  524. ->will($this->returnValue($pearDownloader));
  525. $manager
  526. ->expects($this->once())
  527. ->method('download')
  528. ->with($target, 'vendor/bundles/FOS/UserBundle', false);
  529. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  530. }
  531. public function testUpdateSourceWithEqualTypes()
  532. {
  533. $initial = $this->createPackageMock();
  534. $initial
  535. ->expects($this->once())
  536. ->method('getInstallationSource')
  537. ->will($this->returnValue('source'));
  538. $initial
  539. ->expects($this->once())
  540. ->method('getSourceType')
  541. ->will($this->returnValue('svn'));
  542. $target = $this->createPackageMock();
  543. $target
  544. ->expects($this->once())
  545. ->method('getSourceType')
  546. ->will($this->returnValue('svn'));
  547. $svnDownloader = $this->createDownloaderMock();
  548. $svnDownloader
  549. ->expects($this->once())
  550. ->method('update')
  551. ->with($initial, $target, 'vendor/pkg');
  552. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  553. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  554. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  555. ->getMock();
  556. $manager
  557. ->expects($this->once())
  558. ->method('getDownloaderForInstalledPackage')
  559. ->with($initial)
  560. ->will($this->returnValue($svnDownloader));
  561. $manager->update($initial, $target, 'vendor/pkg');
  562. }
  563. public function testUpdateSourceWithNotEqualTypes()
  564. {
  565. $initial = $this->createPackageMock();
  566. $initial
  567. ->expects($this->once())
  568. ->method('getInstallationSource')
  569. ->will($this->returnValue('source'));
  570. $initial
  571. ->expects($this->once())
  572. ->method('getSourceType')
  573. ->will($this->returnValue('svn'));
  574. $target = $this->createPackageMock();
  575. $target
  576. ->expects($this->once())
  577. ->method('getSourceType')
  578. ->will($this->returnValue('git'));
  579. $svnDownloader = $this->createDownloaderMock();
  580. $svnDownloader
  581. ->expects($this->once())
  582. ->method('remove')
  583. ->with($initial, 'vendor/pkg');
  584. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  585. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  586. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  587. ->getMock();
  588. $manager
  589. ->expects($this->once())
  590. ->method('getDownloaderForInstalledPackage')
  591. ->with($initial)
  592. ->will($this->returnValue($svnDownloader));
  593. $manager
  594. ->expects($this->once())
  595. ->method('download')
  596. ->with($target, 'vendor/pkg', true);
  597. $manager->update($initial, $target, 'vendor/pkg');
  598. }
  599. public function testUpdateMetapackage()
  600. {
  601. $initial = $this->createPackageMock();
  602. $target = $this->createPackageMock();
  603. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  604. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  605. ->setMethods(array('getDownloaderForInstalledPackage'))
  606. ->getMock();
  607. $manager
  608. ->expects($this->once())
  609. ->method('getDownloaderForInstalledPackage')
  610. ->with($initial)
  611. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  612. $manager->update($initial, $target, 'vendor/pkg');
  613. }
  614. public function testRemove()
  615. {
  616. $package = $this->createPackageMock();
  617. $pearDownloader = $this->createDownloaderMock();
  618. $pearDownloader
  619. ->expects($this->once())
  620. ->method('remove')
  621. ->with($package, 'vendor/bundles/FOS/UserBundle');
  622. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  623. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  624. ->setMethods(array('getDownloaderForInstalledPackage'))
  625. ->getMock();
  626. $manager
  627. ->expects($this->once())
  628. ->method('getDownloaderForInstalledPackage')
  629. ->with($package)
  630. ->will($this->returnValue($pearDownloader));
  631. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  632. }
  633. public function testMetapackageRemove()
  634. {
  635. $package = $this->createPackageMock();
  636. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  637. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  638. ->setMethods(array('getDownloaderForInstalledPackage'))
  639. ->getMock();
  640. $manager
  641. ->expects($this->once())
  642. ->method('getDownloaderForInstalledPackage')
  643. ->with($package)
  644. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  645. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  646. }
  647. /**
  648. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  649. */
  650. public function testInstallPreferenceWithoutPreferenceDev()
  651. {
  652. $package = $this->createPackageMock();
  653. $package
  654. ->expects($this->once())
  655. ->method('getSourceType')
  656. ->will($this->returnValue('git'));
  657. $package
  658. ->expects($this->once())
  659. ->method('getDistType')
  660. ->will($this->returnValue('pear'));
  661. $package
  662. ->expects($this->once())
  663. ->method('isDev')
  664. ->will($this->returnValue(true));
  665. $package
  666. ->expects($this->once())
  667. ->method('setInstallationSource')
  668. ->with('source');
  669. $downloader = $this->createDownloaderMock();
  670. $downloader
  671. ->expects($this->once())
  672. ->method('download')
  673. ->with($package, 'target_dir');
  674. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  675. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  676. ->setMethods(array('getDownloaderForInstalledPackage'))
  677. ->getMock();
  678. $manager
  679. ->expects($this->once())
  680. ->method('getDownloaderForInstalledPackage')
  681. ->with($package)
  682. ->will($this->returnValue($downloader));
  683. $manager->download($package, 'target_dir');
  684. }
  685. /**
  686. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  687. */
  688. public function testInstallPreferenceWithoutPreferenceNoDev()
  689. {
  690. $package = $this->createPackageMock();
  691. $package
  692. ->expects($this->once())
  693. ->method('getSourceType')
  694. ->will($this->returnValue('git'));
  695. $package
  696. ->expects($this->once())
  697. ->method('getDistType')
  698. ->will($this->returnValue('pear'));
  699. $package
  700. ->expects($this->once())
  701. ->method('isDev')
  702. ->will($this->returnValue(false));
  703. $package
  704. ->expects($this->once())
  705. ->method('setInstallationSource')
  706. ->with('dist');
  707. $downloader = $this->createDownloaderMock();
  708. $downloader
  709. ->expects($this->once())
  710. ->method('download')
  711. ->with($package, 'target_dir');
  712. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  713. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  714. ->setMethods(array('getDownloaderForInstalledPackage'))
  715. ->getMock();
  716. $manager
  717. ->expects($this->once())
  718. ->method('getDownloaderForInstalledPackage')
  719. ->with($package)
  720. ->will($this->returnValue($downloader));
  721. $manager->download($package, 'target_dir');
  722. }
  723. /**
  724. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  725. */
  726. public function testInstallPreferenceWithoutMatchDev()
  727. {
  728. $package = $this->createPackageMock();
  729. $package
  730. ->expects($this->once())
  731. ->method('getSourceType')
  732. ->will($this->returnValue('git'));
  733. $package
  734. ->expects($this->once())
  735. ->method('getDistType')
  736. ->will($this->returnValue('pear'));
  737. $package
  738. ->expects($this->once())
  739. ->method('isDev')
  740. ->will($this->returnValue(true));
  741. $package
  742. ->expects($this->once())
  743. ->method('getName')
  744. ->will($this->returnValue('bar/package'));
  745. $package
  746. ->expects($this->once())
  747. ->method('setInstallationSource')
  748. ->with('source');
  749. $downloader = $this->createDownloaderMock();
  750. $downloader
  751. ->expects($this->once())
  752. ->method('download')
  753. ->with($package, 'target_dir');
  754. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  755. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  756. ->setMethods(array('getDownloaderForInstalledPackage'))
  757. ->getMock();
  758. $manager
  759. ->expects($this->once())
  760. ->method('getDownloaderForInstalledPackage')
  761. ->with($package)
  762. ->will($this->returnValue($downloader));
  763. $manager->setPreferences(array('foo/*' => 'source'));
  764. $manager->download($package, 'target_dir');
  765. }
  766. /**
  767. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  768. */
  769. public function testInstallPreferenceWithoutMatchNoDev()
  770. {
  771. $package = $this->createPackageMock();
  772. $package
  773. ->expects($this->once())
  774. ->method('getSourceType')
  775. ->will($this->returnValue('git'));
  776. $package
  777. ->expects($this->once())
  778. ->method('getDistType')
  779. ->will($this->returnValue('pear'));
  780. $package
  781. ->expects($this->once())
  782. ->method('isDev')
  783. ->will($this->returnValue(false));
  784. $package
  785. ->expects($this->once())
  786. ->method('getName')
  787. ->will($this->returnValue('bar/package'));
  788. $package
  789. ->expects($this->once())
  790. ->method('setInstallationSource')
  791. ->with('dist');
  792. $downloader = $this->createDownloaderMock();
  793. $downloader
  794. ->expects($this->once())
  795. ->method('download')
  796. ->with($package, 'target_dir');
  797. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  798. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  799. ->setMethods(array('getDownloaderForInstalledPackage'))
  800. ->getMock();
  801. $manager
  802. ->expects($this->once())
  803. ->method('getDownloaderForInstalledPackage')
  804. ->with($package)
  805. ->will($this->returnValue($downloader));
  806. $manager->setPreferences(array('foo/*' => 'source'));
  807. $manager->download($package, 'target_dir');
  808. }
  809. /**
  810. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  811. */
  812. public function testInstallPreferenceWithMatchAutoDev()
  813. {
  814. $package = $this->createPackageMock();
  815. $package
  816. ->expects($this->once())
  817. ->method('getSourceType')
  818. ->will($this->returnValue('git'));
  819. $package
  820. ->expects($this->once())
  821. ->method('getDistType')
  822. ->will($this->returnValue('pear'));
  823. $package
  824. ->expects($this->once())
  825. ->method('isDev')
  826. ->will($this->returnValue(true));
  827. $package
  828. ->expects($this->once())
  829. ->method('getName')
  830. ->will($this->returnValue('foo/package'));
  831. $package
  832. ->expects($this->once())
  833. ->method('setInstallationSource')
  834. ->with('source');
  835. $downloader = $this->createDownloaderMock();
  836. $downloader
  837. ->expects($this->once())
  838. ->method('download')
  839. ->with($package, 'target_dir');
  840. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  841. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  842. ->setMethods(array('getDownloaderForInstalledPackage'))
  843. ->getMock();
  844. $manager
  845. ->expects($this->once())
  846. ->method('getDownloaderForInstalledPackage')
  847. ->with($package)
  848. ->will($this->returnValue($downloader));
  849. $manager->setPreferences(array('foo/*' => 'auto'));
  850. $manager->download($package, 'target_dir');
  851. }
  852. /**
  853. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  854. */
  855. public function testInstallPreferenceWithMatchAutoNoDev()
  856. {
  857. $package = $this->createPackageMock();
  858. $package
  859. ->expects($this->once())
  860. ->method('getSourceType')
  861. ->will($this->returnValue('git'));
  862. $package
  863. ->expects($this->once())
  864. ->method('getDistType')
  865. ->will($this->returnValue('pear'));
  866. $package
  867. ->expects($this->once())
  868. ->method('isDev')
  869. ->will($this->returnValue(false));
  870. $package
  871. ->expects($this->once())
  872. ->method('getName')
  873. ->will($this->returnValue('foo/package'));
  874. $package
  875. ->expects($this->once())
  876. ->method('setInstallationSource')
  877. ->with('dist');
  878. $downloader = $this->createDownloaderMock();
  879. $downloader
  880. ->expects($this->once())
  881. ->method('download')
  882. ->with($package, 'target_dir');
  883. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  884. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  885. ->setMethods(array('getDownloaderForInstalledPackage'))
  886. ->getMock();
  887. $manager
  888. ->expects($this->once())
  889. ->method('getDownloaderForInstalledPackage')
  890. ->with($package)
  891. ->will($this->returnValue($downloader));
  892. $manager->setPreferences(array('foo/*' => 'auto'));
  893. $manager->download($package, 'target_dir');
  894. }
  895. /**
  896. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  897. */
  898. public function testInstallPreferenceWithMatchSource()
  899. {
  900. $package = $this->createPackageMock();
  901. $package
  902. ->expects($this->once())
  903. ->method('getSourceType')
  904. ->will($this->returnValue('git'));
  905. $package
  906. ->expects($this->once())
  907. ->method('getDistType')
  908. ->will($this->returnValue('pear'));
  909. $package
  910. ->expects($this->once())
  911. ->method('getName')
  912. ->will($this->returnValue('foo/package'));
  913. $package
  914. ->expects($this->once())
  915. ->method('setInstallationSource')
  916. ->with('source');
  917. $downloader = $this->createDownloaderMock();
  918. $downloader
  919. ->expects($this->once())
  920. ->method('download')
  921. ->with($package, 'target_dir');
  922. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  923. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  924. ->setMethods(array('getDownloaderForInstalledPackage'))
  925. ->getMock();
  926. $manager
  927. ->expects($this->once())
  928. ->method('getDownloaderForInstalledPackage')
  929. ->with($package)
  930. ->will($this->returnValue($downloader));
  931. $manager->setPreferences(array('foo/*' => 'source'));
  932. $manager->download($package, 'target_dir');
  933. }
  934. /**
  935. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  936. */
  937. public function testInstallPreferenceWithMatchDist()
  938. {
  939. $package = $this->createPackageMock();
  940. $package
  941. ->expects($this->once())
  942. ->method('getSourceType')
  943. ->will($this->returnValue('git'));
  944. $package
  945. ->expects($this->once())
  946. ->method('getDistType')
  947. ->will($this->returnValue('pear'));
  948. $package
  949. ->expects($this->once())
  950. ->method('getName')
  951. ->will($this->returnValue('foo/package'));
  952. $package
  953. ->expects($this->once())
  954. ->method('setInstallationSource')
  955. ->with('dist');
  956. $downloader = $this->createDownloaderMock();
  957. $downloader
  958. ->expects($this->once())
  959. ->method('download')
  960. ->with($package, 'target_dir');
  961. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  962. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  963. ->setMethods(array('getDownloaderForInstalledPackage'))
  964. ->getMock();
  965. $manager
  966. ->expects($this->once())
  967. ->method('getDownloaderForInstalledPackage')
  968. ->with($package)
  969. ->will($this->returnValue($downloader));
  970. $manager->setPreferences(array('foo/*' => 'dist'));
  971. $manager->download($package, 'target_dir');
  972. }
  973. private function createDownloaderMock()
  974. {
  975. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  976. ->getMock();
  977. }
  978. private function createPackageMock()
  979. {
  980. return $this->getMockBuilder('Composer\Package\PackageInterface')
  981. ->getMock();
  982. }
  983. }