FileDownloaderTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, $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, null, null, $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. $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. do {
  79. $path = sys_get_temp_dir().'/'.md5(time().mt_rand());
  80. } while (file_exists($path));
  81. $ioMock = $this->getMock('Composer\IO\IOInterface');
  82. $ioMock->expects($this->any())
  83. ->method('write')
  84. ->will($this->returnCallback(function($messages, $newline = true) use ($path) {
  85. if (is_file($path.'/script.js')) {
  86. unlink($path.'/script.js');
  87. }
  88. return $messages;
  89. }))
  90. ;
  91. $downloader = $this->getDownloader($ioMock);
  92. try {
  93. $downloader->download($packageMock, $path);
  94. $this->fail();
  95. } catch (\Exception $e) {
  96. if (is_dir($path)) {
  97. $fs = new Filesystem();
  98. $fs->removeDirectory($path);
  99. } elseif (is_file($path)) {
  100. unlink($path);
  101. }
  102. $this->assertInstanceOf('UnexpectedValueException', $e);
  103. $this->assertContains('could not be saved to', $e->getMessage());
  104. }
  105. }
  106. public function testDownloadFileWithInvalidChecksum()
  107. {
  108. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  109. $packageMock->expects($this->any())
  110. ->method('getDistUrl')
  111. ->will($this->returnValue('http://example.com/script.js'))
  112. ;
  113. $packageMock->expects($this->any())
  114. ->method('getDistSha1Checksum')
  115. ->will($this->returnValue('invalid'))
  116. ;
  117. $filesystem = $this->getMock('Composer\Util\Filesystem');
  118. do {
  119. $path = sys_get_temp_dir().'/'.md5(time().mt_rand());
  120. } while (file_exists($path));
  121. $downloader = $this->getDownloader(null, null, null, $filesystem);
  122. // make sure the file expected to be downloaded is on disk already
  123. mkdir($path, 0777, true);
  124. touch($path.'/script.js');
  125. try {
  126. $downloader->download($packageMock, $path);
  127. $this->fail();
  128. } catch (\Exception $e) {
  129. if (is_dir($path)) {
  130. $fs = new Filesystem();
  131. $fs->removeDirectory($path);
  132. } elseif (is_file($path)) {
  133. unlink($path);
  134. }
  135. $this->assertInstanceOf('UnexpectedValueException', $e);
  136. $this->assertContains('checksum verification', $e->getMessage());
  137. }
  138. }
  139. }