AllFunctionalTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. $target = dirname(self::$pharPath);
  59. $fs = new Filesystem;
  60. $fs->removeDirectory($target);
  61. $fs->ensureDirectoryExists($target);
  62. chdir($target);
  63. $it = new \RecursiveDirectoryIterator(__DIR__.'/../../../', \RecursiveDirectoryIterator::SKIP_DOTS);
  64. $ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
  65. foreach ($ri as $file) {
  66. $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
  67. if ($file->isDir()) {
  68. $fs->ensureDirectoryExists($targetPath);
  69. } else {
  70. copy($file->getPathname(), $targetPath);
  71. }
  72. }
  73. $proc = new Process('php '.escapeshellarg('./bin/compile'), $target);
  74. $exitcode = $proc->run();
  75. if ($exitcode !== 0 || trim($proc->getOutput())) {
  76. $this->fail($proc->getOutput());
  77. }
  78. $this->assertTrue(file_exists(self::$pharPath));
  79. }
  80. /**
  81. * @dataProvider getTestFiles
  82. * @depends testBuildPhar
  83. */
  84. public function testIntegration(\SplFileInfo $testFile)
  85. {
  86. $testData = $this->parseTestFile($testFile);
  87. $this->oldenv = getenv('COMPOSER_HOME');
  88. $_SERVER['COMPOSER_HOME'] = $this->testDir.'home';
  89. putenv('COMPOSER_HOME='.$_SERVER['COMPOSER_HOME']);
  90. $cmd = 'php '.escapeshellarg(self::$pharPath).' --no-ansi '.$testData['RUN'];
  91. $proc = new Process($cmd, __DIR__.'/Fixtures/functional', null, null, 300);
  92. $exitcode = $proc->run();
  93. if (isset($testData['EXPECT'])) {
  94. $this->assertEquals($testData['EXPECT'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  95. }
  96. if (isset($testData['EXPECT-REGEX'])) {
  97. $this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  98. }
  99. if (isset($testData['EXPECT-ERROR'])) {
  100. $this->assertEquals($testData['EXPECT-ERROR'], $this->cleanOutput($proc->getErrorOutput()));
  101. }
  102. if (isset($testData['EXPECT-ERROR-REGEX'])) {
  103. $this->assertRegExp($testData['EXPECT-ERROR-REGEX'], $this->cleanOutput($proc->getErrorOutput()));
  104. }
  105. if (isset($testData['EXPECT-EXIT-CODE'])) {
  106. $this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode);
  107. }
  108. }
  109. public function getTestFiles()
  110. {
  111. $tests = array();
  112. foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
  113. $tests[] = array($file);
  114. }
  115. return $tests;
  116. }
  117. private function parseTestFile(\SplFileInfo $file)
  118. {
  119. $tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
  120. $data = array();
  121. $section = null;
  122. $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true);
  123. $this->testDir = $testDir;
  124. $varRegex = '#%([a-zA-Z_-]+)%#';
  125. $variableReplacer = function ($match) use (&$data, $testDir) {
  126. list(, $var) = $match;
  127. switch ($var) {
  128. case 'testDir':
  129. $data['test_dir'] = $testDir;
  130. return $testDir;
  131. default:
  132. throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var));
  133. }
  134. };
  135. for ($i = 0, $c = count($tokens); $i < $c; $i++) {
  136. if ('' === $tokens[$i] && null === $section) {
  137. continue;
  138. }
  139. // Handle section headers.
  140. if (null === $section) {
  141. $section = $tokens[$i];
  142. continue;
  143. }
  144. $sectionData = $tokens[$i];
  145. // Allow sections to validate, or modify their section data.
  146. switch ($section) {
  147. case 'RUN':
  148. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  149. break;
  150. case 'EXPECT-EXIT-CODE':
  151. $sectionData = (integer) $sectionData;
  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. }