FileDownloaderTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\FileDownloader;
  13. use Composer\Test\TestCase;
  14. use Composer\Util\Filesystem;
  15. use Composer\Util\Http\Response;
  16. use Composer\Util\Loop;
  17. class FileDownloaderTest extends TestCase
  18. {
  19. private $httpDownloader;
  20. protected function getDownloader($io = null, $config = null, $eventDispatcher = null, $cache = null, $httpDownloader = null, $filesystem = null)
  21. {
  22. $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  23. $config = $config ?: $this->getMockBuilder('Composer\Config')->getMock();
  24. $httpDownloader = $httpDownloader ?: $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
  25. $httpDownloader
  26. ->expects($this->any())
  27. ->method('addCopy')
  28. ->will($this->returnValue(\React\Promise\resolve(new Response(array('url' => 'http://example.org/'), 200, array(), 'file~'))));
  29. $this->httpDownloader = $httpDownloader;
  30. return new FileDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $filesystem);
  31. }
  32. /**
  33. * @expectedException \InvalidArgumentException
  34. */
  35. public function testDownloadForPackageWithoutDistReference()
  36. {
  37. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  38. $packageMock->expects($this->once())
  39. ->method('getDistUrl')
  40. ->will($this->returnValue(null))
  41. ;
  42. $downloader = $this->getDownloader();
  43. $downloader->download($packageMock, '/path');
  44. }
  45. public function testDownloadToExistingFile()
  46. {
  47. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  48. $packageMock->expects($this->once())
  49. ->method('getDistUrl')
  50. ->will($this->returnValue('url'))
  51. ;
  52. $packageMock->expects($this->once())
  53. ->method('getDistUrls')
  54. ->will($this->returnValue(array('url')))
  55. ;
  56. $path = tempnam($this->getUniqueTmpDirectory(), 'c');
  57. $downloader = $this->getDownloader();
  58. try {
  59. $downloader->download($packageMock, $path);
  60. $this->fail();
  61. } catch (\Exception $e) {
  62. if (is_dir($path)) {
  63. $fs = new Filesystem();
  64. $fs->removeDirectory($path);
  65. } elseif (is_file($path)) {
  66. unlink($path);
  67. }
  68. $this->assertInstanceOf('RuntimeException', $e);
  69. $this->assertContains('exists and is not a directory', $e->getMessage());
  70. }
  71. }
  72. public function testGetFileName()
  73. {
  74. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  75. $packageMock->expects($this->once())
  76. ->method('getDistUrl')
  77. ->will($this->returnValue('http://example.com/script.js'))
  78. ;
  79. $downloader = $this->getDownloader();
  80. $method = new \ReflectionMethod($downloader, 'getFileName');
  81. $method->setAccessible(true);
  82. $this->assertEquals('/path_script.js', $method->invoke($downloader, $packageMock, '/path'));
  83. }
  84. public function testDownloadButFileIsUnsaved()
  85. {
  86. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  87. $packageMock->expects($this->any())
  88. ->method('getDistUrl')
  89. ->will($this->returnValue($distUrl = 'http://example.com/script.js'))
  90. ;
  91. $packageMock->expects($this->once())
  92. ->method('getDistUrls')
  93. ->will($this->returnValue(array($distUrl)))
  94. ;
  95. $packageMock->expects($this->atLeastOnce())
  96. ->method('getTransportOptions')
  97. ->will($this->returnValue(array()))
  98. ;
  99. $path = $this->getUniqueTmpDirectory();
  100. $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  101. $ioMock->expects($this->any())
  102. ->method('write')
  103. ->will($this->returnCallback(function ($messages, $newline = true) use ($path) {
  104. if (is_file($path.'/script.js')) {
  105. unlink($path.'/script.js');
  106. }
  107. return $messages;
  108. }))
  109. ;
  110. $downloader = $this->getDownloader($ioMock);
  111. try {
  112. $promise = $downloader->download($packageMock, $path);
  113. $loop = new Loop($this->httpDownloader);
  114. $loop->wait(array($promise));
  115. $this->fail('Download was expected to throw');
  116. } catch (\Exception $e) {
  117. if (is_dir($path)) {
  118. $fs = new Filesystem();
  119. $fs->removeDirectory($path);
  120. } elseif (is_file($path)) {
  121. unlink($path);
  122. }
  123. $this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
  124. $this->assertContains('could not be saved to', $e->getMessage());
  125. }
  126. }
  127. public function testCacheGarbageCollectionIsCalled()
  128. {
  129. $expectedTtl = '99999999';
  130. $configMock = $this->getMockBuilder('Composer\Config')->getMock();
  131. $configMock
  132. ->expects($this->at(0))
  133. ->method('get')
  134. ->with('cache-files-ttl')
  135. ->will($this->returnValue($expectedTtl));
  136. $configMock
  137. ->expects($this->at(1))
  138. ->method('get')
  139. ->with('cache-files-maxsize')
  140. ->will($this->returnValue('500M'));
  141. $cacheMock = $this->getMockBuilder('Composer\Cache')
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $cacheMock
  145. ->expects($this->any())
  146. ->method('gcIsNecessary')
  147. ->will($this->returnValue(true));
  148. $cacheMock
  149. ->expects($this->once())
  150. ->method('gc')
  151. ->with($expectedTtl, $this->anything());
  152. $downloader = $this->getDownloader(null, $configMock, null, $cacheMock, null, null);
  153. }
  154. public function testDownloadFileWithInvalidChecksum()
  155. {
  156. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  157. $packageMock->expects($this->any())
  158. ->method('getDistUrl')
  159. ->will($this->returnValue($distUrl = 'http://example.com/script.js'))
  160. ;
  161. $packageMock->expects($this->atLeastOnce())
  162. ->method('getTransportOptions')
  163. ->will($this->returnValue(array()))
  164. ;
  165. $packageMock->expects($this->any())
  166. ->method('getDistSha1Checksum')
  167. ->will($this->returnValue('invalid'))
  168. ;
  169. $packageMock->expects($this->once())
  170. ->method('getDistUrls')
  171. ->will($this->returnValue(array($distUrl)))
  172. ;
  173. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  174. $path = $this->getUniqueTmpDirectory();
  175. $downloader = $this->getDownloader(null, null, null, null, null, $filesystem);
  176. // make sure the file expected to be downloaded is on disk already
  177. touch($path.'_script.js');
  178. try {
  179. $promise = $downloader->download($packageMock, $path);
  180. $loop = new Loop($this->httpDownloader);
  181. $loop->wait(array($promise));
  182. $this->fail('Download was expected to throw');
  183. } catch (\Exception $e) {
  184. if (is_dir($path)) {
  185. $fs = new Filesystem();
  186. $fs->removeDirectory($path);
  187. } elseif (is_file($path)) {
  188. unlink($path);
  189. }
  190. $this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
  191. $this->assertContains('checksum verification', $e->getMessage());
  192. }
  193. }
  194. public function testDowngradeShowsAppropriateMessage()
  195. {
  196. $oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  197. $oldPackage->expects($this->any())
  198. ->method('getFullPrettyVersion')
  199. ->will($this->returnValue('1.2.0'));
  200. $oldPackage->expects($this->once())
  201. ->method('getVersion')
  202. ->will($this->returnValue('1.2.0.0'));
  203. $newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  204. $newPackage->expects($this->any())
  205. ->method('getFullPrettyVersion')
  206. ->will($this->returnValue('1.0.0'));
  207. $newPackage->expects($this->once())
  208. ->method('getVersion')
  209. ->will($this->returnValue('1.0.0.0'));
  210. $newPackage->expects($this->any())
  211. ->method('getDistUrl')
  212. ->will($this->returnValue($distUrl = 'http://example.com/script.js'));
  213. $newPackage->expects($this->once())
  214. ->method('getDistUrls')
  215. ->will($this->returnValue(array($distUrl)));
  216. $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  217. $ioMock->expects($this->at(0))
  218. ->method('writeError')
  219. ->with($this->stringContains('Downloading'));
  220. $ioMock->expects($this->at(1))
  221. ->method('writeError')
  222. ->with($this->stringContains('Downgrading'));
  223. $path = $this->getUniqueTmpDirectory();
  224. touch($path.'_script.js');
  225. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  226. $filesystem->expects($this->once())
  227. ->method('removeDirectory')
  228. ->will($this->returnValue(true));
  229. $downloader = $this->getDownloader($ioMock, null, null, null, null, $filesystem);
  230. $promise = $downloader->download($newPackage, $path, $oldPackage);
  231. $loop = new Loop($this->httpDownloader);
  232. $loop->wait(array($promise));
  233. $downloader->update($oldPackage, $newPackage, $path);
  234. }
  235. }