ZipDownloaderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. if (!class_exists('ZipArchive')) {
  100. $this->markTestSkipped('zip extension missing');
  101. }
  102. $this->setPrivateProperty('hasSystemUnzip', false);
  103. $this->setPrivateProperty('hasZipArchive', true);
  104. $downloader = new MockedZipDownloader($this->io, $this->config);
  105. $zipArchive = $this->getMock('ZipArchive');
  106. $zipArchive->expects($this->at(0))
  107. ->method('open')
  108. ->will($this->returnValue(true));
  109. $zipArchive->expects($this->at(1))
  110. ->method('extractTo')
  111. ->will($this->returnValue(false));
  112. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  113. $downloader->extract('testfile.zip', 'vendor/dir');
  114. }
  115. /**
  116. * @expectedException \RuntimeException
  117. * @expectedExceptionMessage The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems): Not a directory
  118. */
  119. public function testZipArchiveExtractOnlyFailed()
  120. {
  121. if (!class_exists('ZipArchive')) {
  122. $this->markTestSkipped('zip extension missing');
  123. }
  124. $this->setPrivateProperty('hasSystemUnzip', false);
  125. $this->setPrivateProperty('hasZipArchive', true);
  126. $downloader = new MockedZipDownloader($this->io, $this->config);
  127. $zipArchive = $this->getMock('ZipArchive');
  128. $zipArchive->expects($this->at(0))
  129. ->method('open')
  130. ->will($this->returnValue(true));
  131. $zipArchive->expects($this->at(1))
  132. ->method('extractTo')
  133. ->will($this->throwException(new \ErrorException('Not a directory')));
  134. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  135. $downloader->extract('testfile.zip', 'vendor/dir');
  136. }
  137. /**
  138. * @group only
  139. */
  140. public function testZipArchiveOnlyGood()
  141. {
  142. if (!class_exists('ZipArchive')) {
  143. $this->markTestSkipped('zip extension missing');
  144. }
  145. $this->setPrivateProperty('hasSystemUnzip', false);
  146. $this->setPrivateProperty('hasZipArchive', true);
  147. $downloader = new MockedZipDownloader($this->io, $this->config);
  148. $zipArchive = $this->getMock('ZipArchive');
  149. $zipArchive->expects($this->at(0))
  150. ->method('open')
  151. ->will($this->returnValue(true));
  152. $zipArchive->expects($this->at(1))
  153. ->method('extractTo')
  154. ->will($this->returnValue(true));
  155. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  156. $downloader->extract('testfile.zip', 'vendor/dir');
  157. }
  158. /**
  159. * @expectedException \Exception
  160. * @expectedExceptionMessage Failed to execute unzip
  161. */
  162. public function testSystemUnzipOnlyFailed()
  163. {
  164. if (!class_exists('ZipArchive')) {
  165. $this->markTestSkipped('zip extension missing');
  166. }
  167. $this->setPrivateProperty('hasSystemUnzip', true);
  168. $this->setPrivateProperty('hasZipArchive', false);
  169. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  170. $processExecutor->expects($this->at(0))
  171. ->method('execute')
  172. ->will($this->returnValue(1));
  173. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  174. $downloader->extract('testfile.zip', 'vendor/dir');
  175. }
  176. public function testSystemUnzipOnlyGood()
  177. {
  178. if (!class_exists('ZipArchive')) {
  179. $this->markTestSkipped('zip extension missing');
  180. }
  181. $this->setPrivateProperty('hasSystemUnzip', true);
  182. $this->setPrivateProperty('hasZipArchive', false);
  183. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  184. $processExecutor->expects($this->at(0))
  185. ->method('execute')
  186. ->will($this->returnValue(0));
  187. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  188. $downloader->extract('testfile.zip', 'vendor/dir');
  189. }
  190. public function testNonWindowsFallbackGood()
  191. {
  192. if (!class_exists('ZipArchive')) {
  193. $this->markTestSkipped('zip extension missing');
  194. }
  195. $this->setPrivateProperty('isWindows', false);
  196. $this->setPrivateProperty('hasSystemUnzip', true);
  197. $this->setPrivateProperty('hasZipArchive', true);
  198. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  199. $processExecutor->expects($this->at(0))
  200. ->method('execute')
  201. ->will($this->returnValue(1));
  202. $zipArchive = $this->getMock('ZipArchive');
  203. $zipArchive->expects($this->at(0))
  204. ->method('open')
  205. ->will($this->returnValue(true));
  206. $zipArchive->expects($this->at(1))
  207. ->method('extractTo')
  208. ->will($this->returnValue(true));
  209. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  210. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  211. $downloader->extract('testfile.zip', 'vendor/dir');
  212. }
  213. /**
  214. * @expectedException \Exception
  215. * @expectedExceptionMessage There was an error extracting the ZIP file
  216. */
  217. public function testNonWindowsFallbackFailed()
  218. {
  219. if (!class_exists('ZipArchive')) {
  220. $this->markTestSkipped('zip extension missing');
  221. }
  222. $this->setPrivateProperty('isWindows', false);
  223. $this->setPrivateProperty('hasSystemUnzip', true);
  224. $this->setPrivateProperty('hasZipArchive', true);
  225. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  226. $processExecutor->expects($this->at(0))
  227. ->method('execute')
  228. ->will($this->returnValue(1));
  229. $zipArchive = $this->getMock('ZipArchive');
  230. $zipArchive->expects($this->at(0))
  231. ->method('open')
  232. ->will($this->returnValue(true));
  233. $zipArchive->expects($this->at(1))
  234. ->method('extractTo')
  235. ->will($this->returnValue(false));
  236. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  237. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  238. $downloader->extract('testfile.zip', 'vendor/dir');
  239. }
  240. public function testWindowsFallbackGood()
  241. {
  242. if (!class_exists('ZipArchive')) {
  243. $this->markTestSkipped('zip extension missing');
  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(0));
  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. * @expectedException \Exception
  265. * @expectedExceptionMessage Failed to execute unzip
  266. */
  267. public function testWindowsFallbackFailed()
  268. {
  269. if (!class_exists('ZipArchive')) {
  270. $this->markTestSkipped('zip extension missing');
  271. }
  272. $this->setPrivateProperty('isWindows', true);
  273. $this->setPrivateProperty('hasSystemUnzip', true);
  274. $this->setPrivateProperty('hasZipArchive', true);
  275. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  276. $processExecutor->expects($this->atLeastOnce())
  277. ->method('execute')
  278. ->will($this->returnValue(1));
  279. $zipArchive = $this->getMock('ZipArchive');
  280. $zipArchive->expects($this->at(0))
  281. ->method('open')
  282. ->will($this->returnValue(true));
  283. $zipArchive->expects($this->at(1))
  284. ->method('extractTo')
  285. ->will($this->returnValue(false));
  286. $downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
  287. $this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
  288. $downloader->extract('testfile.zip', 'vendor/dir');
  289. }
  290. }
  291. class MockedZipDownloader extends ZipDownloader
  292. {
  293. public function download(PackageInterface $package, $path, $output = true)
  294. {
  295. return;
  296. }
  297. public function extract($file, $path)
  298. {
  299. parent::extract($file, $path);
  300. }
  301. }