FileDownloaderTest.php 11 KB

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