AllFunctionalTest.php 6.9 KB

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