AllFunctionalTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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;
  12. use Composer\Test\TestCase;
  13. use Composer\Util\Filesystem;
  14. use Symfony\Component\Finder\Finder;
  15. use Symfony\Component\Process\Process;
  16. /**
  17. * @group slow
  18. */
  19. class AllFunctionalTest extends TestCase
  20. {
  21. protected $oldcwd;
  22. protected $oldenv;
  23. protected $testDir;
  24. private static $pharPath;
  25. public function setUp()
  26. {
  27. $this->oldcwd = getcwd();
  28. chdir(__DIR__.'/Fixtures/functional');
  29. }
  30. public function tearDown()
  31. {
  32. chdir($this->oldcwd);
  33. $fs = new Filesystem;
  34. if ($this->testDir) {
  35. $fs->removeDirectory($this->testDir);
  36. $this->testDir = null;
  37. }
  38. if ($this->oldenv) {
  39. $fs->removeDirectory(getenv('COMPOSER_HOME'));
  40. $_SERVER['COMPOSER_HOME'] = $this->oldenv;
  41. putenv('COMPOSER_HOME='.$_SERVER['COMPOSER_HOME']);
  42. $this->oldenv = null;
  43. }
  44. }
  45. public static function setUpBeforeClass()
  46. {
  47. self::$pharPath = self::getUniqueTmpDirectory() . '/composer.phar';
  48. }
  49. public static function tearDownAfterClass()
  50. {
  51. $fs = new Filesystem;
  52. $fs->removeDirectory(dirname(self::$pharPath));
  53. }
  54. public function testBuildPhar()
  55. {
  56. if (defined('HHVM_VERSION')) {
  57. $this->markTestSkipped('Building the phar does not work on HHVM.');
  58. }
  59. $target = dirname(self::$pharPath);
  60. $fs = new Filesystem();
  61. chdir($target);
  62. $it = new \RecursiveDirectoryIterator(__DIR__.'/../../../', \RecursiveDirectoryIterator::SKIP_DOTS);
  63. $ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
  64. foreach ($ri as $file) {
  65. $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
  66. if ($file->isDir()) {
  67. $fs->ensureDirectoryExists($targetPath);
  68. } else {
  69. copy($file->getPathname(), $targetPath);
  70. }
  71. }
  72. $proc = new Process('php '.escapeshellarg('./bin/compile'), $target);
  73. $exitcode = $proc->run();
  74. if ($exitcode !== 0 || trim($proc->getOutput())) {
  75. $this->fail($proc->getOutput());
  76. }
  77. $this->assertFileExists(self::$pharPath);
  78. }
  79. /**
  80. * @dataProvider getTestFiles
  81. * @depends testBuildPhar
  82. */
  83. public function testIntegration(\SplFileInfo $testFile)
  84. {
  85. $testData = $this->parseTestFile($testFile);
  86. $this->oldenv = getenv('COMPOSER_HOME');
  87. $_SERVER['COMPOSER_HOME'] = $this->testDir.'home';
  88. putenv('COMPOSER_HOME='.$_SERVER['COMPOSER_HOME']);
  89. $cmd = 'php '.escapeshellarg(self::$pharPath).' --no-ansi '.$testData['RUN'];
  90. $proc = new Process($cmd, __DIR__.'/Fixtures/functional', null, null, 300);
  91. $exitcode = $proc->run();
  92. if (isset($testData['EXPECT'])) {
  93. $this->assertEquals($testData['EXPECT'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  94. }
  95. if (isset($testData['EXPECT-REGEX'])) {
  96. $this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  97. }
  98. if (isset($testData['EXPECT-ERROR'])) {
  99. $this->assertEquals($testData['EXPECT-ERROR'], $this->cleanOutput($proc->getErrorOutput()));
  100. }
  101. if (isset($testData['EXPECT-ERROR-REGEX'])) {
  102. $this->assertRegExp($testData['EXPECT-ERROR-REGEX'], $this->cleanOutput($proc->getErrorOutput()));
  103. }
  104. if (isset($testData['EXPECT-EXIT-CODE'])) {
  105. $this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode);
  106. }
  107. }
  108. public function getTestFiles()
  109. {
  110. $tests = array();
  111. foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
  112. $tests[] = array($file);
  113. }
  114. return $tests;
  115. }
  116. private function parseTestFile(\SplFileInfo $file)
  117. {
  118. $tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
  119. $data = array();
  120. $section = null;
  121. $testDir = self::getUniqueTmpDirectory();
  122. $this->testDir = $testDir;
  123. $varRegex = '#%([a-zA-Z_-]+)%#';
  124. $variableReplacer = function ($match) use (&$data, $testDir) {
  125. list(, $var) = $match;
  126. switch ($var) {
  127. case 'testDir':
  128. $data['test_dir'] = $testDir;
  129. return $testDir;
  130. default:
  131. throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var));
  132. }
  133. };
  134. foreach ($tokens as $token) {
  135. if ('' === $token && null === $section) {
  136. continue;
  137. }
  138. // Handle section headers.
  139. if (null === $section) {
  140. $section = $token;
  141. continue;
  142. }
  143. $sectionData = $token;
  144. // Allow sections to validate, or modify their section data.
  145. switch ($section) {
  146. case 'RUN':
  147. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  148. break;
  149. case 'EXPECT-EXIT-CODE':
  150. $sectionData = (int) $sectionData;
  151. break;
  152. case 'EXPECT':
  153. case 'EXPECT-REGEX':
  154. case 'EXPECT-ERROR':
  155. case 'EXPECT-ERROR-REGEX':
  156. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  157. break;
  158. default:
  159. throw new \RuntimeException(sprintf(
  160. 'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-ERROR", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-ERROR-REGEX". '
  161. .'Section headers must be written as "--HEADER_NAME--".',
  162. $section
  163. ));
  164. }
  165. $data[$section] = $sectionData;
  166. $section = $sectionData = null;
  167. }
  168. // validate data
  169. if (!isset($data['RUN'])) {
  170. throw new \RuntimeException('The test file must have a section named "RUN".');
  171. }
  172. if (!isset($data['EXPECT']) && !isset($data['EXPECT-ERROR']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-ERROR-REGEX'])) {
  173. throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-ERROR", "EXPECT-REGEX", or "EXPECT-ERROR-REGEX".');
  174. }
  175. return $data;
  176. }
  177. private function cleanOutput($output)
  178. {
  179. $processed = '';
  180. for ($i = 0; $i < strlen($output); $i++) {
  181. if ($output[$i] === "\x08") {
  182. $processed = substr($processed, 0, -1);
  183. } elseif ($output[$i] !== "\r") {
  184. $processed .= $output[$i];
  185. }
  186. }
  187. return $processed;
  188. }
  189. }