DownloadManagerTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 testFullPackageDownload()
  151. {
  152. $package = $this->createPackageMock();
  153. $package
  154. ->expects($this->once())
  155. ->method('getSourceType')
  156. ->will($this->returnValue('git'));
  157. $package
  158. ->expects($this->once())
  159. ->method('getDistType')
  160. ->will($this->returnValue('pear'));
  161. $package
  162. ->expects($this->once())
  163. ->method('setInstallationSource')
  164. ->with('dist');
  165. $downloader = $this->createDownloaderMock();
  166. $downloader
  167. ->expects($this->once())
  168. ->method('download')
  169. ->with($package, 'target_dir');
  170. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  171. ->setConstructorArgs(array(false, $this->filesystem))
  172. ->setMethods(array('getDownloaderForInstalledPackage'))
  173. ->getMock();
  174. $manager
  175. ->expects($this->once())
  176. ->method('getDownloaderForInstalledPackage')
  177. ->with($package)
  178. ->will($this->returnValue($downloader));
  179. $manager->download($package, 'target_dir');
  180. }
  181. public function testBadPackageDownload()
  182. {
  183. $package = $this->createPackageMock();
  184. $package
  185. ->expects($this->once())
  186. ->method('getSourceType')
  187. ->will($this->returnValue(null));
  188. $package
  189. ->expects($this->once())
  190. ->method('getDistType')
  191. ->will($this->returnValue(null));
  192. $manager = new DownloadManager(false, $this->filesystem);
  193. $this->setExpectedException('InvalidArgumentException');
  194. $manager->download($package, 'target_dir');
  195. }
  196. public function testDistOnlyPackageDownload()
  197. {
  198. $package = $this->createPackageMock();
  199. $package
  200. ->expects($this->once())
  201. ->method('getSourceType')
  202. ->will($this->returnValue(null));
  203. $package
  204. ->expects($this->once())
  205. ->method('getDistType')
  206. ->will($this->returnValue('pear'));
  207. $package
  208. ->expects($this->once())
  209. ->method('setInstallationSource')
  210. ->with('dist');
  211. $downloader = $this->createDownloaderMock();
  212. $downloader
  213. ->expects($this->once())
  214. ->method('download')
  215. ->with($package, 'target_dir');
  216. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  217. ->setConstructorArgs(array(false, $this->filesystem))
  218. ->setMethods(array('getDownloaderForInstalledPackage'))
  219. ->getMock();
  220. $manager
  221. ->expects($this->once())
  222. ->method('getDownloaderForInstalledPackage')
  223. ->with($package)
  224. ->will($this->returnValue($downloader));
  225. $manager->download($package, 'target_dir');
  226. }
  227. public function testSourceOnlyPackageDownload()
  228. {
  229. $package = $this->createPackageMock();
  230. $package
  231. ->expects($this->once())
  232. ->method('getSourceType')
  233. ->will($this->returnValue('git'));
  234. $package
  235. ->expects($this->once())
  236. ->method('getDistType')
  237. ->will($this->returnValue(null));
  238. $package
  239. ->expects($this->once())
  240. ->method('setInstallationSource')
  241. ->with('source');
  242. $downloader = $this->createDownloaderMock();
  243. $downloader
  244. ->expects($this->once())
  245. ->method('download')
  246. ->with($package, 'target_dir');
  247. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  248. ->setConstructorArgs(array(false, $this->filesystem))
  249. ->setMethods(array('getDownloaderForInstalledPackage'))
  250. ->getMock();
  251. $manager
  252. ->expects($this->once())
  253. ->method('getDownloaderForInstalledPackage')
  254. ->with($package)
  255. ->will($this->returnValue($downloader));
  256. $manager->download($package, 'target_dir');
  257. }
  258. public function testFullPackageDownloadWithSourcePreferred()
  259. {
  260. $package = $this->createPackageMock();
  261. $package
  262. ->expects($this->once())
  263. ->method('getSourceType')
  264. ->will($this->returnValue('git'));
  265. $package
  266. ->expects($this->once())
  267. ->method('getDistType')
  268. ->will($this->returnValue('pear'));
  269. $package
  270. ->expects($this->once())
  271. ->method('setInstallationSource')
  272. ->with('source');
  273. $downloader = $this->createDownloaderMock();
  274. $downloader
  275. ->expects($this->once())
  276. ->method('download')
  277. ->with($package, 'target_dir');
  278. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  279. ->setConstructorArgs(array(false, $this->filesystem))
  280. ->setMethods(array('getDownloaderForInstalledPackage'))
  281. ->getMock();
  282. $manager
  283. ->expects($this->once())
  284. ->method('getDownloaderForInstalledPackage')
  285. ->with($package)
  286. ->will($this->returnValue($downloader));
  287. $manager->setPreferSource(true);
  288. $manager->download($package, 'target_dir');
  289. }
  290. public function testDistOnlyPackageDownloadWithSourcePreferred()
  291. {
  292. $package = $this->createPackageMock();
  293. $package
  294. ->expects($this->once())
  295. ->method('getSourceType')
  296. ->will($this->returnValue(null));
  297. $package
  298. ->expects($this->once())
  299. ->method('getDistType')
  300. ->will($this->returnValue('pear'));
  301. $package
  302. ->expects($this->once())
  303. ->method('setInstallationSource')
  304. ->with('dist');
  305. $downloader = $this->createDownloaderMock();
  306. $downloader
  307. ->expects($this->once())
  308. ->method('download')
  309. ->with($package, 'target_dir');
  310. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  311. ->setConstructorArgs(array(false, $this->filesystem))
  312. ->setMethods(array('getDownloaderForInstalledPackage'))
  313. ->getMock();
  314. $manager
  315. ->expects($this->once())
  316. ->method('getDownloaderForInstalledPackage')
  317. ->with($package)
  318. ->will($this->returnValue($downloader));
  319. $manager->setPreferSource(true);
  320. $manager->download($package, 'target_dir');
  321. }
  322. public function testSourceOnlyPackageDownloadWithSourcePreferred()
  323. {
  324. $package = $this->createPackageMock();
  325. $package
  326. ->expects($this->once())
  327. ->method('getSourceType')
  328. ->will($this->returnValue('git'));
  329. $package
  330. ->expects($this->once())
  331. ->method('getDistType')
  332. ->will($this->returnValue(null));
  333. $package
  334. ->expects($this->once())
  335. ->method('setInstallationSource')
  336. ->with('source');
  337. $downloader = $this->createDownloaderMock();
  338. $downloader
  339. ->expects($this->once())
  340. ->method('download')
  341. ->with($package, 'target_dir');
  342. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  343. ->setConstructorArgs(array(false, $this->filesystem))
  344. ->setMethods(array('getDownloaderForInstalledPackage'))
  345. ->getMock();
  346. $manager
  347. ->expects($this->once())
  348. ->method('getDownloaderForInstalledPackage')
  349. ->with($package)
  350. ->will($this->returnValue($downloader));
  351. $manager->setPreferSource(true);
  352. $manager->download($package, 'target_dir');
  353. }
  354. public function testBadPackageDownloadWithSourcePreferred()
  355. {
  356. $package = $this->createPackageMock();
  357. $package
  358. ->expects($this->once())
  359. ->method('getSourceType')
  360. ->will($this->returnValue(null));
  361. $package
  362. ->expects($this->once())
  363. ->method('getDistType')
  364. ->will($this->returnValue(null));
  365. $manager = new DownloadManager(false, $this->filesystem);
  366. $manager->setPreferSource(true);
  367. $this->setExpectedException('InvalidArgumentException');
  368. $manager->download($package, 'target_dir');
  369. }
  370. public function testUpdateDistWithEqualTypes()
  371. {
  372. $initial = $this->createPackageMock();
  373. $initial
  374. ->expects($this->once())
  375. ->method('getInstallationSource')
  376. ->will($this->returnValue('dist'));
  377. $initial
  378. ->expects($this->once())
  379. ->method('getDistType')
  380. ->will($this->returnValue('pear'));
  381. $target = $this->createPackageMock();
  382. $target
  383. ->expects($this->once())
  384. ->method('getDistType')
  385. ->will($this->returnValue('pear'));
  386. $target
  387. ->expects($this->once())
  388. ->method('setInstallationSource')
  389. ->with('dist');
  390. $pearDownloader = $this->createDownloaderMock();
  391. $pearDownloader
  392. ->expects($this->once())
  393. ->method('update')
  394. ->with($initial, $target, 'vendor/bundles/FOS/UserBundle');
  395. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  396. ->setConstructorArgs(array(false, $this->filesystem))
  397. ->setMethods(array('getDownloaderForInstalledPackage'))
  398. ->getMock();
  399. $manager
  400. ->expects($this->once())
  401. ->method('getDownloaderForInstalledPackage')
  402. ->with($initial)
  403. ->will($this->returnValue($pearDownloader));
  404. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  405. }
  406. public function testUpdateDistWithNotEqualTypes()
  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('composer'));
  422. $pearDownloader = $this->createDownloaderMock();
  423. $pearDownloader
  424. ->expects($this->once())
  425. ->method('remove')
  426. ->with($initial, 'vendor/bundles/FOS/UserBundle');
  427. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  428. ->setConstructorArgs(array(false, $this->filesystem))
  429. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  430. ->getMock();
  431. $manager
  432. ->expects($this->once())
  433. ->method('getDownloaderForInstalledPackage')
  434. ->with($initial)
  435. ->will($this->returnValue($pearDownloader));
  436. $manager
  437. ->expects($this->once())
  438. ->method('download')
  439. ->with($target, 'vendor/bundles/FOS/UserBundle', false);
  440. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  441. }
  442. public function testUpdateSourceWithEqualTypes()
  443. {
  444. $initial = $this->createPackageMock();
  445. $initial
  446. ->expects($this->once())
  447. ->method('getInstallationSource')
  448. ->will($this->returnValue('source'));
  449. $initial
  450. ->expects($this->once())
  451. ->method('getSourceType')
  452. ->will($this->returnValue('svn'));
  453. $target = $this->createPackageMock();
  454. $target
  455. ->expects($this->once())
  456. ->method('getSourceType')
  457. ->will($this->returnValue('svn'));
  458. $svnDownloader = $this->createDownloaderMock();
  459. $svnDownloader
  460. ->expects($this->once())
  461. ->method('update')
  462. ->with($initial, $target, 'vendor/pkg');
  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($svnDownloader));
  472. $manager->update($initial, $target, 'vendor/pkg');
  473. }
  474. public function testUpdateSourceWithNotEqualTypes()
  475. {
  476. $initial = $this->createPackageMock();
  477. $initial
  478. ->expects($this->once())
  479. ->method('getInstallationSource')
  480. ->will($this->returnValue('source'));
  481. $initial
  482. ->expects($this->once())
  483. ->method('getSourceType')
  484. ->will($this->returnValue('svn'));
  485. $target = $this->createPackageMock();
  486. $target
  487. ->expects($this->once())
  488. ->method('getSourceType')
  489. ->will($this->returnValue('git'));
  490. $svnDownloader = $this->createDownloaderMock();
  491. $svnDownloader
  492. ->expects($this->once())
  493. ->method('remove')
  494. ->with($initial, 'vendor/pkg');
  495. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  496. ->setConstructorArgs(array(false, $this->filesystem))
  497. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  498. ->getMock();
  499. $manager
  500. ->expects($this->once())
  501. ->method('getDownloaderForInstalledPackage')
  502. ->with($initial)
  503. ->will($this->returnValue($svnDownloader));
  504. $manager
  505. ->expects($this->once())
  506. ->method('download')
  507. ->with($target, 'vendor/pkg', true);
  508. $manager->update($initial, $target, 'vendor/pkg');
  509. }
  510. public function testRemove()
  511. {
  512. $package = $this->createPackageMock();
  513. $pearDownloader = $this->createDownloaderMock();
  514. $pearDownloader
  515. ->expects($this->once())
  516. ->method('remove')
  517. ->with($package, 'vendor/bundles/FOS/UserBundle');
  518. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  519. ->setConstructorArgs(array(false, $this->filesystem))
  520. ->setMethods(array('getDownloaderForInstalledPackage'))
  521. ->getMock();
  522. $manager
  523. ->expects($this->once())
  524. ->method('getDownloaderForInstalledPackage')
  525. ->with($package)
  526. ->will($this->returnValue($pearDownloader));
  527. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  528. }
  529. private function createDownloaderMock()
  530. {
  531. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  532. ->getMock();
  533. }
  534. private function createPackageMock()
  535. {
  536. return $this->getMockBuilder('Composer\Package\PackageInterface')
  537. ->getMock();
  538. }
  539. }