AllFunctionalTest.php 6.2 KB

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