AllFunctionalTest.php 4.9 KB

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