AllFunctionalTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Composer\Test\Functional;
  3. use Symfony\Component\Process\Process;
  4. use Composer\Util\Filesystem;
  5. use Symfony\Component\Finder\Finder;
  6. class AllFunctionalTest extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @dataProvider getTestFiles
  10. */
  11. public function testIntegration(\SplFileInfo $testFile)
  12. {
  13. $testData = $this->parseTestFile($testFile);
  14. $cmd = 'php '.__DIR__.'/../../../../bin/composer '.$testData['command'];
  15. $proc = new Process($cmd);
  16. $exitcode = $proc->run();
  17. if (isset($testData['output'])) {
  18. $this->assertEquals($testData['output'], $proc->getOutput());
  19. }
  20. if (isset($testData['outputregex'])) {
  21. $this->assertRegExp($testData['outputregex'], $proc->getOutput());
  22. }
  23. if (isset($testData['erroroutput'])) {
  24. $this->assertEquals($testData['erroroutput'], $proc->getErrorOutput());
  25. }
  26. if (isset($testData['erroroutputregex'])) {
  27. $this->assertRegExp($testData['erroroutputregex'], $proc->getErrorOutput());
  28. }
  29. if (isset($testData['exitcode'])) {
  30. $this->assertSame($testData['exitcode'], $exitcode);
  31. }
  32. // Clean up.
  33. $fs = new Filesystem();
  34. if (isset($testData['test_dir']) && is_dir($testData['test_dir'])) {
  35. $fs->removeDirectory($testData['test_dir']);
  36. }
  37. }
  38. public function getTestFiles()
  39. {
  40. $tests = array();
  41. foreach (Finder::create()->in(__DIR__)->name('*.test')->files() as $file) {
  42. $tests[] = array($file);
  43. }
  44. return $tests;
  45. }
  46. private function parseTestFile(\SplFileInfo $file)
  47. {
  48. $tokens = preg_split('#(?:^|\n\n)-- ([a-zA-Z]+) --\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
  49. $data = array();
  50. $section = null;
  51. $varRegex = '#%([^%]+)%#';
  52. $variableReplacer = function($match) use (&$data) {
  53. list(, $var) = $match;
  54. switch ($var) {
  55. case 'testDir':
  56. $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true);
  57. $data['test_dir'] = $testDir;
  58. return $testDir;
  59. default:
  60. throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var));
  61. }
  62. };
  63. for ($i=0,$c=count($tokens); $i<$c; $i++) {
  64. if ('' === $tokens[$i] && null === $section) {
  65. continue;
  66. }
  67. // Handle section headers.
  68. if (null === $section) {
  69. $section = strtolower($tokens[$i]);
  70. continue;
  71. }
  72. $sectionData = $tokens[$i];
  73. // Allow sections to validate, or modify their section data.
  74. switch ($section) {
  75. case 'command':
  76. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  77. break;
  78. case 'exitcode':
  79. $sectionData = (integer) $sectionData;
  80. case 'erroroutputregex':
  81. case 'outputregex':
  82. case 'erroroutput':
  83. case 'output':
  84. $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
  85. break;
  86. default:
  87. throw new \RuntimeException(sprintf('Unknown section "%s". Allowed sections: "command", "output", "erroroutput", "exitcode", "outputregex", "erroroutputregex". '
  88. .'Section headers must be written as "-- HEADER_NAME --" and be preceded by an empty line if not at the start of the file.', $section));
  89. }
  90. $data[$section] = $sectionData;
  91. $section = $sectionData = null;
  92. }
  93. // validate data
  94. if (!isset($data['command'])) {
  95. throw new \RuntimeException('The test file must have a section named "COMMAND".');
  96. }
  97. if (!isset($data['output']) && !isset($data['erroroutput']) && !isset($data['outputregex']) && !isset($data['erroroutputregex'])) {
  98. throw new \RuntimeException('The test file must have a section named "OUTPUT", "ERROROUTPUT", "OUTPUTREGEX", or "ERROROUTPUTREGEX".');
  99. }
  100. return $data;
  101. }
  102. }