FileDownloaderTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, $rfs = 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, null, $rfs);
  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. $path = tempnam(sys_get_temp_dir(), 'c');
  44. $downloader = $this->getDownloader();
  45. try {
  46. $downloader->download($packageMock, $path);
  47. $this->fail();
  48. } catch (\Exception $e) {
  49. if (is_dir($path)) {
  50. $fs = new Filesystem();
  51. $fs->removeDirectory($path);
  52. } elseif (is_file($path)) {
  53. unlink($path);
  54. }
  55. $this->assertInstanceOf('RuntimeException', $e);
  56. $this->assertContains('exists and is not a directory', $e->getMessage());
  57. }
  58. }
  59. public function testGetFileName()
  60. {
  61. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  62. $packageMock->expects($this->once())
  63. ->method('getDistUrl')
  64. ->will($this->returnValue('http://example.com/script.js'))
  65. ;
  66. $downloader = $this->getDownloader();
  67. $method = new \ReflectionMethod($downloader, 'getFileName');
  68. $method->setAccessible(true);
  69. $this->assertEquals('/path/script.js', $method->invoke($downloader, $packageMock, '/path'));
  70. }
  71. public function testDownloadButFileIsUnsaved()
  72. {
  73. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  74. $packageMock->expects($this->any())
  75. ->method('getDistUrl')
  76. ->will($this->returnValue('http://example.com/script.js'))
  77. ;
  78. $packageMock->expects($this->atLeastOnce())
  79. ->method('getOptions')
  80. ->will($this->returnValue(array()))
  81. ;
  82. do {
  83. $path = sys_get_temp_dir().'/'.md5(time().mt_rand());
  84. } while (file_exists($path));
  85. $ioMock = $this->getMock('Composer\IO\IOInterface');
  86. $ioMock->expects($this->any())
  87. ->method('write')
  88. ->will($this->returnCallback(function($messages, $newline = true) use ($path) {
  89. if (is_file($path.'/script.js')) {
  90. unlink($path.'/script.js');
  91. }
  92. return $messages;
  93. }))
  94. ;
  95. $downloader = $this->getDownloader($ioMock);
  96. try {
  97. $downloader->download($packageMock, $path);
  98. $this->fail();
  99. } catch (\Exception $e) {
  100. if (is_dir($path)) {
  101. $fs = new Filesystem();
  102. $fs->removeDirectory($path);
  103. } elseif (is_file($path)) {
  104. unlink($path);
  105. }
  106. $this->assertInstanceOf('UnexpectedValueException', $e);
  107. $this->assertContains('could not be saved to', $e->getMessage());
  108. }
  109. }
  110. public function testDownloadFileWithInvalidChecksum()
  111. {
  112. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  113. $packageMock->expects($this->any())
  114. ->method('getDistUrl')
  115. ->will($this->returnValue('http://example.com/script.js'))
  116. ;
  117. $packageMock->expects($this->atLeastOnce())
  118. ->method('getOptions')
  119. ->will($this->returnValue(array()))
  120. ;
  121. $packageMock->expects($this->any())
  122. ->method('getDistSha1Checksum')
  123. ->will($this->returnValue('invalid'))
  124. ;
  125. do {
  126. $path = sys_get_temp_dir().'/'.md5(time().mt_rand());
  127. } while (file_exists($path));
  128. $downloader = $this->getDownloader();
  129. // make sure the file expected to be downloaded is on disk already
  130. mkdir($path, 0777, true);
  131. touch($path.'/script.js');
  132. try {
  133. $downloader->download($packageMock, $path);
  134. $this->fail();
  135. } catch (\Exception $e) {
  136. if (is_dir($path)) {
  137. $fs = new Filesystem();
  138. $fs->removeDirectory($path);
  139. } elseif (is_file($path)) {
  140. unlink($path);
  141. }
  142. $this->assertInstanceOf('UnexpectedValueException', $e);
  143. $this->assertContains('checksum verification', $e->getMessage());
  144. }
  145. }
  146. }