ZipArchiverTest.php 1.7 KB

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