ArchiverTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Util\Filesystem;
  13. use Composer\Util\ProcessExecutor;
  14. use Composer\Package\MemoryPackage;
  15. /**
  16. * @author Till Klampaeckel <till@php.net>
  17. * @author Matthieu Moquet <matthieu@moquet.net>
  18. */
  19. abstract class ArchiverTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var \Composer\Util\Filesystem
  23. */
  24. protected $filesystem;
  25. /**
  26. * @var \Composer\Util\ProcessExecutor
  27. */
  28. protected $process;
  29. /**
  30. * @var string
  31. */
  32. protected $testDir;
  33. public function setUp()
  34. {
  35. $this->filesystem = new Filesystem();
  36. $this->process = new ProcessExecutor();
  37. $this->testDir = sys_get_temp_dir().'/composer_archivertest_git_repository'.mt_rand();
  38. }
  39. /**
  40. * Create local git repository to run tests against!
  41. */
  42. protected function setupGitRepo()
  43. {
  44. $this->filesystem->removeDirectory($this->testDir);
  45. $this->filesystem->ensureDirectoryExists($this->testDir);
  46. $currentWorkDir = getcwd();
  47. chdir($this->testDir);
  48. $result = $this->process->execute('git init -q');
  49. if ($result > 0) {
  50. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  51. }
  52. $result = file_put_contents('b', 'a');
  53. if (false === $result) {
  54. throw new \RuntimeException('Could not save file.');
  55. }
  56. $result = $this->process->execute('git add b && git commit -m "commit b" -q');
  57. if ($result > 0) {
  58. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  59. }
  60. chdir($currentWorkDir);
  61. }
  62. protected function removeGitRepo()
  63. {
  64. $this->filesystem->removeDirectory($this->testDir);
  65. }
  66. protected function setupPackage()
  67. {
  68. $package = new MemoryPackage('archivertest/archivertest', 'master', 'master');
  69. $package->setSourceUrl(realpath($this->testDir));
  70. $package->setSourceReference('master');
  71. $package->setSourceType('git');
  72. return $package;
  73. }
  74. }