ArchiverTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\MemoryPackage;
  13. use Composer\Util\Filesystem;
  14. use Composer\Util\ProcessExecutor;
  15. abstract class ArchiverTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var \Composer\Util\Filesystem
  19. */
  20. protected $fs;
  21. /**
  22. * @var \Composer\Util\ProcessExecutor
  23. */
  24. protected $process;
  25. /**
  26. * @var string
  27. */
  28. protected $testdir = '';
  29. public function setUp()
  30. {
  31. $this->fs = new Filesystem;
  32. $this->process = new ProcessExecutor;
  33. $this->testdir = sys_get_temp_dir() . '/composer_archivertest_git_repository' . mt_rand();
  34. }
  35. protected function getTestDir()
  36. {
  37. return $this->testdir;
  38. }
  39. /**
  40. * Create local git repository to run tests against!
  41. */
  42. protected function setupGitRepo()
  43. {
  44. $td = $this->getTestDir();
  45. $this->fs->removeDirectory($td);
  46. $this->fs->ensureDirectoryExists($td);
  47. $currentWorkDir = getcwd();
  48. chdir($td);
  49. $result = $this->process->execute("git init -q");
  50. if ($result > 0) {
  51. throw new \RuntimeException(
  52. "Could not init: " . $this->process->getErrorOutput());
  53. }
  54. $result = file_put_contents('b', 'a');
  55. if (false === $result) {
  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. throw new \RuntimeException(
  61. "Could not init: " . $this->process->getErrorOutput());
  62. }
  63. chdir($currentWorkDir);
  64. }
  65. protected function removeGitRepo()
  66. {
  67. $td = $this->getTestDir();
  68. $this->fs->removeDirectory($td);
  69. }
  70. protected function setupPackage()
  71. {
  72. $td = $this->getTestDir();
  73. $package = new MemoryPackage('archivertest/archivertest', 'master', 'master');
  74. $package->setSourceUrl("file://$td");
  75. $package->setSourceReference('master');
  76. $package->setSourceType('git');
  77. return $package;
  78. }
  79. protected function getPackageFileName(MemoryPackage $package)
  80. {
  81. return $package->getVersion();
  82. }
  83. }