FileDownloaderTest.php 4.7 KB

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