DownloadManagerTest.php 14 KB

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