AllFunctionalTest.php 6.6 KB

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