PharArchiverTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\PharArchiver;
  13. class PharArchiverTest extends ArchiverTest
  14. {
  15. public function testTarArchive()
  16. {
  17. // Set up repository
  18. $this->setupDummyRepo();
  19. $package = $this->setupPackage();
  20. $target = $this->getUniqueTmpDirectory().'/composer_archiver_test.tar';
  21. // Test archive
  22. $archiver = new PharArchiver();
  23. $archiver->archive($package->getSourceUrl(), $target, 'tar', array('foo/bar', 'baz', '!/foo/bar/baz'));
  24. $this->assertFileExists($target);
  25. $this->filesystem->removeDirectory(dirname($target));
  26. }
  27. public function testZipArchive()
  28. {
  29. // Set up repository
  30. $this->setupDummyRepo();
  31. $package = $this->setupPackage();
  32. $target = $this->getUniqueTmpDirectory().'/composer_archiver_test.zip';
  33. // Test archive
  34. $archiver = new PharArchiver();
  35. $archiver->archive($package->getSourceUrl(), $target, 'zip');
  36. $this->assertFileExists($target);
  37. $this->filesystem->removeDirectory(dirname($target));
  38. }
  39. /**
  40. * Create a local dummy repository to run tests against!
  41. */
  42. protected function setupDummyRepo()
  43. {
  44. $currentWorkDir = getcwd();
  45. chdir($this->testDir);
  46. $this->writeFile('file.txt', 'content', $currentWorkDir);
  47. $this->writeFile('foo/bar/baz', 'content', $currentWorkDir);
  48. $this->writeFile('foo/bar/ignoreme', 'content', $currentWorkDir);
  49. $this->writeFile('x/baz', 'content', $currentWorkDir);
  50. $this->writeFile('x/includeme', 'content', $currentWorkDir);
  51. chdir($currentWorkDir);
  52. }
  53. protected function writeFile($path, $content, $currentWorkDir)
  54. {
  55. if (!file_exists(dirname($path))) {
  56. mkdir(dirname($path), 0777, true);
  57. }
  58. $result = file_put_contents($path, 'a');
  59. if (false === $result) {
  60. chdir($currentWorkDir);
  61. throw new \RuntimeException('Could not save file.');
  62. }
  63. }
  64. }