FileDownloaderTest.php 9.8 KB

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