ArchiverTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Package;
  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. chdir($currentWorkDir);
  51. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  52. }
  53. $result = file_put_contents('b', 'a');
  54. if (false === $result) {
  55. chdir($currentWorkDir);
  56. throw new \RuntimeException('Could not save file.');
  57. }
  58. $result = $this->process->execute('git add b && git commit -m "commit b" -q');
  59. if ($result > 0) {
  60. chdir($currentWorkDir);
  61. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  62. }
  63. chdir($currentWorkDir);
  64. }
  65. protected function removeGitRepo()
  66. {
  67. $this->filesystem->removeDirectory($this->testDir);
  68. }
  69. protected function setupPackage()
  70. {
  71. $package = new Package('archivertest/archivertest', 'master', 'master');
  72. $package->setSourceUrl(realpath($this->testDir));
  73. $package->setSourceReference('master');
  74. $package->setSourceType('git');
  75. return $package;
  76. }
  77. }