PageRenderTime 31ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/composer/composer/tests/Composer/Test/Downloader/FileDownloaderTest.php

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