CiIntegrationTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Smoke;
  12. use Keradus\CliExecutor\BashScriptExecutor;
  13. use Keradus\CliExecutor\CommandExecutor;
  14. use PhpCsFixer\Tests\TestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @requires OS Linux|Darwin
  21. * @coversNothing
  22. * @group covers-nothing
  23. * @large
  24. */
  25. final class CiIntegrationTest extends TestCase
  26. {
  27. public static $fixtureDir;
  28. public static function setUpBeforeClass()
  29. {
  30. parent::setUpBeforeClass();
  31. self::$fixtureDir = __DIR__.'/../Fixtures/ci-integration';
  32. try {
  33. self::executeScript(array(
  34. 'rm -rf .git',
  35. 'git init -q',
  36. 'git config user.name test',
  37. 'git config user.email test',
  38. 'git add .',
  39. 'git commit -m "init" -q',
  40. ));
  41. } catch (\RuntimeException $e) {
  42. self::markTestSkipped($e->getMessage());
  43. }
  44. }
  45. public static function tearDownAfterClass()
  46. {
  47. parent::tearDownAfterClass();
  48. self::executeCommand('rm -rf .git');
  49. }
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. self::executeScript(array(
  54. 'git reset . -q',
  55. 'git checkout . -q',
  56. 'git clean -fdq',
  57. 'git checkout master -q',
  58. ));
  59. }
  60. /**
  61. * @param string $branchName
  62. * @param string[] $caseCommands
  63. * @param string[] $expectedResult1Lines
  64. * @param string[] $expectedResult2Lines
  65. * @param string $expectedResult3Files
  66. *
  67. * @dataProvider provideIntegrationCases
  68. */
  69. public function testIntegration(
  70. $branchName,
  71. array $caseCommands,
  72. array $expectedResult1Lines,
  73. array $expectedResult2Lines,
  74. $expectedResult3Files
  75. ) {
  76. self::executeScript(array_merge(
  77. array(
  78. "git checkout -b ${branchName} -q",
  79. ),
  80. $caseCommands
  81. ));
  82. $integrationScript = explode("\n", str_replace('vendor/bin/', './../../../', file_get_contents(__DIR__.'/../../dev-tools/ci-integration.sh')));
  83. $steps = array(
  84. "COMMIT_RANGE=\"master..${branchName}\"",
  85. "{$integrationScript[3]}\n{$integrationScript[4]}",
  86. $integrationScript[5],
  87. $integrationScript[6],
  88. $integrationScript[7],
  89. );
  90. $result1 = self::executeScript(array(
  91. $steps[0],
  92. $steps[1],
  93. $steps[2],
  94. 'echo "$CHANGED_FILES"',
  95. ));
  96. $this->assertSame(implode("\n", $expectedResult1Lines)."\n", $result1->getOutput());
  97. $result2 = self::executeScript(array(
  98. $steps[0],
  99. $steps[1],
  100. $steps[2],
  101. $steps[3],
  102. 'echo "${EXTRA_ARGS}"',
  103. ));
  104. $this->assertSame(implode("\n", $expectedResult2Lines), $result2->getOutput());
  105. $result3 = self::executeScript(array(
  106. $steps[0],
  107. $steps[1],
  108. $steps[2],
  109. $steps[3],
  110. $steps[4],
  111. ));
  112. $optionalIncompatibilityWarning = 'PHP needs to be a minimum version of PHP 5.3.6 and maximum version of PHP 7.2.*.
  113. Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.
  114. ';
  115. $optionalXdebugWarning = 'You are running PHP CS Fixer with xdebug enabled. This has a major impact on runtime performance.
  116. If you need help while solving warnings, ask at https://gitter.im/PHP-CS-Fixer, we will help you!
  117. ';
  118. $executionDetails = "Loaded config default from \".php_cs.dist\".
  119. ${expectedResult3Files}
  120. Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error";
  121. $this->assertRegExp(
  122. sprintf(
  123. '/^(%s)?(%s)?%s$/',
  124. preg_quote($optionalIncompatibilityWarning, '/'),
  125. preg_quote($optionalXdebugWarning, '/'),
  126. preg_quote($executionDetails, '/')
  127. ),
  128. $result3->getError()
  129. );
  130. $this->assertRegExp(
  131. '/^\s*Checked all files in \d+\.\d+ seconds, \d+\.\d+ MB memory used\s*$/',
  132. $result3->getOutput()
  133. );
  134. }
  135. public function provideIntegrationCases()
  136. {
  137. return array(
  138. array(
  139. 'random-changes',
  140. array(
  141. 'touch dir\ a/file.php',
  142. 'rm -r dir\ c',
  143. 'echo "" >> dir\ b/file\ b.php',
  144. 'echo "echo 1;" >> dir\ b/file\ b.php',
  145. 'git add .',
  146. 'git commit -m "Random changes" -q',
  147. ),
  148. array(
  149. 'dir a/file.php',
  150. 'dir b/file b.php',
  151. ),
  152. array(
  153. '--path-mode=intersection',
  154. '--',
  155. 'dir a/file.php',
  156. 'dir b/file b.php',
  157. '',
  158. ),
  159. 'S.',
  160. ),
  161. array(
  162. 'changes-including-dist-config-file',
  163. array(
  164. 'echo "" >> dir\ b/file\ b.php',
  165. 'echo "echo 1;" >> dir\ b/file\ b.php',
  166. // `sed -i ...` is not handled the same on Linux and macOS
  167. 'sed -e \'s/@Symfony/@PSR2/\' .php_cs.dist > .php_cs.dist.new',
  168. 'mv .php_cs.dist.new .php_cs.dist',
  169. 'git add .',
  170. 'git commit -m "Random changes including config file" -q',
  171. ),
  172. array(
  173. '.php_cs.dist',
  174. 'dir b/file b.php',
  175. ),
  176. array(
  177. '',
  178. '',
  179. ),
  180. '...',
  181. ),
  182. array(
  183. 'changes-including-custom-config-file-creation',
  184. array(
  185. 'echo "" >> dir\ b/file\ b.php',
  186. 'echo "echo 1;" >> dir\ b/file\ b.php',
  187. 'sed -e \'s/@Symfony/@PSR2/\' .php_cs.dist > .php_cs',
  188. 'git add .',
  189. 'git commit -m "Random changes including custom config file creation" -q',
  190. ),
  191. array(
  192. '.php_cs',
  193. 'dir b/file b.php',
  194. ),
  195. array(
  196. '',
  197. '',
  198. ),
  199. '...',
  200. ),
  201. array(
  202. 'changes-including-composer-lock',
  203. array(
  204. 'echo "" >> dir\ b/file\ b.php',
  205. 'echo "echo 1;" >> dir\ b/file\ b.php',
  206. 'touch composer.lock',
  207. 'git add .',
  208. 'git commit -m "Random changes including composer.lock" -q',
  209. ),
  210. array(
  211. 'composer.lock',
  212. 'dir b/file b.php',
  213. ),
  214. array(
  215. '',
  216. '',
  217. ),
  218. '...',
  219. ),
  220. );
  221. }
  222. private static function executeCommand($command)
  223. {
  224. return CommandExecutor::create($command, self::$fixtureDir)->getResult();
  225. }
  226. private static function executeScript(array $scriptParts)
  227. {
  228. return BashScriptExecutor::create($scriptParts, self::$fixtureDir)->getResult();
  229. }
  230. }