ZipDownloaderTest.php 12 KB

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