PharArchiverTest.php 2.3 KB

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