AllFunctionalTest.php 5.3 KB

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