DownloadManagerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. $pearDownloader = $this->createDownloaderMock();
  44. $pearDownloader
  45. ->expects($this->once())
  46. ->method('download')
  47. ->with($package, 'target_dir', 'dist_url', 'sha1');
  48. $manager = new DownloadManager();
  49. $manager->setDownloader('pear', $pearDownloader);
  50. $manager->download($package, 'target_dir');
  51. }
  52. public function testBadPackageDownload()
  53. {
  54. $package = $this->createPackageMock();
  55. $package
  56. ->expects($this->once())
  57. ->method('getSourceType')
  58. ->will($this->returnValue(null));
  59. $package
  60. ->expects($this->once())
  61. ->method('getDistType')
  62. ->will($this->returnValue(null));
  63. $manager = new DownloadManager();
  64. $this->setExpectedException('InvalidArgumentException');
  65. $manager->download($package, 'target_dir');
  66. }
  67. public function testDistOnlyPackageDownload()
  68. {
  69. $package = $this->createPackageMock();
  70. $package
  71. ->expects($this->once())
  72. ->method('getSourceType')
  73. ->will($this->returnValue(null));
  74. $package
  75. ->expects($this->once())
  76. ->method('getDistType')
  77. ->will($this->returnValue('pear'));
  78. $package
  79. ->expects($this->once())
  80. ->method('getDistUrl')
  81. ->will($this->returnValue('dist_url'));
  82. $package
  83. ->expects($this->once())
  84. ->method('getDistSha1Checksum')
  85. ->will($this->returnValue('sha1'));
  86. $pearDownloader = $this->createDownloaderMock();
  87. $pearDownloader
  88. ->expects($this->once())
  89. ->method('download')
  90. ->with($package, 'target_dir', 'dist_url', 'sha1');
  91. $manager = new DownloadManager();
  92. $manager->setDownloader('pear', $pearDownloader);
  93. $manager->download($package, 'target_dir');
  94. }
  95. public function testSourceOnlyPackageDownload()
  96. {
  97. $package = $this->createPackageMock();
  98. $package
  99. ->expects($this->once())
  100. ->method('getSourceType')
  101. ->will($this->returnValue('git'));
  102. $package
  103. ->expects($this->once())
  104. ->method('getDistType')
  105. ->will($this->returnValue(null));
  106. $package
  107. ->expects($this->once())
  108. ->method('getSourceUrl')
  109. ->will($this->returnValue('source_url'));
  110. $gitDownloader = $this->createDownloaderMock();
  111. $gitDownloader
  112. ->expects($this->once())
  113. ->method('download')
  114. ->with($package, 'vendor/pkg', 'source_url');
  115. $manager = new DownloadManager();
  116. $manager->setDownloader('git', $gitDownloader);
  117. $manager->download($package, 'vendor/pkg');
  118. }
  119. public function testFullPackageDownloadWithSourcePreferred()
  120. {
  121. $package = $this->createPackageMock();
  122. $package
  123. ->expects($this->once())
  124. ->method('getSourceType')
  125. ->will($this->returnValue('git'));
  126. $package
  127. ->expects($this->once())
  128. ->method('getDistType')
  129. ->will($this->returnValue('pear'));
  130. $package
  131. ->expects($this->once())
  132. ->method('getSourceUrl')
  133. ->will($this->returnValue('source_url'));
  134. $gitDownloader = $this->createDownloaderMock();
  135. $gitDownloader
  136. ->expects($this->once())
  137. ->method('download')
  138. ->with($package, 'vendor/pkg', 'source_url');
  139. $manager = new DownloadManager();
  140. $manager->setDownloader('git', $gitDownloader);
  141. $manager->preferSource();
  142. $manager->download($package, 'vendor/pkg');
  143. }
  144. public function testDistOnlyPackageDownloadWithSourcePreferred()
  145. {
  146. $package = $this->createPackageMock();
  147. $package
  148. ->expects($this->once())
  149. ->method('getSourceType')
  150. ->will($this->returnValue(null));
  151. $package
  152. ->expects($this->once())
  153. ->method('getDistType')
  154. ->will($this->returnValue('pear'));
  155. $package
  156. ->expects($this->once())
  157. ->method('getDistUrl')
  158. ->will($this->returnValue('dist_url'));
  159. $package
  160. ->expects($this->once())
  161. ->method('getDistSha1Checksum')
  162. ->will($this->returnValue('sha1'));
  163. $pearDownloader = $this->createDownloaderMock();
  164. $pearDownloader
  165. ->expects($this->once())
  166. ->method('download')
  167. ->with($package, 'target_dir', 'dist_url', 'sha1');
  168. $manager = new DownloadManager();
  169. $manager->setDownloader('pear', $pearDownloader);
  170. $manager->preferSource();
  171. $manager->download($package, 'target_dir');
  172. }
  173. public function testSourceOnlyPackageDownloadWithSourcePreferred()
  174. {
  175. $package = $this->createPackageMock();
  176. $package
  177. ->expects($this->once())
  178. ->method('getSourceType')
  179. ->will($this->returnValue('git'));
  180. $package
  181. ->expects($this->once())
  182. ->method('getDistType')
  183. ->will($this->returnValue(null));
  184. $package
  185. ->expects($this->once())
  186. ->method('getSourceUrl')
  187. ->will($this->returnValue('source_url'));
  188. $gitDownloader = $this->createDownloaderMock();
  189. $gitDownloader
  190. ->expects($this->once())
  191. ->method('download')
  192. ->with($package, 'vendor/pkg', 'source_url');
  193. $manager = new DownloadManager();
  194. $manager->setDownloader('git', $gitDownloader);
  195. $manager->preferSource();
  196. $manager->download($package, 'vendor/pkg');
  197. }
  198. public function testBadPackageDownloadWithSourcePreferred()
  199. {
  200. $package = $this->createPackageMock();
  201. $package
  202. ->expects($this->once())
  203. ->method('getSourceType')
  204. ->will($this->returnValue(null));
  205. $package
  206. ->expects($this->once())
  207. ->method('getDistType')
  208. ->will($this->returnValue(null));
  209. $manager = new DownloadManager();
  210. $manager->preferSource();
  211. $this->setExpectedException('InvalidArgumentException');
  212. $manager->download($package, 'target_dir');
  213. }
  214. public function testUpdateDist()
  215. {
  216. $initial = $this->createPackageMock();
  217. $initial
  218. ->expects($this->once())
  219. ->method('getDistType')
  220. ->will($this->returnValue('pear'));
  221. $target = $this->createPackageMock();
  222. $pearDownloader = $this->createDownloaderMock();
  223. $pearDownloader
  224. ->expects($this->once())
  225. ->method('update')
  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', 'dist');
  230. }
  231. public function testUpdateSource()
  232. {
  233. $initial = $this->createPackageMock();
  234. $initial
  235. ->expects($this->once())
  236. ->method('getSourceType')
  237. ->will($this->returnValue('svn'));
  238. $target = $this->createPackageMock();
  239. $svnDownloader = $this->createDownloaderMock();
  240. $svnDownloader
  241. ->expects($this->once())
  242. ->method('update')
  243. ->with($initial, $target, 'vendor/pkg');
  244. $manager = new DownloadManager();
  245. $manager->setDownloader('svn', $svnDownloader);
  246. $manager->update($initial, $target, 'vendor/pkg', 'source');
  247. }
  248. public function testRemoveDist()
  249. {
  250. $package = $this->createPackageMock();
  251. $package
  252. ->expects($this->once())
  253. ->method('getDistType')
  254. ->will($this->returnValue('pear'));
  255. $pearDownloader = $this->createDownloaderMock();
  256. $pearDownloader
  257. ->expects($this->once())
  258. ->method('remove')
  259. ->with($package, 'vendor/bundles/FOS/UserBundle');
  260. $manager = new DownloadManager();
  261. $manager->setDownloader('pear', $pearDownloader);
  262. $manager->remove($package, 'vendor/bundles/FOS/UserBundle', 'dist');
  263. }
  264. public function testRemoveSource()
  265. {
  266. $package = $this->createPackageMock();
  267. $package
  268. ->expects($this->once())
  269. ->method('getSourceType')
  270. ->will($this->returnValue('svn'));
  271. $svnDownloader = $this->createDownloaderMock();
  272. $svnDownloader
  273. ->expects($this->once())
  274. ->method('remove')
  275. ->with($package, 'vendor/pkg');
  276. $manager = new DownloadManager();
  277. $manager->setDownloader('svn', $svnDownloader);
  278. $manager->remove($package, 'vendor/pkg', 'source');
  279. }
  280. private function createDownloaderMock()
  281. {
  282. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  283. ->getMock();
  284. }
  285. private function createPackageMock()
  286. {
  287. return $this->getMockBuilder('Composer\Package\PackageInterface')
  288. ->getMock();
  289. }
  290. }