DownloadManagerTest.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. protected $io;
  17. public function setUp()
  18. {
  19. $this->filesystem = $this->getMock('Composer\Util\Filesystem');
  20. $this->io = $this->getMock('Composer\IO\IOInterface');
  21. }
  22. public function testSetGetDownloader()
  23. {
  24. $downloader = $this->createDownloaderMock();
  25. $manager = new DownloadManager($this->io, false, $this->filesystem);
  26. $manager->setDownloader('test', $downloader);
  27. $this->assertSame($downloader, $manager->getDownloader('test'));
  28. $this->setExpectedException('InvalidArgumentException');
  29. $manager->getDownloader('unregistered');
  30. }
  31. public function testGetDownloaderForIncorrectlyInstalledPackage()
  32. {
  33. $package = $this->createPackageMock();
  34. $package
  35. ->expects($this->once())
  36. ->method('getInstallationSource')
  37. ->will($this->returnValue(null));
  38. $manager = new DownloadManager($this->io, false, $this->filesystem);
  39. $this->setExpectedException('InvalidArgumentException');
  40. $manager->getDownloaderForInstalledPackage($package);
  41. }
  42. public function testGetDownloaderForCorrectlyInstalledDistPackage()
  43. {
  44. $package = $this->createPackageMock();
  45. $package
  46. ->expects($this->once())
  47. ->method('getInstallationSource')
  48. ->will($this->returnValue('dist'));
  49. $package
  50. ->expects($this->once())
  51. ->method('getDistType')
  52. ->will($this->returnValue('pear'));
  53. $downloader = $this->createDownloaderMock();
  54. $downloader
  55. ->expects($this->once())
  56. ->method('getInstallationSource')
  57. ->will($this->returnValue('dist'));
  58. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  59. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  60. ->setMethods(array('getDownloader'))
  61. ->getMock();
  62. $manager
  63. ->expects($this->once())
  64. ->method('getDownloader')
  65. ->with('pear')
  66. ->will($this->returnValue($downloader));
  67. $this->assertSame($downloader, $manager->getDownloaderForInstalledPackage($package));
  68. }
  69. public function testGetDownloaderForIncorrectlyInstalledDistPackage()
  70. {
  71. $package = $this->createPackageMock();
  72. $package
  73. ->expects($this->once())
  74. ->method('getInstallationSource')
  75. ->will($this->returnValue('dist'));
  76. $package
  77. ->expects($this->once())
  78. ->method('getDistType')
  79. ->will($this->returnValue('git'));
  80. $downloader = $this->createDownloaderMock();
  81. $downloader
  82. ->expects($this->exactly(2))
  83. ->method('getInstallationSource')
  84. ->will($this->returnValue('source'));
  85. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  86. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  87. ->setMethods(array('getDownloader'))
  88. ->getMock();
  89. $manager
  90. ->expects($this->once())
  91. ->method('getDownloader')
  92. ->with('git')
  93. ->will($this->returnValue($downloader));
  94. $this->setExpectedException('LogicException');
  95. $manager->getDownloaderForInstalledPackage($package);
  96. }
  97. public function testGetDownloaderForCorrectlyInstalledSourcePackage()
  98. {
  99. $package = $this->createPackageMock();
  100. $package
  101. ->expects($this->once())
  102. ->method('getInstallationSource')
  103. ->will($this->returnValue('source'));
  104. $package
  105. ->expects($this->once())
  106. ->method('getSourceType')
  107. ->will($this->returnValue('git'));
  108. $downloader = $this->createDownloaderMock();
  109. $downloader
  110. ->expects($this->once())
  111. ->method('getInstallationSource')
  112. ->will($this->returnValue('source'));
  113. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  114. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  115. ->setMethods(array('getDownloader'))
  116. ->getMock();
  117. $manager
  118. ->expects($this->once())
  119. ->method('getDownloader')
  120. ->with('git')
  121. ->will($this->returnValue($downloader));
  122. $this->assertSame($downloader, $manager->getDownloaderForInstalledPackage($package));
  123. }
  124. public function testGetDownloaderForIncorrectlyInstalledSourcePackage()
  125. {
  126. $package = $this->createPackageMock();
  127. $package
  128. ->expects($this->once())
  129. ->method('getInstallationSource')
  130. ->will($this->returnValue('source'));
  131. $package
  132. ->expects($this->once())
  133. ->method('getSourceType')
  134. ->will($this->returnValue('pear'));
  135. $downloader = $this->createDownloaderMock();
  136. $downloader
  137. ->expects($this->exactly(2))
  138. ->method('getInstallationSource')
  139. ->will($this->returnValue('dist'));
  140. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  141. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  142. ->setMethods(array('getDownloader'))
  143. ->getMock();
  144. $manager
  145. ->expects($this->once())
  146. ->method('getDownloader')
  147. ->with('pear')
  148. ->will($this->returnValue($downloader));
  149. $this->setExpectedException('LogicException');
  150. $manager->getDownloaderForInstalledPackage($package);
  151. }
  152. public function testGetDownloaderForMetapackage()
  153. {
  154. $package = $this->createPackageMock();
  155. $package
  156. ->expects($this->once())
  157. ->method('getType')
  158. ->will($this->returnValue('metapackage'));
  159. $manager = new DownloadManager($this->io, false, $this->filesystem);
  160. $this->assertNull($manager->getDownloaderForInstalledPackage($package));
  161. }
  162. public function testFullPackageDownload()
  163. {
  164. $package = $this->createPackageMock();
  165. $package
  166. ->expects($this->once())
  167. ->method('getSourceType')
  168. ->will($this->returnValue('git'));
  169. $package
  170. ->expects($this->once())
  171. ->method('getDistType')
  172. ->will($this->returnValue('pear'));
  173. $package
  174. ->expects($this->once())
  175. ->method('setInstallationSource')
  176. ->with('dist');
  177. $downloader = $this->createDownloaderMock();
  178. $downloader
  179. ->expects($this->once())
  180. ->method('download')
  181. ->with($package, 'target_dir');
  182. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  183. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  184. ->setMethods(array('getDownloaderForInstalledPackage'))
  185. ->getMock();
  186. $manager
  187. ->expects($this->once())
  188. ->method('getDownloaderForInstalledPackage')
  189. ->with($package)
  190. ->will($this->returnValue($downloader));
  191. $manager->download($package, 'target_dir');
  192. }
  193. public function testFullPackageDownloadFailover()
  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('pear'));
  204. $package
  205. ->expects($this->any())
  206. ->method('getPrettyString')
  207. ->will($this->returnValue('prettyPackage'));
  208. $package
  209. ->expects($this->at(3))
  210. ->method('setInstallationSource')
  211. ->with('dist');
  212. $package
  213. ->expects($this->at(5))
  214. ->method('setInstallationSource')
  215. ->with('source');
  216. $downloaderFail = $this->createDownloaderMock();
  217. $downloaderFail
  218. ->expects($this->once())
  219. ->method('download')
  220. ->with($package, 'target_dir')
  221. ->will($this->throwException(new \RuntimeException("Foo")));
  222. $downloaderSuccess = $this->createDownloaderMock();
  223. $downloaderSuccess
  224. ->expects($this->once())
  225. ->method('download')
  226. ->with($package, 'target_dir');
  227. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  228. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  229. ->setMethods(array('getDownloaderForInstalledPackage'))
  230. ->getMock();
  231. $manager
  232. ->expects($this->at(0))
  233. ->method('getDownloaderForInstalledPackage')
  234. ->with($package)
  235. ->will($this->returnValue($downloaderFail));
  236. $manager
  237. ->expects($this->at(1))
  238. ->method('getDownloaderForInstalledPackage')
  239. ->with($package)
  240. ->will($this->returnValue($downloaderSuccess));
  241. $manager->download($package, 'target_dir');
  242. }
  243. public function testBadPackageDownload()
  244. {
  245. $package = $this->createPackageMock();
  246. $package
  247. ->expects($this->once())
  248. ->method('getSourceType')
  249. ->will($this->returnValue(null));
  250. $package
  251. ->expects($this->once())
  252. ->method('getDistType')
  253. ->will($this->returnValue(null));
  254. $manager = new DownloadManager($this->io, false, $this->filesystem);
  255. $this->setExpectedException('InvalidArgumentException');
  256. $manager->download($package, 'target_dir');
  257. }
  258. public function testDistOnlyPackageDownload()
  259. {
  260. $package = $this->createPackageMock();
  261. $package
  262. ->expects($this->once())
  263. ->method('getSourceType')
  264. ->will($this->returnValue(null));
  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('dist');
  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($this->io, 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->download($package, 'target_dir');
  288. }
  289. public function testSourceOnlyPackageDownload()
  290. {
  291. $package = $this->createPackageMock();
  292. $package
  293. ->expects($this->once())
  294. ->method('getSourceType')
  295. ->will($this->returnValue('git'));
  296. $package
  297. ->expects($this->once())
  298. ->method('getDistType')
  299. ->will($this->returnValue(null));
  300. $package
  301. ->expects($this->once())
  302. ->method('setInstallationSource')
  303. ->with('source');
  304. $downloader = $this->createDownloaderMock();
  305. $downloader
  306. ->expects($this->once())
  307. ->method('download')
  308. ->with($package, 'target_dir');
  309. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  310. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  311. ->setMethods(array('getDownloaderForInstalledPackage'))
  312. ->getMock();
  313. $manager
  314. ->expects($this->once())
  315. ->method('getDownloaderForInstalledPackage')
  316. ->with($package)
  317. ->will($this->returnValue($downloader));
  318. $manager->download($package, 'target_dir');
  319. }
  320. public function testMetapackagePackageDownload()
  321. {
  322. $package = $this->createPackageMock();
  323. $package
  324. ->expects($this->once())
  325. ->method('getSourceType')
  326. ->will($this->returnValue('git'));
  327. $package
  328. ->expects($this->once())
  329. ->method('getDistType')
  330. ->will($this->returnValue(null));
  331. $package
  332. ->expects($this->once())
  333. ->method('setInstallationSource')
  334. ->with('source');
  335. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  336. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  337. ->setMethods(array('getDownloaderForInstalledPackage'))
  338. ->getMock();
  339. $manager
  340. ->expects($this->once())
  341. ->method('getDownloaderForInstalledPackage')
  342. ->with($package)
  343. ->will($this->returnValue(null)); // There is no downloader for Metapackages.
  344. $manager->download($package, 'target_dir');
  345. }
  346. public function testFullPackageDownloadWithSourcePreferred()
  347. {
  348. $package = $this->createPackageMock();
  349. $package
  350. ->expects($this->once())
  351. ->method('getSourceType')
  352. ->will($this->returnValue('git'));
  353. $package
  354. ->expects($this->once())
  355. ->method('getDistType')
  356. ->will($this->returnValue('pear'));
  357. $package
  358. ->expects($this->once())
  359. ->method('setInstallationSource')
  360. ->with('source');
  361. $downloader = $this->createDownloaderMock();
  362. $downloader
  363. ->expects($this->once())
  364. ->method('download')
  365. ->with($package, 'target_dir');
  366. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  367. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  368. ->setMethods(array('getDownloaderForInstalledPackage'))
  369. ->getMock();
  370. $manager
  371. ->expects($this->once())
  372. ->method('getDownloaderForInstalledPackage')
  373. ->with($package)
  374. ->will($this->returnValue($downloader));
  375. $manager->setPreferSource(true);
  376. $manager->download($package, 'target_dir');
  377. }
  378. public function testDistOnlyPackageDownloadWithSourcePreferred()
  379. {
  380. $package = $this->createPackageMock();
  381. $package
  382. ->expects($this->once())
  383. ->method('getSourceType')
  384. ->will($this->returnValue(null));
  385. $package
  386. ->expects($this->once())
  387. ->method('getDistType')
  388. ->will($this->returnValue('pear'));
  389. $package
  390. ->expects($this->once())
  391. ->method('setInstallationSource')
  392. ->with('dist');
  393. $downloader = $this->createDownloaderMock();
  394. $downloader
  395. ->expects($this->once())
  396. ->method('download')
  397. ->with($package, 'target_dir');
  398. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  399. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  400. ->setMethods(array('getDownloaderForInstalledPackage'))
  401. ->getMock();
  402. $manager
  403. ->expects($this->once())
  404. ->method('getDownloaderForInstalledPackage')
  405. ->with($package)
  406. ->will($this->returnValue($downloader));
  407. $manager->setPreferSource(true);
  408. $manager->download($package, 'target_dir');
  409. }
  410. public function testSourceOnlyPackageDownloadWithSourcePreferred()
  411. {
  412. $package = $this->createPackageMock();
  413. $package
  414. ->expects($this->once())
  415. ->method('getSourceType')
  416. ->will($this->returnValue('git'));
  417. $package
  418. ->expects($this->once())
  419. ->method('getDistType')
  420. ->will($this->returnValue(null));
  421. $package
  422. ->expects($this->once())
  423. ->method('setInstallationSource')
  424. ->with('source');
  425. $downloader = $this->createDownloaderMock();
  426. $downloader
  427. ->expects($this->once())
  428. ->method('download')
  429. ->with($package, 'target_dir');
  430. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  431. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  432. ->setMethods(array('getDownloaderForInstalledPackage'))
  433. ->getMock();
  434. $manager
  435. ->expects($this->once())
  436. ->method('getDownloaderForInstalledPackage')
  437. ->with($package)
  438. ->will($this->returnValue($downloader));
  439. $manager->setPreferSource(true);
  440. $manager->download($package, 'target_dir');
  441. }
  442. public function testBadPackageDownloadWithSourcePreferred()
  443. {
  444. $package = $this->createPackageMock();
  445. $package
  446. ->expects($this->once())
  447. ->method('getSourceType')
  448. ->will($this->returnValue(null));
  449. $package
  450. ->expects($this->once())
  451. ->method('getDistType')
  452. ->will($this->returnValue(null));
  453. $manager = new DownloadManager($this->io, false, $this->filesystem);
  454. $manager->setPreferSource(true);
  455. $this->setExpectedException('InvalidArgumentException');
  456. $manager->download($package, 'target_dir');
  457. }
  458. public function testUpdateDistWithEqualTypes()
  459. {
  460. $initial = $this->createPackageMock();
  461. $initial
  462. ->expects($this->once())
  463. ->method('getInstallationSource')
  464. ->will($this->returnValue('dist'));
  465. $initial
  466. ->expects($this->once())
  467. ->method('getDistType')
  468. ->will($this->returnValue('pear'));
  469. $target = $this->createPackageMock();
  470. $target
  471. ->expects($this->once())
  472. ->method('getDistType')
  473. ->will($this->returnValue('pear'));
  474. $target
  475. ->expects($this->once())
  476. ->method('setInstallationSource')
  477. ->with('dist');
  478. $pearDownloader = $this->createDownloaderMock();
  479. $pearDownloader
  480. ->expects($this->once())
  481. ->method('update')
  482. ->with($initial, $target, 'vendor/bundles/FOS/UserBundle');
  483. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  484. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  485. ->setMethods(array('getDownloaderForInstalledPackage'))
  486. ->getMock();
  487. $manager
  488. ->expects($this->once())
  489. ->method('getDownloaderForInstalledPackage')
  490. ->with($initial)
  491. ->will($this->returnValue($pearDownloader));
  492. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  493. }
  494. public function testUpdateDistWithNotEqualTypes()
  495. {
  496. $initial = $this->createPackageMock();
  497. $initial
  498. ->expects($this->once())
  499. ->method('getInstallationSource')
  500. ->will($this->returnValue('dist'));
  501. $initial
  502. ->expects($this->once())
  503. ->method('getDistType')
  504. ->will($this->returnValue('pear'));
  505. $target = $this->createPackageMock();
  506. $target
  507. ->expects($this->once())
  508. ->method('getDistType')
  509. ->will($this->returnValue('composer'));
  510. $pearDownloader = $this->createDownloaderMock();
  511. $pearDownloader
  512. ->expects($this->once())
  513. ->method('remove')
  514. ->with($initial, 'vendor/bundles/FOS/UserBundle');
  515. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  516. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  517. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  518. ->getMock();
  519. $manager
  520. ->expects($this->once())
  521. ->method('getDownloaderForInstalledPackage')
  522. ->with($initial)
  523. ->will($this->returnValue($pearDownloader));
  524. $manager
  525. ->expects($this->once())
  526. ->method('download')
  527. ->with($target, 'vendor/bundles/FOS/UserBundle', false);
  528. $manager->update($initial, $target, 'vendor/bundles/FOS/UserBundle');
  529. }
  530. public function testUpdateSourceWithEqualTypes()
  531. {
  532. $initial = $this->createPackageMock();
  533. $initial
  534. ->expects($this->once())
  535. ->method('getInstallationSource')
  536. ->will($this->returnValue('source'));
  537. $initial
  538. ->expects($this->once())
  539. ->method('getSourceType')
  540. ->will($this->returnValue('svn'));
  541. $target = $this->createPackageMock();
  542. $target
  543. ->expects($this->once())
  544. ->method('getSourceType')
  545. ->will($this->returnValue('svn'));
  546. $svnDownloader = $this->createDownloaderMock();
  547. $svnDownloader
  548. ->expects($this->once())
  549. ->method('update')
  550. ->with($initial, $target, 'vendor/pkg');
  551. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  552. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  553. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  554. ->getMock();
  555. $manager
  556. ->expects($this->once())
  557. ->method('getDownloaderForInstalledPackage')
  558. ->with($initial)
  559. ->will($this->returnValue($svnDownloader));
  560. $manager->update($initial, $target, 'vendor/pkg');
  561. }
  562. public function testUpdateSourceWithNotEqualTypes()
  563. {
  564. $initial = $this->createPackageMock();
  565. $initial
  566. ->expects($this->once())
  567. ->method('getInstallationSource')
  568. ->will($this->returnValue('source'));
  569. $initial
  570. ->expects($this->once())
  571. ->method('getSourceType')
  572. ->will($this->returnValue('svn'));
  573. $target = $this->createPackageMock();
  574. $target
  575. ->expects($this->once())
  576. ->method('getSourceType')
  577. ->will($this->returnValue('git'));
  578. $svnDownloader = $this->createDownloaderMock();
  579. $svnDownloader
  580. ->expects($this->once())
  581. ->method('remove')
  582. ->with($initial, 'vendor/pkg');
  583. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  584. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  585. ->setMethods(array('getDownloaderForInstalledPackage', 'download'))
  586. ->getMock();
  587. $manager
  588. ->expects($this->once())
  589. ->method('getDownloaderForInstalledPackage')
  590. ->with($initial)
  591. ->will($this->returnValue($svnDownloader));
  592. $manager
  593. ->expects($this->once())
  594. ->method('download')
  595. ->with($target, 'vendor/pkg', true);
  596. $manager->update($initial, $target, 'vendor/pkg');
  597. }
  598. public function testUpdateMetapackage()
  599. {
  600. $initial = $this->createPackageMock();
  601. $target = $this->createPackageMock();
  602. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  603. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  604. ->setMethods(array('getDownloaderForInstalledPackage'))
  605. ->getMock();
  606. $manager
  607. ->expects($this->once())
  608. ->method('getDownloaderForInstalledPackage')
  609. ->with($initial)
  610. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  611. $manager->update($initial, $target, 'vendor/pkg');
  612. }
  613. public function testRemove()
  614. {
  615. $package = $this->createPackageMock();
  616. $pearDownloader = $this->createDownloaderMock();
  617. $pearDownloader
  618. ->expects($this->once())
  619. ->method('remove')
  620. ->with($package, 'vendor/bundles/FOS/UserBundle');
  621. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  622. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  623. ->setMethods(array('getDownloaderForInstalledPackage'))
  624. ->getMock();
  625. $manager
  626. ->expects($this->once())
  627. ->method('getDownloaderForInstalledPackage')
  628. ->with($package)
  629. ->will($this->returnValue($pearDownloader));
  630. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  631. }
  632. public function testMetapackageRemove()
  633. {
  634. $package = $this->createPackageMock();
  635. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  636. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  637. ->setMethods(array('getDownloaderForInstalledPackage'))
  638. ->getMock();
  639. $manager
  640. ->expects($this->once())
  641. ->method('getDownloaderForInstalledPackage')
  642. ->with($package)
  643. ->will($this->returnValue(null)); // There is no downloader for metapackages.
  644. $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
  645. }
  646. /**
  647. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  648. */
  649. public function testInstallPreferenceWithoutPreferenceDev()
  650. {
  651. $package = $this->createPackageMock();
  652. $package
  653. ->expects($this->once())
  654. ->method('getSourceType')
  655. ->will($this->returnValue('git'));
  656. $package
  657. ->expects($this->once())
  658. ->method('getDistType')
  659. ->will($this->returnValue('pear'));
  660. $package
  661. ->expects($this->once())
  662. ->method('isDev')
  663. ->will($this->returnValue(true));
  664. $package
  665. ->expects($this->once())
  666. ->method('setInstallationSource')
  667. ->with('source');
  668. $downloader = $this->createDownloaderMock();
  669. $downloader
  670. ->expects($this->once())
  671. ->method('download')
  672. ->with($package, 'target_dir');
  673. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  674. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  675. ->setMethods(array('getDownloaderForInstalledPackage'))
  676. ->getMock();
  677. $manager
  678. ->expects($this->once())
  679. ->method('getDownloaderForInstalledPackage')
  680. ->with($package)
  681. ->will($this->returnValue($downloader));
  682. $manager->download($package, 'target_dir');
  683. }
  684. /**
  685. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  686. */
  687. public function testInstallPreferenceWithoutPreferenceNoDev()
  688. {
  689. $package = $this->createPackageMock();
  690. $package
  691. ->expects($this->once())
  692. ->method('getSourceType')
  693. ->will($this->returnValue('git'));
  694. $package
  695. ->expects($this->once())
  696. ->method('getDistType')
  697. ->will($this->returnValue('pear'));
  698. $package
  699. ->expects($this->once())
  700. ->method('isDev')
  701. ->will($this->returnValue(false));
  702. $package
  703. ->expects($this->once())
  704. ->method('setInstallationSource')
  705. ->with('dist');
  706. $downloader = $this->createDownloaderMock();
  707. $downloader
  708. ->expects($this->once())
  709. ->method('download')
  710. ->with($package, 'target_dir');
  711. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  712. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  713. ->setMethods(array('getDownloaderForInstalledPackage'))
  714. ->getMock();
  715. $manager
  716. ->expects($this->once())
  717. ->method('getDownloaderForInstalledPackage')
  718. ->with($package)
  719. ->will($this->returnValue($downloader));
  720. $manager->download($package, 'target_dir');
  721. }
  722. /**
  723. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  724. */
  725. public function testInstallPreferenceWithoutMatchDev()
  726. {
  727. $package = $this->createPackageMock();
  728. $package
  729. ->expects($this->once())
  730. ->method('getSourceType')
  731. ->will($this->returnValue('git'));
  732. $package
  733. ->expects($this->once())
  734. ->method('getDistType')
  735. ->will($this->returnValue('pear'));
  736. $package
  737. ->expects($this->once())
  738. ->method('isDev')
  739. ->will($this->returnValue(true));
  740. $package
  741. ->expects($this->once())
  742. ->method('getName')
  743. ->will($this->returnValue('bar/package'));
  744. $package
  745. ->expects($this->once())
  746. ->method('setInstallationSource')
  747. ->with('source');
  748. $downloader = $this->createDownloaderMock();
  749. $downloader
  750. ->expects($this->once())
  751. ->method('download')
  752. ->with($package, 'target_dir');
  753. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  754. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  755. ->setMethods(array('getDownloaderForInstalledPackage'))
  756. ->getMock();
  757. $manager
  758. ->expects($this->once())
  759. ->method('getDownloaderForInstalledPackage')
  760. ->with($package)
  761. ->will($this->returnValue($downloader));
  762. $manager->setPreferences(array('foo/*' => 'source'));
  763. $manager->download($package, 'target_dir');
  764. }
  765. /**
  766. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  767. */
  768. public function testInstallPreferenceWithoutMatchNoDev()
  769. {
  770. $package = $this->createPackageMock();
  771. $package
  772. ->expects($this->once())
  773. ->method('getSourceType')
  774. ->will($this->returnValue('git'));
  775. $package
  776. ->expects($this->once())
  777. ->method('getDistType')
  778. ->will($this->returnValue('pear'));
  779. $package
  780. ->expects($this->once())
  781. ->method('isDev')
  782. ->will($this->returnValue(false));
  783. $package
  784. ->expects($this->once())
  785. ->method('getName')
  786. ->will($this->returnValue('bar/package'));
  787. $package
  788. ->expects($this->once())
  789. ->method('setInstallationSource')
  790. ->with('dist');
  791. $downloader = $this->createDownloaderMock();
  792. $downloader
  793. ->expects($this->once())
  794. ->method('download')
  795. ->with($package, 'target_dir');
  796. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  797. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  798. ->setMethods(array('getDownloaderForInstalledPackage'))
  799. ->getMock();
  800. $manager
  801. ->expects($this->once())
  802. ->method('getDownloaderForInstalledPackage')
  803. ->with($package)
  804. ->will($this->returnValue($downloader));
  805. $manager->setPreferences(array('foo/*' => 'source'));
  806. $manager->download($package, 'target_dir');
  807. }
  808. /**
  809. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  810. */
  811. public function testInstallPreferenceWithMatchAutoDev()
  812. {
  813. $package = $this->createPackageMock();
  814. $package
  815. ->expects($this->once())
  816. ->method('getSourceType')
  817. ->will($this->returnValue('git'));
  818. $package
  819. ->expects($this->once())
  820. ->method('getDistType')
  821. ->will($this->returnValue('pear'));
  822. $package
  823. ->expects($this->once())
  824. ->method('isDev')
  825. ->will($this->returnValue(true));
  826. $package
  827. ->expects($this->once())
  828. ->method('getName')
  829. ->will($this->returnValue('foo/package'));
  830. $package
  831. ->expects($this->once())
  832. ->method('setInstallationSource')
  833. ->with('source');
  834. $downloader = $this->createDownloaderMock();
  835. $downloader
  836. ->expects($this->once())
  837. ->method('download')
  838. ->with($package, 'target_dir');
  839. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  840. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  841. ->setMethods(array('getDownloaderForInstalledPackage'))
  842. ->getMock();
  843. $manager
  844. ->expects($this->once())
  845. ->method('getDownloaderForInstalledPackage')
  846. ->with($package)
  847. ->will($this->returnValue($downloader));
  848. $manager->setPreferences(array('foo/*' => 'auto'));
  849. $manager->download($package, 'target_dir');
  850. }
  851. /**
  852. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  853. */
  854. public function testInstallPreferenceWithMatchAutoNoDev()
  855. {
  856. $package = $this->createPackageMock();
  857. $package
  858. ->expects($this->once())
  859. ->method('getSourceType')
  860. ->will($this->returnValue('git'));
  861. $package
  862. ->expects($this->once())
  863. ->method('getDistType')
  864. ->will($this->returnValue('pear'));
  865. $package
  866. ->expects($this->once())
  867. ->method('isDev')
  868. ->will($this->returnValue(false));
  869. $package
  870. ->expects($this->once())
  871. ->method('getName')
  872. ->will($this->returnValue('foo/package'));
  873. $package
  874. ->expects($this->once())
  875. ->method('setInstallationSource')
  876. ->with('dist');
  877. $downloader = $this->createDownloaderMock();
  878. $downloader
  879. ->expects($this->once())
  880. ->method('download')
  881. ->with($package, 'target_dir');
  882. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  883. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  884. ->setMethods(array('getDownloaderForInstalledPackage'))
  885. ->getMock();
  886. $manager
  887. ->expects($this->once())
  888. ->method('getDownloaderForInstalledPackage')
  889. ->with($package)
  890. ->will($this->returnValue($downloader));
  891. $manager->setPreferences(array('foo/*' => 'auto'));
  892. $manager->download($package, 'target_dir');
  893. }
  894. /**
  895. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  896. */
  897. public function testInstallPreferenceWithMatchSource()
  898. {
  899. $package = $this->createPackageMock();
  900. $package
  901. ->expects($this->once())
  902. ->method('getSourceType')
  903. ->will($this->returnValue('git'));
  904. $package
  905. ->expects($this->once())
  906. ->method('getDistType')
  907. ->will($this->returnValue('pear'));
  908. $package
  909. ->expects($this->once())
  910. ->method('getName')
  911. ->will($this->returnValue('foo/package'));
  912. $package
  913. ->expects($this->once())
  914. ->method('setInstallationSource')
  915. ->with('source');
  916. $downloader = $this->createDownloaderMock();
  917. $downloader
  918. ->expects($this->once())
  919. ->method('download')
  920. ->with($package, 'target_dir');
  921. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  922. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  923. ->setMethods(array('getDownloaderForInstalledPackage'))
  924. ->getMock();
  925. $manager
  926. ->expects($this->once())
  927. ->method('getDownloaderForInstalledPackage')
  928. ->with($package)
  929. ->will($this->returnValue($downloader));
  930. $manager->setPreferences(array('foo/*' => 'source'));
  931. $manager->download($package, 'target_dir');
  932. }
  933. /**
  934. * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
  935. */
  936. public function testInstallPreferenceWithMatchDist()
  937. {
  938. $package = $this->createPackageMock();
  939. $package
  940. ->expects($this->once())
  941. ->method('getSourceType')
  942. ->will($this->returnValue('git'));
  943. $package
  944. ->expects($this->once())
  945. ->method('getDistType')
  946. ->will($this->returnValue('pear'));
  947. $package
  948. ->expects($this->once())
  949. ->method('getName')
  950. ->will($this->returnValue('foo/package'));
  951. $package
  952. ->expects($this->once())
  953. ->method('setInstallationSource')
  954. ->with('dist');
  955. $downloader = $this->createDownloaderMock();
  956. $downloader
  957. ->expects($this->once())
  958. ->method('download')
  959. ->with($package, 'target_dir');
  960. $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  961. ->setConstructorArgs(array($this->io, false, $this->filesystem))
  962. ->setMethods(array('getDownloaderForInstalledPackage'))
  963. ->getMock();
  964. $manager
  965. ->expects($this->once())
  966. ->method('getDownloaderForInstalledPackage')
  967. ->with($package)
  968. ->will($this->returnValue($downloader));
  969. $manager->setPreferences(array('foo/*' => 'dist'));
  970. $manager->download($package, 'target_dir');
  971. }
  972. private function createDownloaderMock()
  973. {
  974. return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
  975. ->getMock();
  976. }
  977. private function createPackageMock()
  978. {
  979. return $this->getMockBuilder('Composer\Package\PackageInterface')
  980. ->getMock();
  981. }
  982. }