ZipDownloaderTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\ZipDownloader;
  13. use Composer\TestCase;
  14. use Composer\Util\Filesystem;
  15. class ZipDownloaderTest extends TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $testDir;
  21. public function setUp()
  22. {
  23. $this->testDir = $this->getUniqueTmpDirectory();
  24. }
  25. public function tearDown()
  26. {
  27. $fs = new Filesystem;
  28. $fs->removeDirectory($this->testDir);
  29. }
  30. public function testErrorMessages()
  31. {
  32. if (!class_exists('ZipArchive')) {
  33. $this->markTestSkipped('zip extension missing');
  34. }
  35. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  36. $packageMock->expects($this->any())
  37. ->method('getDistUrl')
  38. ->will($this->returnValue($distUrl = 'file://'.__FILE__))
  39. ;
  40. $packageMock->expects($this->any())
  41. ->method('getDistUrls')
  42. ->will($this->returnValue(array($distUrl)))
  43. ;
  44. $packageMock->expects($this->atLeastOnce())
  45. ->method('getTransportOptions')
  46. ->will($this->returnValue(array()))
  47. ;
  48. $io = $this->getMock('Composer\IO\IOInterface');
  49. $config = $this->getMock('Composer\Config');
  50. $config->expects($this->at(0))
  51. ->method('get')
  52. ->with('disable-tls')
  53. ->will($this->returnValue(false));
  54. $config->expects($this->at(1))
  55. ->method('get')
  56. ->with('cafile')
  57. ->will($this->returnValue(null));
  58. $config->expects($this->at(2))
  59. ->method('get')
  60. ->with('capath')
  61. ->will($this->returnValue(null));
  62. $config->expects($this->at(3))
  63. ->method('get')
  64. ->with('vendor-dir')
  65. ->will($this->returnValue($this->testDir));
  66. $downloader = new ZipDownloader($io, $config);
  67. try {
  68. $downloader->download($packageMock, sys_get_temp_dir().'/composer-zip-test');
  69. $this->fail('Download of invalid zip files should throw an exception');
  70. } catch (\UnexpectedValueException $e) {
  71. $this->assertContains('is not a zip archive', $e->getMessage());
  72. }
  73. }
  74. /**
  75. * @expectedException \Exception
  76. * @expectedExceptionMessage ZipArchive Failed
  77. */
  78. function testZipArchiveOnlyFailed() {
  79. $downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
  80. $e = new \Exception("ZipArchive Failed");
  81. $downloader->setUp(TRUE, FALSE, $e, NULL);
  82. $downloader->extract('testfile.zip', 'vendor/dir');
  83. }
  84. function testZipArchiveOnlyGood() {
  85. $downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
  86. $downloader->setUp(TRUE, FALSE, TRUE, NULL);
  87. $downloader->extract('testfile.zip', 'vendor/dir');
  88. }
  89. /**
  90. * @expectedException \Exception
  91. * @expectedExceptionMessage SystemUnzip Failed
  92. */
  93. function testSystemUnzipOnlyFailed() {
  94. $this->setExpectedException(\Exception::class);
  95. $downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
  96. $e = new \Exception("SystemUnzip Failed");
  97. $downloader->setUp(FALSE, TRUE, NULL, $e);
  98. $downloader->extract('testfile.zip', 'vendor/dir');
  99. }
  100. function testSystemUnzipOnlyGood() {
  101. $downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
  102. $downloader->setUp(FALSE, TRUE, NULL, TRUE);
  103. $downloader->extract('testfile.zip', 'vendor/dir');
  104. }
  105. function testSystemUnzipFallbackGood() {
  106. $downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
  107. $e = new \Exception("test");
  108. $downloader->setUp(TRUE, TRUE, $e, TRUE);
  109. $downloader->extract('testfile.zip', 'vendor/dir');
  110. }
  111. /**
  112. * @expectedException \Exception
  113. * @expectedExceptionMessage ZipArchive Failed
  114. */
  115. function testSystemUnzipFallbackFailed() {
  116. $this->setExpectedException(\Exception::class);
  117. $downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
  118. $e1 = new \Exception("ZipArchive Failed");
  119. $e2 = new \Exception("SystemUnzip Failed");
  120. $downloader->setUp(TRUE, TRUE, $e1, $e2);
  121. $downloader->extract('testfile.zip', 'vendor/dir');
  122. }
  123. }
  124. class TestDownloader extends ZipDownloader {
  125. public function __construct($io)
  126. {
  127. $this->io = $io;
  128. }
  129. public function extract($file, $path) {
  130. parent::extract($file, $path);
  131. }
  132. public function setUp($zipArchive, $systemUnzip, $zipArchiveResponse, $systemUnzipResponse) {
  133. self::$hasZipArchive = $zipArchive;
  134. self::$hasSystemUnzip = $systemUnzip;
  135. $this->zipArchiveResponse = $zipArchiveResponse;
  136. $this->systemUnzipResponse = $systemUnzipResponse;
  137. }
  138. protected function extractWithZipArchive($file, $path) {
  139. return $this->zipArchiveResponse;
  140. }
  141. protected function extractWithSystemUnzip($file, $path, $fallback) {
  142. return $this->systemUnzipResponse;
  143. }
  144. }