ZipDownloaderTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\Package\PackageInterface;
  14. use Composer\TestCase;
  15. use Composer\Util\Filesystem;
  16. class ZipDownloaderTest extends TestCase
  17. {
  18. /**
  19. * @var string
  20. */
  21. private $testDir;
  22. private $prophet;
  23. public function setUp()
  24. {
  25. $this->testDir = $this->getUniqueTmpDirectory();
  26. $this->io = $this->getMock('Composer\IO\IOInterface');
  27. $this->config = $this->getMock('Composer\Config');
  28. }
  29. public function tearDown()
  30. {
  31. $fs = new Filesystem;
  32. $fs->removeDirectory($this->testDir);
  33. ZipDownloader::$hasSystemUnzip = null;
  34. ZipDownloader::$hasZipArchive = null;
  35. }
  36. public function setPrivateProperty($name, $value, $obj = null)
  37. {
  38. $reflectionClass = new \ReflectionClass('Composer\Downloader\ZipDownloader');
  39. $reflectedProperty = $reflectionClass->getProperty($name);
  40. $reflectedProperty->setAccessible(true);
  41. if ($obj === null) {
  42. $reflectedProperty = $reflectedProperty->setValue($value);
  43. } else {
  44. $reflectedProperty = $reflectedProperty->setValue($obj, $value);
  45. }
  46. }
  47. /**
  48. * @group only
  49. */
  50. public function testErrorMessages()
  51. {
  52. if (!class_exists('ZipArchive')) {
  53. $this->markTestSkipped('zip extension missing');
  54. }
  55. $this->config->expects($this->at(0))
  56. ->method('get')
  57. ->with('disable-tls')
  58. ->will($this->returnValue(false));
  59. $this->config->expects($this->at(1))
  60. ->method('get')
  61. ->with('cafile')
  62. ->will($this->returnValue(null));
  63. $this->config->expects($this->at(2))
  64. ->method('get')
  65. ->with('capath')
  66. ->will($this->returnValue(null));
  67. $this->config->expects($this->at(3))
  68. ->method('get')
  69. ->with('vendor-dir')
  70. ->will($this->returnValue($this->testDir));
  71. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  72. $packageMock->expects($this->any())
  73. ->method('getDistUrl')
  74. ->will($this->returnValue($distUrl = 'file://'.__FILE__))
  75. ;
  76. $packageMock->expects($this->any())
  77. ->method('getDistUrls')
  78. ->will($this->returnValue(array($distUrl)))
  79. ;
  80. $packageMock->expects($this->atLeastOnce())
  81. ->method('getTransportOptions')
  82. ->will($this->returnValue(array()))
  83. ;
  84. $downloader = new ZipDownloader($this->io, $this->config);
  85. ZipDownloader::$hasSystemUnzip = false;
  86. try {
  87. $downloader->download($packageMock, sys_get_temp_dir().'/composer-zip-test');
  88. $this->fail('Download of invalid zip files should throw an exception');
  89. } catch (\Exception $e) {
  90. $this->assertContains('is not a zip archive', $e->getMessage());
  91. }
  92. }
  93. /**
  94. * @expectedException \RuntimeException
  95. * @expectedExceptionMessage There was an error extracting the ZIP file
  96. */
  97. public function testZipArchiveOnlyFailed()
  98. {
  99. MockedZipDownloader::$hasSystemUnzip = false;
  100. MockedZipDownloader::$hasZipArchive = true;
  101. $downloader = new MockedZipDownloader($this->io, $this->config);
  102. $zipArchive = $this->getMock('ZipArchive');
  103. $zipArchive->expects($this->at(0))
  104. ->method('open')
  105. ->will($this->returnValue(true));
  106. $zipArchive->expects($this->at(1))
  107. ->method('extractTo')
  108. ->will($this->returnValue(false));
  109. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  110. $downloader->extract('testfile.zip', 'vendor/dir');
  111. }
  112. /**
  113. * @group only
  114. */
  115. public function testZipArchiveOnlyGood()
  116. {
  117. MockedZipDownloader::$hasSystemUnzip = false;
  118. MockedZipDownloader::$hasZipArchive = true;
  119. $downloader = new MockedZipDownloader($this->io, $this->config);
  120. $zipArchive = $this->getMock('ZipArchive');
  121. $zipArchive->expects($this->at(0))
  122. ->method('open')
  123. ->will($this->returnValue(true));
  124. $zipArchive->expects($this->at(1))
  125. ->method('extractTo')
  126. ->will($this->returnValue(true));
  127. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  128. $downloader->extract('testfile.zip', 'vendor/dir');
  129. }
  130. /**
  131. * @expectedException \Exception
  132. * @expectedExceptionMessage Failed to execute unzip
  133. */
  134. public function testSystemUnzipOnlyFailed()
  135. {
  136. MockedZipDownloader::$hasSystemUnzip = true;
  137. MockedZipDownloader::$hasZipArchive = false;
  138. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  139. $processExecutor->expects($this->at(0))
  140. ->method('execute')
  141. ->will($this->returnValue(1));
  142. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  143. $downloader->extract('testfile.zip', 'vendor/dir');
  144. }
  145. public function testSystemUnzipOnlyGood()
  146. {
  147. MockedZipDownloader::$hasSystemUnzip = true;
  148. MockedZipDownloader::$hasZipArchive = false;
  149. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  150. $processExecutor->expects($this->at(0))
  151. ->method('execute')
  152. ->will($this->returnValue(0));
  153. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  154. $downloader->extract('testfile.zip', 'vendor/dir');
  155. }
  156. public function testNonWindowsFallbackGood()
  157. {
  158. MockedZipDownloader::$isWindows = false;
  159. MockedZipDownloader::$hasSystemUnzip = true;
  160. MockedZipDownloader::$hasZipArchive = true;
  161. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  162. $processExecutor->expects($this->at(0))
  163. ->method('execute')
  164. ->will($this->returnValue(1));
  165. $zipArchive = $this->getMock('ZipArchive');
  166. $zipArchive->expects($this->at(0))
  167. ->method('open')
  168. ->will($this->returnValue(true));
  169. $zipArchive->expects($this->at(1))
  170. ->method('extractTo')
  171. ->will($this->returnValue(true));
  172. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  173. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  174. $downloader->extract('testfile.zip', 'vendor/dir');
  175. }
  176. /**
  177. * @expectedException \Exception
  178. * @expectedExceptionMessage There was an error extracting the ZIP file
  179. */
  180. public function testNonWindowsFallbackFailed()
  181. {
  182. MockedZipDownloader::$isWindows = false;
  183. MockedZipDownloader::$hasSystemUnzip = true;
  184. MockedZipDownloader::$hasZipArchive = true;
  185. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  186. $processExecutor->expects($this->at(0))
  187. ->method('execute')
  188. ->will($this->returnValue(1));
  189. $zipArchive = $this->getMock('ZipArchive');
  190. $zipArchive->expects($this->at(0))
  191. ->method('open')
  192. ->will($this->returnValue(true));
  193. $zipArchive->expects($this->at(1))
  194. ->method('extractTo')
  195. ->will($this->returnValue(false));
  196. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  197. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  198. $downloader->extract('testfile.zip', 'vendor/dir');
  199. }
  200. public function testWindowsFallbackGood()
  201. {
  202. MockedZipDownloader::$isWindows = true;
  203. MockedZipDownloader::$hasSystemUnzip = true;
  204. MockedZipDownloader::$hasZipArchive = true;
  205. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  206. $processExecutor->expects($this->atLeastOnce())
  207. ->method('execute')
  208. ->will($this->returnValue(0));
  209. $zipArchive = $this->getMock('ZipArchive');
  210. $zipArchive->expects($this->at(0))
  211. ->method('open')
  212. ->will($this->returnValue(true));
  213. $zipArchive->expects($this->at(1))
  214. ->method('extractTo')
  215. ->will($this->returnValue(false));
  216. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  217. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  218. $downloader->extract('testfile.zip', 'vendor/dir');
  219. }
  220. /**
  221. * @expectedException \Exception
  222. * @expectedExceptionMessage Failed to execute unzip
  223. */
  224. public function testWindowsFallbackFailed()
  225. {
  226. MockedZipDownloader::$isWindows = true;
  227. MockedZipDownloader::$hasSystemUnzip = true;
  228. MockedZipDownloader::$hasZipArchive = true;
  229. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  230. $processExecutor->expects($this->atLeastOnce())
  231. ->method('execute')
  232. ->will($this->returnValue(1));
  233. $zipArchive = $this->getMock('ZipArchive');
  234. $zipArchive->expects($this->at(0))
  235. ->method('open')
  236. ->will($this->returnValue(true));
  237. $zipArchive->expects($this->at(1))
  238. ->method('extractTo')
  239. ->will($this->returnValue(false));
  240. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  241. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  242. $downloader->extract('testfile.zip', 'vendor/dir');
  243. }
  244. }
  245. class MockedZipDownloader extends ZipDownloader
  246. {
  247. public function download(PackageInterface $package, $path, $output = true)
  248. {
  249. return;
  250. }
  251. public function extract($file, $path)
  252. {
  253. parent::extract($file, $path);
  254. }
  255. }