DownloadManagerTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. class DownloadManagerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $filesystem;
  16. public function setUp()
  17. {
  18. $this->filesystem = $this->getMock('Composer\Util\Filesystem');
  19. }
  20. public function testSetGetDownloader()
  21. {
  22. $downloader = $this->createDownloaderMock();
  23. $manager = new DownloadManager(false, $this->filesystem);
  24. $manager->setDownloader('test', $downloader);
  25. $this->assertSame($downloader, $manager->getDownloader('test'));
  26. $this->setExpectedException('InvalidArgumentException');
  27. $manager->getDownloader('unregistered');
  28. }
  29. public function testGetDownloaderForIncorrectlyInstalledPackage()
  30. {
  31. $package = $this->createPackageMock();
  32. $package
  33. ->expects($this->once())
  34. ->method('getInstallationSource')
  35. ->will($this->returnValue(null));
  36. $manager = new DownloadManager(false, $this->filesystem);
  37. $this->setExpectedException('InvalidArgumentException');
  38. $manager->getDownloaderForInstalledPackage($package);
  39. }
  40. public function testGetDownloaderForCorrectlyInstalledDistPackage()
  41. {
  42. $package = $this->createPackageMock();
  43. $package
  44. ->expects($this->once())
  45. ->method('getInstallationSource')
  46. ->will($this->returnValue('dist'));
  47. $package
  48. ->expects($this->once())
  49. ->method('getDistType')
  50. ->will($this->returnValue('pear'));
  51. $downloader = $this->createDownloaderMock();
  52. $downloader
  53. ->expects($this->once())
  54. ->method('getInstallationSource')
  55. ->will($this->returnValue('dist'));
  56. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  57. ->setConstructorArgs(array(false, $this->filesystem))
  58. ->setMethods(array('getDownloader'))
  59. ->getMock();
  60. $manager
  61. ->expects($this->once())
  62. ->method('getDownloader')
  63. ->with('pear')
  64. ->will($this->returnValue($downloader));
  65. $this->assertSame($downloader, $manager->getDownloaderForInstalledPackage($package));
  66. }
  67. public function testGetDownloaderForIncorrectlyInstalledDistPackage()
  68. {
  69. $package = $this->createPackageMock();
  70. $package
  71. ->expects($this->once())
  72. ->method('getInstallationSource')
  73. ->will($this->returnValue('dist'));
  74. $package
  75. ->expects($this->once())
  76. ->method('getDistType')
  77. ->will($this->returnValue('git'));
  78. $downloader = $this->createDownloaderMock();
  79. $downloader
  80. ->expects($this->exactly(2))
  81. ->method('getInstallationSource')
  82. ->will($this->returnValue('source'));
  83. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  84. ->setConstructorArgs(array(false, $this->filesystem))
  85. ->setMethods(array('getDownloader'))
  86. ->getMock();
  87. $manager
  88. ->expects($this->once())
  89. ->method('getDownloader')
  90. ->with('git')
  91. ->will($this->returnValue($downloader));
  92. $this->setExpectedException('LogicException');
  93. $manager->getDownloaderForInstalledPackage($package);
  94. }
  95. public function testGetDownloaderForCorrectlyInstalledSourcePackage()
  96. {
  97. $package = $this->createPackageMock();
  98. $package
  99. ->expects($this->once())
  100. ->method('getInstallationSource')
  101. ->will($this->returnValue('source'));
  102. $package
  103. ->expects($this->once())
  104. ->method('getSourceType')
  105. ->will($this->returnValue('git'));
  106. $downloader = $this->createDownloaderMock();
  107. $downloader
  108. ->expects($this->once())
  109. ->method('getInstallationSource')
  110. ->will($this->returnValue('source'));
  111. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  112. ->setConstructorArgs(array(false, $this->filesystem))
  113. ->setMethods(array('getDownloader'))
  114. ->getMock();
  115. $manager
  116. ->expects($this->once())
  117. ->method('getDownloader')
  118. ->with('git')
  119. ->will($this->returnValue($downloader));
  120. $this->assertSame($downloader, $manager->getDownloaderForInstalledPackage($package));
  121. }
  122. public function testGetDownloaderForIncorrectlyInstalledSourcePackage()
  123. {
  124. $package = $this->createPackageMock();
  125. $package
  126. ->expects($this->once())
  127. ->method('getInstallationSource')
  128. ->will($this->returnValue('source'));
  129. $package
  130. ->expects($this->once())
  131. ->method('getSourceType')
  132. ->will($this->returnValue('pear'));
  133. $downloader = $this->createDownloaderMock();
  134. $downloader
  135. ->expects($this->exactly(2))
  136. ->method('getInstallationSource')
  137. ->will($this->returnValue('dist'));
  138. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  139. ->setConstructorArgs(array(false, $this->filesystem))
  140. ->setMethods(array('getDownloader'))
  141. ->getMock();
  142. $manager
  143. ->expects($this->once())
  144. ->method('getDownloader')
  145. ->with('pear')
  146. ->will($this->returnValue($downloader));
  147. $this->setExpectedException('LogicException');
  148. $manager->getDownloaderForInstalledPackage($package);
  149. }
  150. public function testGetDownloaderForMetapackage()
  151. {
  152. $package = $this->createPackageMock();
  153. $package
  154. ->expects($this->once())
  155. ->method('getType')
  156. ->will($this->returnValue('metapackage'));
  157. $manager = new DownloadManager(false, $this->filesystem);
  158. $this->assertNull($manager->getDownloaderForInstalledPackage($package));
  159. }
  160. public function testFullPackageDownload()
  161. {
  162. $package = $this->createPackageMock();
  163. $package
  164. ->expects($this->once())
  165. ->method('getSourceType')
  166. ->will($this->returnValue('git'));
  167. $package
  168. ->expects($this->once())
  169. ->method('getDistType')
  170. ->will($this->returnValue('pear'));
  171. $package
  172. ->expects($this->once())
  173. ->method('setInstallationSource')
  174. ->with('dist');
  175. $downloader = $this->createDownloaderMock();
  176. $downloader
  177. ->expects($this->once())
  178. ->method('download')
  179. ->with($package, 'target_dir');
  180. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  181. ->setConstructorArgs(array(false, $this->filesystem))
  182. ->setMethods(array('getDownloaderForInstalledPackage'))
  183. ->getMock();
  184. $manager
  185. ->expects($this->once())
  186. ->method('getDownloaderForInstalledPackage')
  187. ->with($package)
  188. ->will($this->returnValue($downloader));
  189. $manager->download($package, 'target_dir');
  190. }
  191. public function testBadPackageDownload()
  192. {
  193. $package = $this->createPackageMock();
  194. $package
  195. ->expects($this->once())
  196. ->method('getSourceType')
  197. ->will($this->returnValue(null));
  198. $package
  199. ->expects($this->once())
  200. ->method('getDistType')
  201. ->will($this->returnValue(null));
  202. $manager = new DownloadManager(false, $this->filesystem);
  203. $this->setExpectedException('InvalidArgumentException');
  204. $manager->download($package, 'target_dir');
  205. }
  206. public function testDistOnlyPackageDownload()
  207. {
  208. $package = $this->createPackageMock();
  209. $package
  210. ->expects($this->once())
  211. ->method('getSourceType')
  212. ->will($this->returnValue(null));
  213. $package
  214. ->expects($this->once())
  215. ->method('getDistType')
  216. ->will($this->returnValue('pear'));
  217. $package
  218. ->expects($this->once())
  219. ->method('setInstallationSource')
  220. ->with('dist');
  221. $downloader = $this->createDownloaderMock();
  222. $downloader
  223. ->expects($this->once())
  224. ->method('download')
  225. ->with($package, 'target_dir');
  226. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  227. ->setConstructorArgs(array(false, $this->filesystem))
  228. ->setMethods(array('getDownloaderForInstalledPackage'))
  229. ->getMock();
  230. $manager
  231. ->expects($this->once())
  232. ->method('getDownloaderForInstalledPackage')
  233. ->with($package)
  234. ->will($this->returnValue($downloader));
  235. $manager->download($package, 'target_dir');
  236. }
  237. public function testSourceOnlyPackageDownload()
  238. {
  239. $package = $this->createPackageMock();
  240. $package
  241. ->expects($this->once())
  242. ->method('getSourceType')
  243. ->will($this->returnValue('git'));
  244. $package
  245. ->expects($this->once())
  246. ->method('getDistType')
  247. ->will($this->returnValue(null));
  248. $package
  249. ->expects($this->once())
  250. ->method('setInstallationSource')
  251. ->with('source');
  252. $downloader = $this->createDownloaderMock();
  253. $downloader
  254. ->expects($this->once())
  255. ->method('download')
  256. ->with($package, 'target_dir');
  257. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  258. ->setConstructorArgs(array(false, $this->filesystem))
  259. ->setMethods(array('getDownloaderForInstalledPackage'))
  260. ->getMock();
  261. $manager
  262. ->expects($this->once())
  263. ->method('getDownloaderForInstalledPackage')
  264. ->with($package)
  265. ->will($this->returnValue($downloader));
  266. $manager->download($package, 'target_dir');
  267. }
  268. public function testMetapackagePackageDownload()
  269. {
  270. $package = $this->createPackageMock();
  271. $package
  272. ->expects($this->once())
  273. ->method('getSourceType')
  274. ->will($this->returnValue('git'));
  275. $package
  276. ->expects($this->once())
  277. ->method('getDistType')
  278. ->will($this->returnValue(null));
  279. $package
  280. ->expects($this->once())
  281. ->method('setInstallationSource')
  282. ->with('source');
  283. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  284. ->setConstructorArgs(array(false, $this->filesystem))
  285. ->setMethods(array('getDownloaderForInstalledPackage'))
  286. ->getMock();
  287. $manager
  288. ->expects($this->once())
  289. ->method('getDownloaderForInstalledPackage')
  290. ->with($package)
  291. ->will($this->returnValue(null)); // There is no downloader for Metapackages.
  292. $manager->download($package, 'target_dir');
  293. }
  294. public function testFullPackageDownloadWithSourcePreferred()
  295. {
  296. $package = $this->createPackageMock();
  297. $package
  298. ->expects($this->once())
  299. ->method('getSourceType')
  300. ->will($this->returnValue('git'));
  301. $package
  302. ->expects($this->once())
  303. ->method('getDistType')
  304. ->will($this->returnValue('pear'));
  305. $package
  306. ->expects($this->once())
  307. ->method('setInstallationSource')
  308. ->with('source');
  309. $downloader = $this->createDownloaderMock();
  310. $downloader
  311. ->expects($this->once())
  312. ->method('download')
  313. ->with($package, 'target_dir');
  314. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  315. ->setConstructorArgs(array(false, $this->filesystem))
  316. ->setMethods(array('getDownloaderForInstalledPackage'))
  317. ->getMock();
  318. $manager
  319. ->expects($this->once())
  320. ->method('getDownloaderForInstalledPackage')
  321. ->with($package)
  322. ->will($this->returnValue($downloader));
  323. $manager->setPreferSource(true);
  324. $manager->download($package, 'target_dir');
  325. }
  326. public function testDistOnlyPackageDownloadWithSourcePreferred()
  327. {
  328. $package = $this->createPackageMock();
  329. $package
  330. ->expects($this->once())
  331. ->method('getSourceType')
  332. ->will($this->returnValue(null));
  333. $package
  334. ->expects($this->once())
  335. ->method('getDistType')
  336. ->will($this->returnValue('pear'));
  337. $package
  338. ->expects($this->once())
  339. ->method('setInstallationSource')
  340. ->with('dist');
  341. $downloader = $this->createDownloaderMock();
  342. $downloader
  343. ->expects($this->once())
  344. ->method('download')
  345. ->with($package, 'target_dir');
  346. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  347. ->setConstructorArgs(array(false, $this->filesystem))
  348. ->setMethods(array('getDownloaderForInstalledPackage'))
  349. ->getMock();
  350. $manager
  351. ->expects($this->once())
  352. ->method('getDownloaderForInstalledPackage')
  353. ->with($package)
  354. ->will($this->returnValue($downloader));
  355. $manager->setPreferSource(true);
  356. $manager->download($package, 'target_dir');
  357. }
  358. public function testSourceOnlyPackageDownloadWithSourcePreferred()
  359. {
  360. $package = $this->createPackageMock();
  361. $package
  362. ->expects($this->once())
  363. ->method('getSourceType')
  364. ->will($this->returnValue('git'));
  365. $package
  366. ->expects($this->once())
  367. ->method('getDistType')
  368. ->will($this->returnValue(null));
  369. $package
  370. ->expects($this->once())
  371. ->method('setInstallationSource')
  372. ->with('source');
  373. $downloader = $this->createDownloaderMock();
  374. $downloader
  375. ->expects($this->once())
  376. ->method('download')
  377. ->with($package, 'target_dir');
  378. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  379. ->setConstructorArgs(array(false, $this->filesystem))
  380. ->setMethods(array('getDownloaderForInstalledPackage'))
  381. ->getMock();
  382. $manager
  383. ->expects($this->once())
  384. ->method('getDownloaderForInstalledPackage')
  385. ->with($package)
  386. ->will($this->returnValue($downloader));
  387. $manager->setPreferSource(true);
  388. $manager->download($package, 'target_dir');
  389. }
  390. public function testBadPackageDownloadWithSourcePreferred()
  391. {
  392. $package = $this->createPackageMock();
  393. $package
  394. ->expects($this->once())
  395. ->method('getSourceType')
  396. ->will($this->returnValue(null));
  397. $package
  398. ->expects($this->once())
  399. ->method('getDistType')
  400. ->will($this->returnValue(null));
  401. $manager = new DownloadManager(false, $this->filesystem);
  402. $manager->setPreferSource(true);
  403. $this->setExpectedException('InvalidArgumentException');
  404. $manager->download($package, 'target_dir');
  405. }
  406. public function testUpdateDistWithEqualTypes()
  407. {
  408. $initial = $this->createPackageMock();
  409. $initial
  410. ->expects($this->once())
  411. ->method('getInstallationSource')
  412. ->will($this->returnValue('dist'));
  413. $initial
  414. ->expects($this->once())
  415. ->method('getDistType')
  416. ->will($this->returnValue('pear'));
  417. $target = $this->createPackageMock();
  418. $target
  419. ->expects($this->once())
  420. ->method('getDistType')
  421. ->will($this->returnValue('pear'));
  422. $target
  423. ->expects($this->once())
  424. ->method('setInstallationSource')
  425. ->with('dist');
  426. $pearDownloader = $this->createDownloaderMock();
  427. $pearDownloader
  428. ->expects($this->once())
  429. ->method('update')
  430. ->with($initial, $target, 'vendor/bundles/FOS/UserBundle');
  431. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  432. ->setConstructorArgs(array(false, $this->filesystem))
  433. ->setMethods(array('getDownloaderForInstalledPackage'))
  434. ->getMock();
  435. $manager
  436. ->expects($this->once())
  437. ->method('getDownloaderForInstalledPackage')
  438. ->with($initial)
  439. ->will($this->returnValue($pearDownloader));
  440. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  441. }
  442. public function testUpdateDistWithNotEqualTypes()
  443. {
  444. $initial = $this->createPackageMock();
  445. $initial
  446. ->expects($this->once())
  447. ->method('getInstallationSource')
  448. ->will($this->returnValue('dist'));
  449. $initial
  450. ->expects($this->once())
  451. ->method('getDistType')
  452. ->will($this->returnValue('pear'));
  453. $target = $this->createPackageMock();
  454. $target
  455. ->expects($this->once())
  456. ->method('getDistType')
  457. ->will($this->returnValue('composer'));
  458. $pearDownloader = $this->createDownloaderMock();
  459. $pearDownloader
  460. ->expects($this->once())
  461. ->method('remove')
  462. ->with($initial, 'vendor/bundles/FOS/UserBundle');
  463. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  464. ->setConstructorArgs(array(false, $this->filesystem))
  465. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  466. ->getMock();
  467. $manager
  468. ->expects($this->once())
  469. ->method('getDownloaderForInstalledPackage')
  470. ->with($initial)
  471. ->will($this->returnValue($pearDownloader));
  472. $manager
  473. ->expects($this->once())
  474. ->method('download')
  475. ->with($target, 'vendor/bundles/FOS/UserBundle', false);
  476. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  477. }
  478. public function testUpdateSourceWithEqualTypes()
  479. {
  480. $initial = $this->createPackageMock();
  481. $initial
  482. ->expects($this->once())
  483. ->method('getInstallationSource')
  484. ->will($this->returnValue('source'));
  485. $initial
  486. ->expects($this->once())
  487. ->method('getSourceType')
  488. ->will($this->returnValue('svn'));
  489. $target = $this->createPackageMock();
  490. $target
  491. ->expects($this->once())
  492. ->method('getSourceType')
  493. ->will($this->returnValue('svn'));
  494. $svnDownloader = $this->createDownloaderMock();
  495. $svnDownloader
  496. ->expects($this->once())
  497. ->method('update')
  498. ->with($initial, $target, 'vendor/pkg');
  499. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  500. ->setConstructorArgs(array(false, $this->filesystem))
  501. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  502. ->getMock();
  503. $manager
  504. ->expects($this->once())
  505. ->method('getDownloaderForInstalledPackage')
  506. ->with($initial)
  507. ->will($this->returnValue($svnDownloader));
  508. $manager->update($initial, $target, 'vendor/pkg');
  509. }
  510. public function testUpdateSourceWithNotEqualTypes()
  511. {
  512. $initial = $this->createPackageMock();
  513. $initial
  514. ->expects($this->once())
  515. ->method('getInstallationSource')
  516. ->will($this->returnValue('source'));
  517. $initial
  518. ->expects($this->once())
  519. ->method('getSourceType')
  520. ->will($this->returnValue('svn'));
  521. $target = $this->createPackageMock();
  522. $target
  523. ->expects($this->once())
  524. ->method('getSourceType')
  525. ->will($this->returnValue('git'));
  526. $svnDownloader = $this->createDownloaderMock();
  527. $svnDownloader
  528. ->expects($this->once())
  529. ->method('remove')
  530. ->with($initial, 'vendor/pkg');
  531. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  532. ->setConstructorArgs(array(false, $this->filesystem))
  533. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  534. ->getMock();
  535. $manager
  536. ->expects($this->once())
  537. ->method('getDownloaderForInstalledPackage')
  538. ->with($initial)
  539. ->will($this->returnValue($svnDownloader));
  540. $manager
  541. ->expects($this->once())
  542. ->method('download')
  543. ->with($target, 'vendor/pkg', true);
  544. $manager->update($initial, $target, 'vendor/pkg');
  545. }
  546. public function testUpdateMetapackage()
  547. {
  548. $initial = $this->createPackageMock();
  549. $target = $this->createPackageMock();
  550. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  551. ->setConstructorArgs(array(false, $this->filesystem))
  552. ->setMethods(array('getDownloaderForInstalledPackage'))
  553. ->getMock();
  554. $manager
  555. ->expects($this->once())
  556. ->method('getDownloaderForInstalledPackage')
  557. ->with($initial)
  558. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  559. $manager->update($initial, $target, 'vendor/pkg');
  560. }
  561. public function testRemove()
  562. {
  563. $package = $this->createPackageMock();
  564. $pearDownloader = $this->createDownloaderMock();
  565. $pearDownloader
  566. ->expects($this->once())
  567. ->method('remove')
  568. ->with($package, 'vendor/bundles/FOS/UserBundle');
  569. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  570. ->setConstructorArgs(array(false, $this->filesystem))
  571. ->setMethods(array('getDownloaderForInstalledPackage'))
  572. ->getMock();
  573. $manager
  574. ->expects($this->once())
  575. ->method('getDownloaderForInstalledPackage')
  576. ->with($package)
  577. ->will($this->returnValue($pearDownloader));
  578. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  579. }
  580. public function testMetapackageRemove()
  581. {
  582. $package = $this->createPackageMock();
  583. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  584. ->setConstructorArgs(array(false, $this->filesystem))
  585. ->setMethods(array('getDownloaderForInstalledPackage'))
  586. ->getMock();
  587. $manager
  588. ->expects($this->once())
  589. ->method('getDownloaderForInstalledPackage')
  590. ->with($package)
  591. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  592. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  593. }
  594. private function createDownloaderMock()
  595. {
  596. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  597. ->getMock();
  598. }
  599. private function createPackageMock()
  600. {
  601. return $this->getMockBuilder('Composer\Package\PackageInterface')
  602. ->getMock();
  603. }
  604. }