AllFunctionalTest.php 6.3 KB

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