AllFunctionalTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Composer\Test;
  3. use Symfony\Component\Process\Process;
  4. use Composer\Util\Filesystem;
  5. use Symfony\Component\Finder\Finder;
  6. /**
  7. * @group slow
  8. */
  9. class AllFunctionalTest extends \PHPUnit_Framework_TestCase
  10. {
  11. protected $oldcwd;
  12. protected $oldenv;
  13. protected $testDir;
  14. public function setUp()
  15. {
  16. $this->oldcwd = getcwd();
  17. chdir(__DIR__.'/Fixtures/functional');
  18. }
  19. public function tearDown()
  20. {
  21. chdir($this->oldcwd);
  22. $fs = new Filesystem;
  23. if ($this->testDir) {
  24. $fs->removeDirectory($this->testDir);
  25. $this->testDir = null;
  26. }
  27. if ($this->oldenv) {
  28. $fs->removeDirectory(getenv('COMPOSER_HOME'));
  29. putenv('COMPOSER_HOME='.$this->oldenv);
  30. $this->oldenv = null;
  31. }
  32. }
  33. /**
  34. * @dataProvider getTestFiles
  35. */
  36. public function testIntegration(\SplFileInfo $testFile)
  37. {
  38. $testData = $this->parseTestFile($testFile);
  39. $this->oldenv = getenv('COMPOSER_HOME');
  40. putenv('COMPOSER_HOME='.$this->testDir.'home');
  41. $cmd = 'php '.__DIR__.'/../../../bin/composer --no-ansi '.$testData['RUN'];
  42. $proc = new Process($cmd);
  43. $exitcode = $proc->run();
  44. if (isset($testData['EXPECT'])) {
  45. $this->assertEquals($testData['EXPECT'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  46. }
  47. if (isset($testData['EXPECT-REGEX'])) {
  48. $this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  49. }
  50. if (isset($testData['EXPECT-ERROR'])) {
  51. $this->assertEquals($testData['EXPECT-ERROR'], $this->cleanOutput($proc->getErrorOutput()));
  52. }
  53. if (isset($testData['EXPECT-ERROR-REGEX'])) {
  54. $this->assertRegExp($testData['EXPECT-ERROR-REGEX'], $this->cleanOutput($proc->getErrorOutput()));
  55. }
  56. if (isset($testData['EXPECT-EXIT-CODE'])) {
  57. $this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode);
  58. }
  59. }
  60. public function getTestFiles()
  61. {
  62. $tests = array();
  63. foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
  64. $tests[] = array($file);
  65. }
  66. return $tests;
  67. }
  68. private function parseTestFile(\SplFileInfo $file)
  69. {
  70. $tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
  71. $data = array();
  72. $section = null;
  73. $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true);
  74. $this->testDir = $testDir;
  75. $varRegex = '#%([a-zA-Z_-]+)%#';
  76. $variableReplacer = function($match) use (&$data, $testDir) {
  77. list(, $var) = $match;
  78. switch ($var) {
  79. case 'testDir':
  80. $data['test_dir'] = $testDir;
  81. return $testDir;
  82. default:
  83. throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var));
  84. }
  85. };
  86. for ($i = 0, $c = count($tokens); $i < $c; $i++) {
  87. if ('' === $tokens[$i] && null === $section) {
  88. continue;
  89. }
  90. // Handle section headers.
  91. if (null === $section) {
  92. $section = $tokens[$i];
  93. continue;
  94. }
  95. $sectionData = $tokens[$i];
  96. // Allow sections to validate, or modify their section data.
  97. switch ($section) {
  98. case 'RUN':
  99. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  100. break;
  101. case 'EXPECT-EXIT-CODE':
  102. $sectionData = (integer) $sectionData;
  103. case 'EXPECT':
  104. case 'EXPECT-REGEX':
  105. case 'EXPECT-ERROR':
  106. case 'EXPECT-ERROR-REGEX':
  107. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  108. break;
  109. default:
  110. throw new \RuntimeException(sprintf(
  111. 'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-ERROR", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-ERROR-REGEX". '
  112. .'Section headers must be written as "--HEADER_NAME--".',
  113. $section
  114. ));
  115. }
  116. $data[$section] = $sectionData;
  117. $section = $sectionData = null;
  118. }
  119. // validate data
  120. if (!isset($data['RUN'])) {
  121. throw new \RuntimeException('The test file must have a section named "RUN".');
  122. }
  123. if (!isset($data['EXPECT']) && !isset($data['EXPECT-ERROR']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-ERROR-REGEX'])) {
  124. throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-ERROR", "EXPECT-REGEX", or "EXPECT-ERROR-REGEX".');
  125. }
  126. return $data;
  127. }
  128. private function cleanOutput($output)
  129. {
  130. $processed = '';
  131. for ($i = 0; $i < strlen($output); $i++) {
  132. if ($output[$i] === "\x08") {
  133. $processed = substr($processed, 0, -1);
  134. } elseif ($output[$i] !== "\r") {
  135. $processed .= $output[$i];
  136. }
  137. }
  138. return $processed;
  139. }
  140. }