FileDownloaderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, $rfs = null)
  17. {
  18. $io = $io ?: $this->getMock('Composer\IO\IOInterface');
  19. $rfs = $rfs ?: $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()->getMock();
  20. return new FileDownloader($io, $rfs);
  21. }
  22. /**
  23. * @expectedException \InvalidArgumentException
  24. */
  25. public function testDownloadForPackageWithoutDistReference()
  26. {
  27. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  28. $packageMock->expects($this->once())
  29. ->method('getDistUrl')
  30. ->will($this->returnValue(null))
  31. ;
  32. $downloader = $this->getDownloader();
  33. $downloader->download($packageMock, '/path');
  34. }
  35. public function testDownloadToExistingFile()
  36. {
  37. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  38. $packageMock->expects($this->once())
  39. ->method('getDistUrl')
  40. ->will($this->returnValue('url'))
  41. ;
  42. $path = tempnam(sys_get_temp_dir(), 'c');
  43. $downloader = $this->getDownloader();
  44. try {
  45. $downloader->download($packageMock, $path);
  46. $this->fail();
  47. } catch (\Exception $e) {
  48. if (file_exists($path)) {
  49. unset($path);
  50. }
  51. $this->assertInstanceOf('RuntimeException', $e);
  52. $this->assertContains('exists and is not a directory', $e->getMessage());
  53. }
  54. }
  55. public function testGetFileName()
  56. {
  57. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  58. $packageMock->expects($this->once())
  59. ->method('getDistUrl')
  60. ->will($this->returnValue('http://example.com/script.js'))
  61. ;
  62. $downloader = $this->getDownloader();
  63. $method = new \ReflectionMethod($downloader, 'getFileName');
  64. $method->setAccessible(true);
  65. $this->assertEquals('/path/script.js', $method->invoke($downloader, $packageMock, '/path'));
  66. }
  67. public function testDownloadButFileIsUnsaved()
  68. {
  69. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  70. $packageMock->expects($this->any())
  71. ->method('getDistUrl')
  72. ->will($this->returnValue('http://example.com/script.js'))
  73. ;
  74. do {
  75. $path = sys_get_temp_dir().'/'.md5(time().rand());
  76. } while (file_exists($path));
  77. $ioMock = $this->getMock('Composer\IO\IOInterface');
  78. $ioMock->expects($this->any())
  79. ->method('write')
  80. ->will($this->returnCallback(function($messages, $newline = true) use ($path) {
  81. if (is_file($path.'/script.js')) {
  82. unlink($path.'/script.js');
  83. }
  84. return $messages;
  85. }))
  86. ;
  87. $downloader = $this->getDownloader($ioMock);
  88. try {
  89. $downloader->download($packageMock, $path);
  90. $this->fail();
  91. } catch (\Exception $e) {
  92. if (is_dir($path)) {
  93. $fs = new Filesystem();
  94. $fs->removeDirectory($path);
  95. } else if (is_file($path)) {
  96. unset($path);
  97. }
  98. $this->assertInstanceOf('UnexpectedValueException', $e);
  99. $this->assertContains('could not be saved to', $e->getMessage());
  100. }
  101. }
  102. public function testDownloadFileWithInvalidChecksum()
  103. {
  104. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  105. $packageMock->expects($this->any())
  106. ->method('getDistUrl')
  107. ->will($this->returnValue('http://example.com/script.js'))
  108. ;
  109. $packageMock->expects($this->any())
  110. ->method('getDistSha1Checksum')
  111. ->will($this->returnValue('invalid'))
  112. ;
  113. do {
  114. $path = sys_get_temp_dir().'/'.md5(time().rand());
  115. } while (file_exists($path));
  116. $downloader = $this->getDownloader();
  117. // make sure the file expected to be downloaded is on disk already
  118. mkdir($path, 0777, true);
  119. touch($path.'/script.js');
  120. try {
  121. $downloader->download($packageMock, $path);
  122. $this->fail();
  123. } catch (\Exception $e) {
  124. if (is_dir($path)) {
  125. $fs = new Filesystem();
  126. $fs->removeDirectory($path);
  127. } else if (is_file($path)) {
  128. unset($path);
  129. }
  130. $this->assertInstanceOf('UnexpectedValueException', $e);
  131. $this->assertContains('checksum verification', $e->getMessage());
  132. }
  133. }
  134. }