ZipDownloaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. $this->setPrivateProperty('hasSystemUnzip', null);
  34. $this->setPrivateProperty('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. $this->setPrivateProperty('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. $this->setPrivateProperty('hasSystemUnzip', false);
  100. $this->setPrivateProperty('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. * @expectedException \RuntimeException
  114. * @expectedExceptionMessage The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems): Not a directory
  115. */
  116. public function testZipArchiveExtractOnlyFailed()
  117. {
  118. $this->setPrivateProperty('hasSystemUnzip', false);
  119. $this->setPrivateProperty('hasZipArchive', true);
  120. $downloader = new MockedZipDownloader($this->io, $this->config);
  121. $zipArchive = $this->getMock('ZipArchive');
  122. $zipArchive->expects($this->at(0))
  123. ->method('open')
  124. ->will($this->returnValue(true));
  125. $zipArchive->expects($this->at(1))
  126. ->method('extractTo')
  127. ->will($this->throwException(new \ErrorException('Not a directory')));
  128. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  129. $downloader->extract('testfile.zip', 'vendor/dir');
  130. }
  131. /**
  132. * @group only
  133. */
  134. public function testZipArchiveOnlyGood()
  135. {
  136. $this->setPrivateProperty('hasSystemUnzip', false);
  137. $this->setPrivateProperty('hasZipArchive', true);
  138. $downloader = new MockedZipDownloader($this->io, $this->config);
  139. $zipArchive = $this->getMock('ZipArchive');
  140. $zipArchive->expects($this->at(0))
  141. ->method('open')
  142. ->will($this->returnValue(true));
  143. $zipArchive->expects($this->at(1))
  144. ->method('extractTo')
  145. ->will($this->returnValue(true));
  146. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  147. $downloader->extract('testfile.zip', 'vendor/dir');
  148. }
  149. /**
  150. * @expectedException \Exception
  151. * @expectedExceptionMessage Failed to execute unzip
  152. */
  153. public function testSystemUnzipOnlyFailed()
  154. {
  155. $this->setPrivateProperty('hasSystemUnzip', true);
  156. $this->setPrivateProperty('hasZipArchive', false);
  157. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  158. $processExecutor->expects($this->at(0))
  159. ->method('execute')
  160. ->will($this->returnValue(1));
  161. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  162. $downloader->extract('testfile.zip', 'vendor/dir');
  163. }
  164. public function testSystemUnzipOnlyGood()
  165. {
  166. $this->setPrivateProperty('hasSystemUnzip', true);
  167. $this->setPrivateProperty('hasZipArchive', false);
  168. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  169. $processExecutor->expects($this->at(0))
  170. ->method('execute')
  171. ->will($this->returnValue(0));
  172. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  173. $downloader->extract('testfile.zip', 'vendor/dir');
  174. }
  175. public function testNonWindowsFallbackGood()
  176. {
  177. $this->setPrivateProperty('isWindows', false);
  178. $this->setPrivateProperty('hasSystemUnzip', true);
  179. $this->setPrivateProperty('hasZipArchive', true);
  180. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  181. $processExecutor->expects($this->at(0))
  182. ->method('execute')
  183. ->will($this->returnValue(1));
  184. $zipArchive = $this->getMock('ZipArchive');
  185. $zipArchive->expects($this->at(0))
  186. ->method('open')
  187. ->will($this->returnValue(true));
  188. $zipArchive->expects($this->at(1))
  189. ->method('extractTo')
  190. ->will($this->returnValue(true));
  191. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  192. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  193. $downloader->extract('testfile.zip', 'vendor/dir');
  194. }
  195. /**
  196. * @expectedException \Exception
  197. * @expectedExceptionMessage There was an error extracting the ZIP file
  198. */
  199. public function testNonWindowsFallbackFailed()
  200. {
  201. $this->setPrivateProperty('isWindows', false);
  202. $this->setPrivateProperty('hasSystemUnzip', true);
  203. $this->setPrivateProperty('hasZipArchive', true);
  204. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  205. $processExecutor->expects($this->at(0))
  206. ->method('execute')
  207. ->will($this->returnValue(1));
  208. $zipArchive = $this->getMock('ZipArchive');
  209. $zipArchive->expects($this->at(0))
  210. ->method('open')
  211. ->will($this->returnValue(true));
  212. $zipArchive->expects($this->at(1))
  213. ->method('extractTo')
  214. ->will($this->returnValue(false));
  215. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  216. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  217. $downloader->extract('testfile.zip', 'vendor/dir');
  218. }
  219. public function testWindowsFallbackGood()
  220. {
  221. $this->setPrivateProperty('isWindows', true);
  222. $this->setPrivateProperty('hasSystemUnzip', true);
  223. $this->setPrivateProperty('hasZipArchive', true);
  224. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  225. $processExecutor->expects($this->atLeastOnce())
  226. ->method('execute')
  227. ->will($this->returnValue(0));
  228. $zipArchive = $this->getMock('ZipArchive');
  229. $zipArchive->expects($this->at(0))
  230. ->method('open')
  231. ->will($this->returnValue(true));
  232. $zipArchive->expects($this->at(1))
  233. ->method('extractTo')
  234. ->will($this->returnValue(false));
  235. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  236. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  237. $downloader->extract('testfile.zip', 'vendor/dir');
  238. }
  239. /**
  240. * @expectedException \Exception
  241. * @expectedExceptionMessage Failed to execute unzip
  242. */
  243. public function testWindowsFallbackFailed()
  244. {
  245. $this->setPrivateProperty('isWindows', true);
  246. $this->setPrivateProperty('hasSystemUnzip', true);
  247. $this->setPrivateProperty('hasZipArchive', true);
  248. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  249. $processExecutor->expects($this->atLeastOnce())
  250. ->method('execute')
  251. ->will($this->returnValue(1));
  252. $zipArchive = $this->getMock('ZipArchive');
  253. $zipArchive->expects($this->at(0))
  254. ->method('open')
  255. ->will($this->returnValue(true));
  256. $zipArchive->expects($this->at(1))
  257. ->method('extractTo')
  258. ->will($this->returnValue(false));
  259. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  260. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  261. $downloader->extract('testfile.zip', 'vendor/dir');
  262. }
  263. }
  264. class MockedZipDownloader extends ZipDownloader
  265. {
  266. public function download(PackageInterface $package, $path, $output = true)
  267. {
  268. return;
  269. }
  270. public function extract($file, $path)
  271. {
  272. parent::extract($file, $path);
  273. }
  274. }