AllFunctionalTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public function setUp()
  13. {
  14. $this->oldcwd = getcwd();
  15. chdir(__DIR__.'/Fixtures/functional');
  16. }
  17. public function tearDown()
  18. {
  19. chdir($this->oldcwd);
  20. }
  21. /**
  22. * @dataProvider getTestFiles
  23. */
  24. public function testIntegration(\SplFileInfo $testFile)
  25. {
  26. $testData = $this->parseTestFile($testFile);
  27. $cmd = 'php '.__DIR__.'/../../../bin/composer --no-ansi '.$testData['RUN'];
  28. $proc = new Process($cmd);
  29. $exitcode = $proc->run();
  30. if (isset($testData['EXPECT'])) {
  31. $this->assertEquals($testData['EXPECT'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  32. }
  33. if (isset($testData['EXPECT-REGEX'])) {
  34. $this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
  35. }
  36. if (isset($testData['EXPECT-ERROR'])) {
  37. $this->assertEquals($testData['EXPECT-ERROR'], $this->cleanOutput($proc->getErrorOutput()));
  38. }
  39. if (isset($testData['EXPECT-ERROR-REGEX'])) {
  40. $this->assertRegExp($testData['EXPECT-ERROR-REGEX'], $this->cleanOutput($proc->getErrorOutput()));
  41. }
  42. if (isset($testData['EXPECT-EXIT-CODE'])) {
  43. $this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode);
  44. }
  45. // Clean up.
  46. $fs = new Filesystem();
  47. if (isset($testData['test_dir']) && is_dir($testData['test_dir'])) {
  48. $fs->removeDirectory($testData['test_dir']);
  49. }
  50. }
  51. public function getTestFiles()
  52. {
  53. $tests = array();
  54. foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
  55. $tests[] = array($file);
  56. }
  57. return $tests;
  58. }
  59. private function parseTestFile(\SplFileInfo $file)
  60. {
  61. $tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
  62. $data = array();
  63. $section = null;
  64. $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true);
  65. $varRegex = '#%([a-zA-Z_-]+)%#';
  66. $variableReplacer = function($match) use (&$data, $testDir) {
  67. list(, $var) = $match;
  68. switch ($var) {
  69. case 'testDir':
  70. $data['test_dir'] = $testDir;
  71. return $testDir;
  72. default:
  73. throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var));
  74. }
  75. };
  76. for ($i = 0, $c = count($tokens); $i < $c; $i++) {
  77. if ('' === $tokens[$i] && null === $section) {
  78. continue;
  79. }
  80. // Handle section headers.
  81. if (null === $section) {
  82. $section = $tokens[$i];
  83. continue;
  84. }
  85. $sectionData = $tokens[$i];
  86. // Allow sections to validate, or modify their section data.
  87. switch ($section) {
  88. case 'RUN':
  89. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  90. break;
  91. case 'EXPECT-EXIT-CODE':
  92. $sectionData = (integer) $sectionData;
  93. case 'EXPECT':
  94. case 'EXPECT-REGEX':
  95. case 'EXPECT-ERROR':
  96. case 'EXPECT-ERROR-REGEX':
  97. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  98. break;
  99. default:
  100. throw new \RuntimeException(sprintf(
  101. 'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-ERROR", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-ERROR-REGEX". '
  102. .'Section headers must be written as "--HEADER_NAME--".',
  103. $section
  104. ));
  105. }
  106. $data[$section] = $sectionData;
  107. $section = $sectionData = null;
  108. }
  109. // validate data
  110. if (!isset($data['RUN'])) {
  111. throw new \RuntimeException('The test file must have a section named "RUN".');
  112. }
  113. if (!isset($data['EXPECT']) && !isset($data['EXPECT-ERROR']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-ERROR-REGEX'])) {
  114. throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-ERROR", "EXPECT-REGEX", or "EXPECT-ERROR-REGEX".');
  115. }
  116. return $data;
  117. }
  118. private function cleanOutput($output)
  119. {
  120. $processed = '';
  121. for ($i = 0; $i < strlen($output); $i++) {
  122. if ($output[$i] === "\x08") {
  123. $processed = substr($processed, 0, -1);
  124. } elseif ($output[$i] !== "\r") {
  125. $processed .= $output[$i];
  126. }
  127. }
  128. return $processed;
  129. }
  130. }