ZipDownloaderTest.php 12 KB

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