CiIntegrationTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\CommandExecutor;
  13. use Keradus\CliExecutor\ScriptExecutor;
  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([
  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. protected function tearDown()
  51. {
  52. parent::tearDown();
  53. self::executeScript([
  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 $expectedResult3FilesLine
  66. *
  67. * @dataProvider provideIntegrationCases
  68. */
  69. public function testIntegration(
  70. $branchName,
  71. array $caseCommands,
  72. array $expectedResult1Lines,
  73. array $expectedResult2Lines,
  74. $expectedResult3FilesLine
  75. ) {
  76. self::executeScript(array_merge(
  77. [
  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 = [
  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([
  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([
  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([
  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.6.0 and maximum version of PHP 7.3.*.
  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. $expectedResult3Files = substr($expectedResult3FilesLine, 0, strpos($expectedResult3FilesLine, ' '));
  119. $pattern = sprintf(
  120. '/^(?:%s)?(?:%s)?%s\n([\.S]{%d})(?: [^\n]*)?\n%s$/',
  121. preg_quote($optionalIncompatibilityWarning, '/'),
  122. preg_quote($optionalXdebugWarning, '/'),
  123. preg_quote('Loaded config default from ".php_cs.dist".', '/'),
  124. \strlen($expectedResult3Files),
  125. preg_quote('Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error', '/')
  126. );
  127. $this->assertRegExp($pattern, $result3->getError());
  128. preg_match($pattern, $result3->getError(), $matches);
  129. $this->assertArrayHasKey(1, $matches);
  130. $this->assertSame(substr_count($expectedResult3Files, '.'), substr_count($matches[1], '.'));
  131. $this->assertSame(substr_count($expectedResult3Files, 'S'), substr_count($matches[1], 'S'));
  132. $this->assertRegExp(
  133. '/^\s*Checked all files in \d+\.\d+ seconds, \d+\.\d+ MB memory used\s*$/',
  134. $result3->getOutput()
  135. );
  136. }
  137. public function provideIntegrationCases()
  138. {
  139. return [
  140. 'random-changes' => [
  141. 'random-changes',
  142. [
  143. 'touch dir\ a/file.php',
  144. 'rm -r dir\ c',
  145. 'echo "" >> dir\ b/file\ b.php',
  146. 'echo "echo 1;" >> dir\ b/file\ b.php',
  147. 'git add .',
  148. 'git commit -m "Random changes" -q',
  149. ],
  150. [
  151. 'dir a/file.php',
  152. 'dir b/file b.php',
  153. ],
  154. [
  155. '--path-mode=intersection',
  156. '--',
  157. 'dir a/file.php',
  158. 'dir b/file b.php',
  159. '',
  160. ],
  161. 'S. 2 / 2 (100%)',
  162. ],
  163. 'changes-including-dist-config-file' => [
  164. 'changes-including-dist-config-file',
  165. [
  166. 'echo "" >> dir\ b/file\ b.php',
  167. 'echo "echo 1;" >> dir\ b/file\ b.php',
  168. // `sed -i ...` is not handled the same on Linux and macOS
  169. 'sed -e \'s/@Symfony/@PSR2/\' .php_cs.dist > .php_cs.dist.new',
  170. 'mv .php_cs.dist.new .php_cs.dist',
  171. 'git add .',
  172. 'git commit -m "Random changes including config file" -q',
  173. ],
  174. [
  175. '.php_cs.dist',
  176. 'dir b/file b.php',
  177. ],
  178. [
  179. '',
  180. '',
  181. ],
  182. '... 3 / 3 (100%)',
  183. ],
  184. 'changes-including-custom-config-file-creation' => [
  185. 'changes-including-custom-config-file-creation',
  186. [
  187. 'echo "" >> dir\ b/file\ b.php',
  188. 'echo "echo 1;" >> dir\ b/file\ b.php',
  189. 'sed -e \'s/@Symfony/@PSR2/\' .php_cs.dist > .php_cs',
  190. 'git add .',
  191. 'git commit -m "Random changes including custom config file creation" -q',
  192. ],
  193. [
  194. '.php_cs',
  195. 'dir b/file b.php',
  196. ],
  197. [
  198. '',
  199. '',
  200. ],
  201. '... 3 / 3 (100%)',
  202. ],
  203. 'changes-including-composer-lock' => [
  204. 'changes-including-composer-lock',
  205. [
  206. 'echo "" >> dir\ b/file\ b.php',
  207. 'echo "echo 1;" >> dir\ b/file\ b.php',
  208. 'touch composer.lock',
  209. 'git add .',
  210. 'git commit -m "Random changes including composer.lock" -q',
  211. ],
  212. [
  213. 'composer.lock',
  214. 'dir b/file b.php',
  215. ],
  216. [
  217. '',
  218. '',
  219. ],
  220. '... 3 / 3 (100%)',
  221. ],
  222. ];
  223. }
  224. private static function executeCommand($command)
  225. {
  226. return CommandExecutor::create($command, self::$fixtureDir)->getResult();
  227. }
  228. private static function executeScript(array $scriptParts)
  229. {
  230. return ScriptExecutor::create($scriptParts, self::$fixtureDir)->getResult();
  231. }
  232. }