ZipArchiverTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Package\Archiver;
  12. use Composer\Package\Archiver\ZipArchiver;
  13. class ZipArchiverTest extends ArchiverTest
  14. {
  15. public function testZipArchive()
  16. {
  17. if (!class_exists('ZipArchive')) {
  18. $this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".');
  19. }
  20. // Set up repository
  21. $this->setupDummyRepo();
  22. $package = $this->setupPackage();
  23. $target = sys_get_temp_dir().'/composer_archiver_test.zip';
  24. // Test archive
  25. $archiver = new ZipArchiver();
  26. $archiver->archive($package->getSourceUrl(), $target, 'zip');
  27. $this->assertFileExists($target);
  28. unlink($target);
  29. }
  30. /**
  31. * Create a local dummy repository to run tests against!
  32. */
  33. protected function setupDummyRepo()
  34. {
  35. $currentWorkDir = getcwd();
  36. chdir($this->testDir);
  37. $this->writeFile('file.txt', 'content', $currentWorkDir);
  38. $this->writeFile('foo/bar/baz', 'content', $currentWorkDir);
  39. $this->writeFile('foo/bar/ignoreme', 'content', $currentWorkDir);
  40. $this->writeFile('x/baz', 'content', $currentWorkDir);
  41. $this->writeFile('x/includeme', 'content', $currentWorkDir);
  42. chdir($currentWorkDir);
  43. }
  44. protected function writeFile($path, $content, $currentWorkDir)
  45. {
  46. if (!file_exists(dirname($path))) {
  47. mkdir(dirname($path), 0777, true);
  48. }
  49. $result = file_put_contents($path, 'a');
  50. if (false === $result) {
  51. chdir($currentWorkDir);
  52. throw new \RuntimeException('Could not save file.');
  53. }
  54. }
  55. }