DownloadManagerTest.php 20 KB

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