FileDownloaderTest.php 8.9 KB

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