DownloadManagerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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('UnexpectedValueException');
  22. $manager->getDownloader('unregistered');
  23. }
  24. public function testFullPackageDownload()
  25. {
  26. $package = $this->createPackageMock();
  27. $package
  28. ->expects($this->once())
  29. ->method('getSourceType')
  30. ->will($this->returnValue('git'));
  31. $package
  32. ->expects($this->once())
  33. ->method('getDistType')
  34. ->will($this->returnValue('pear'));
  35. $package
  36. ->expects($this->once())
  37. ->method('getDistUrl')
  38. ->will($this->returnValue('dist_url'));
  39. $package
  40. ->expects($this->once())
  41. ->method('getDistSha1Checksum')
  42. ->will($this->returnValue('sha1'));
  43. $package
  44. ->expects($this->once())
  45. ->method('setInstallationSource')
  46. ->with('dist');
  47. $pearDownloader = $this->createDownloaderMock();
  48. $pearDownloader
  49. ->expects($this->once())
  50. ->method('download')
  51. ->with($package, 'target_dir', 'dist_url', 'sha1');
  52. $manager = new DownloadManager();
  53. $manager->setDownloader('pear', $pearDownloader);
  54. $manager->download($package, 'target_dir');
  55. }
  56. public function testBadPackageDownload()
  57. {
  58. $package = $this->createPackageMock();
  59. $package
  60. ->expects($this->once())
  61. ->method('getSourceType')
  62. ->will($this->returnValue(null));
  63. $package
  64. ->expects($this->once())
  65. ->method('getDistType')
  66. ->will($this->returnValue(null));
  67. $manager = new DownloadManager();
  68. $this->setExpectedException('InvalidArgumentException');
  69. $manager->download($package, 'target_dir');
  70. }
  71. public function testDistOnlyPackageDownload()
  72. {
  73. $package = $this->createPackageMock();
  74. $package
  75. ->expects($this->once())
  76. ->method('getSourceType')
  77. ->will($this->returnValue(null));
  78. $package
  79. ->expects($this->once())
  80. ->method('getDistType')
  81. ->will($this->returnValue('pear'));
  82. $package
  83. ->expects($this->once())
  84. ->method('getDistUrl')
  85. ->will($this->returnValue('dist_url'));
  86. $package
  87. ->expects($this->once())
  88. ->method('getDistSha1Checksum')
  89. ->will($this->returnValue('sha1'));
  90. $package
  91. ->expects($this->once())
  92. ->method('setInstallationSource')
  93. ->with('dist');
  94. $pearDownloader = $this->createDownloaderMock();
  95. $pearDownloader
  96. ->expects($this->once())
  97. ->method('download')
  98. ->with($package, 'target_dir', 'dist_url', 'sha1');
  99. $manager = new DownloadManager();
  100. $manager->setDownloader('pear', $pearDownloader);
  101. $manager->download($package, 'target_dir');
  102. }
  103. public function testSourceOnlyPackageDownload()
  104. {
  105. $package = $this->createPackageMock();
  106. $package
  107. ->expects($this->once())
  108. ->method('getSourceType')
  109. ->will($this->returnValue('git'));
  110. $package
  111. ->expects($this->once())
  112. ->method('getDistType')
  113. ->will($this->returnValue(null));
  114. $package
  115. ->expects($this->once())
  116. ->method('getSourceUrl')
  117. ->will($this->returnValue('source_url'));
  118. $package
  119. ->expects($this->once())
  120. ->method('setInstallationSource')
  121. ->with('source');
  122. $gitDownloader = $this->createDownloaderMock();
  123. $gitDownloader
  124. ->expects($this->once())
  125. ->method('download')
  126. ->with($package, 'vendor/pkg', 'source_url');
  127. $manager = new DownloadManager();
  128. $manager->setDownloader('git', $gitDownloader);
  129. $manager->download($package, 'vendor/pkg');
  130. }
  131. public function testFullPackageDownloadWithSourcePreferred()
  132. {
  133. $package = $this->createPackageMock();
  134. $package
  135. ->expects($this->once())
  136. ->method('getSourceType')
  137. ->will($this->returnValue('git'));
  138. $package
  139. ->expects($this->once())
  140. ->method('getDistType')
  141. ->will($this->returnValue('pear'));
  142. $package
  143. ->expects($this->once())
  144. ->method('getSourceUrl')
  145. ->will($this->returnValue('source_url'));
  146. $package
  147. ->expects($this->once())
  148. ->method('setInstallationSource')
  149. ->with('source');
  150. $gitDownloader = $this->createDownloaderMock();
  151. $gitDownloader
  152. ->expects($this->once())
  153. ->method('download')
  154. ->with($package, 'vendor/pkg', 'source_url');
  155. $manager = new DownloadManager();
  156. $manager->setDownloader('git', $gitDownloader);
  157. $manager->preferSource();
  158. $manager->download($package, 'vendor/pkg');
  159. }
  160. public function testDistOnlyPackageDownloadWithSourcePreferred()
  161. {
  162. $package = $this->createPackageMock();
  163. $package
  164. ->expects($this->once())
  165. ->method('getSourceType')
  166. ->will($this->returnValue(null));
  167. $package
  168. ->expects($this->once())
  169. ->method('getDistType')
  170. ->will($this->returnValue('pear'));
  171. $package
  172. ->expects($this->once())
  173. ->method('getDistUrl')
  174. ->will($this->returnValue('dist_url'));
  175. $package
  176. ->expects($this->once())
  177. ->method('getDistSha1Checksum')
  178. ->will($this->returnValue('sha1'));
  179. $package
  180. ->expects($this->once())
  181. ->method('setInstallationSource')
  182. ->with('dist');
  183. $pearDownloader = $this->createDownloaderMock();
  184. $pearDownloader
  185. ->expects($this->once())
  186. ->method('download')
  187. ->with($package, 'target_dir', 'dist_url', 'sha1');
  188. $manager = new DownloadManager();
  189. $manager->setDownloader('pear', $pearDownloader);
  190. $manager->preferSource();
  191. $manager->download($package, 'target_dir');
  192. }
  193. public function testSourceOnlyPackageDownloadWithSourcePreferred()
  194. {
  195. $package = $this->createPackageMock();
  196. $package
  197. ->expects($this->once())
  198. ->method('getSourceType')
  199. ->will($this->returnValue('git'));
  200. $package
  201. ->expects($this->once())
  202. ->method('getDistType')
  203. ->will($this->returnValue(null));
  204. $package
  205. ->expects($this->once())
  206. ->method('getSourceUrl')
  207. ->will($this->returnValue('source_url'));
  208. $package
  209. ->expects($this->once())
  210. ->method('setInstallationSource')
  211. ->with('source');
  212. $gitDownloader = $this->createDownloaderMock();
  213. $gitDownloader
  214. ->expects($this->once())
  215. ->method('download')
  216. ->with($package, 'vendor/pkg', 'source_url');
  217. $manager = new DownloadManager();
  218. $manager->setDownloader('git', $gitDownloader);
  219. $manager->preferSource();
  220. $manager->download($package, 'vendor/pkg');
  221. }
  222. public function testBadPackageDownloadWithSourcePreferred()
  223. {
  224. $package = $this->createPackageMock();
  225. $package
  226. ->expects($this->once())
  227. ->method('getSourceType')
  228. ->will($this->returnValue(null));
  229. $package
  230. ->expects($this->once())
  231. ->method('getDistType')
  232. ->will($this->returnValue(null));
  233. $manager = new DownloadManager();
  234. $manager->preferSource();
  235. $this->setExpectedException('InvalidArgumentException');
  236. $manager->download($package, 'target_dir');
  237. }
  238. public function testUpdateDistWithEqualTypes()
  239. {
  240. $initial = $this->createPackageMock();
  241. $initial
  242. ->expects($this->once())
  243. ->method('getInstallationSource')
  244. ->will($this->returnValue('dist'));
  245. $initial
  246. ->expects($this->once())
  247. ->method('getDistType')
  248. ->will($this->returnValue('pear'));
  249. $target = $this->createPackageMock();
  250. $target
  251. ->expects($this->once())
  252. ->method('getDistType')
  253. ->will($this->returnValue('pear'));
  254. $pearDownloader = $this->createDownloaderMock();
  255. $pearDownloader
  256. ->expects($this->once())
  257. ->method('update')
  258. ->with($initial, $target, 'vendor/bundles/FOS/UserBundle');
  259. $manager = new DownloadManager();
  260. $manager->setDownloader('pear', $pearDownloader);
  261. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  262. }
  263. public function testUpdateDistWithNotEqualTypes()
  264. {
  265. $initial = $this->createPackageMock();
  266. $initial
  267. ->expects($this->once())
  268. ->method('getInstallationSource')
  269. ->will($this->returnValue('dist'));
  270. $initial
  271. ->expects($this->once())
  272. ->method('getDistType')
  273. ->will($this->returnValue('pear'));
  274. $target = $this->createPackageMock();
  275. $target
  276. ->expects($this->once())
  277. ->method('getDistType')
  278. ->will($this->returnValue('composer'));
  279. $pearDownloader = $this->createDownloaderMock();
  280. $pearDownloader
  281. ->expects($this->once())
  282. ->method('remove')
  283. ->with($initial, 'vendor/bundles/FOS/UserBundle');
  284. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  285. ->setMethods(array('download'))
  286. ->getMock();
  287. $manager
  288. ->expects($this->once())
  289. ->method('download')
  290. ->with($target, 'vendor/bundles/FOS/UserBundle', false);
  291. $manager->setDownloader('pear', $pearDownloader);
  292. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  293. }
  294. public function testUpdateSourceWithEqualTypes()
  295. {
  296. $initial = $this->createPackageMock();
  297. $initial
  298. ->expects($this->once())
  299. ->method('getInstallationSource')
  300. ->will($this->returnValue('source'));
  301. $initial
  302. ->expects($this->once())
  303. ->method('getSourceType')
  304. ->will($this->returnValue('svn'));
  305. $target = $this->createPackageMock();
  306. $target
  307. ->expects($this->once())
  308. ->method('getSourceType')
  309. ->will($this->returnValue('svn'));
  310. $svnDownloader = $this->createDownloaderMock();
  311. $svnDownloader
  312. ->expects($this->once())
  313. ->method('update')
  314. ->with($initial, $target, 'vendor/pkg');
  315. $manager = new DownloadManager();
  316. $manager->setDownloader('svn', $svnDownloader);
  317. $manager->update($initial, $target, 'vendor/pkg');
  318. }
  319. public function testUpdateSourceWithNotEqualTypes()
  320. {
  321. $initial = $this->createPackageMock();
  322. $initial
  323. ->expects($this->once())
  324. ->method('getInstallationSource')
  325. ->will($this->returnValue('source'));
  326. $initial
  327. ->expects($this->once())
  328. ->method('getSourceType')
  329. ->will($this->returnValue('svn'));
  330. $target = $this->createPackageMock();
  331. $target
  332. ->expects($this->once())
  333. ->method('getSourceType')
  334. ->will($this->returnValue('git'));
  335. $svnDownloader = $this->createDownloaderMock();
  336. $svnDownloader
  337. ->expects($this->once())
  338. ->method('remove')
  339. ->with($initial, 'vendor/pkg');
  340. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  341. ->setMethods(array('download'))
  342. ->getMock();
  343. $manager
  344. ->expects($this->once())
  345. ->method('download')
  346. ->with($target, 'vendor/pkg', true);
  347. $manager->setDownloader('svn', $svnDownloader);
  348. $manager->update($initial, $target, 'vendor/pkg');
  349. }
  350. public function testUpdateBadlyInstalledPackage()
  351. {
  352. $initial = $this->createPackageMock();
  353. $target = $this->createPackageMock();
  354. $this->setExpectedException('InvalidArgumentException');
  355. $manager = new DownloadManager();
  356. $manager->update($initial, $target, 'vendor/pkg');
  357. }
  358. public function testRemoveDist()
  359. {
  360. $package = $this->createPackageMock();
  361. $package
  362. ->expects($this->once())
  363. ->method('getInstallationSource')
  364. ->will($this->returnValue('dist'));
  365. $package
  366. ->expects($this->once())
  367. ->method('getDistType')
  368. ->will($this->returnValue('pear'));
  369. $pearDownloader = $this->createDownloaderMock();
  370. $pearDownloader
  371. ->expects($this->once())
  372. ->method('remove')
  373. ->with($package, 'vendor/bundles/FOS/UserBundle');
  374. $manager = new DownloadManager();
  375. $manager->setDownloader('pear', $pearDownloader);
  376. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  377. }
  378. public function testRemoveSource()
  379. {
  380. $package = $this->createPackageMock();
  381. $package
  382. ->expects($this->once())
  383. ->method('getInstallationSource')
  384. ->will($this->returnValue('source'));
  385. $package
  386. ->expects($this->once())
  387. ->method('getSourceType')
  388. ->will($this->returnValue('svn'));
  389. $svnDownloader = $this->createDownloaderMock();
  390. $svnDownloader
  391. ->expects($this->once())
  392. ->method('remove')
  393. ->with($package, 'vendor/pkg');
  394. $manager = new DownloadManager();
  395. $manager->setDownloader('svn', $svnDownloader);
  396. $manager->remove($package, 'vendor/pkg');
  397. }
  398. public function testRemoveBadlyInstalledPackage()
  399. {
  400. $package = $this->createPackageMock();
  401. $manager = new DownloadManager();
  402. $this->setExpectedException('InvalidArgumentException');
  403. $manager->remove($package, 'vendor/pkg');
  404. }
  405. private function createDownloaderMock()
  406. {
  407. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  408. ->getMock();
  409. }
  410. private function createPackageMock()
  411. {
  412. return $this->getMockBuilder('Composer\Package\PackageInterface')
  413. ->getMock();
  414. }
  415. }