AllFunctionalTest.php 6.8 KB

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