InstallViaComposerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Smoke;
  13. use Keradus\CliExecutor\CommandExecutor;
  14. use PhpCsFixer\Console\Application;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @coversNothing
  22. *
  23. * @group covers-nothing
  24. *
  25. * @large
  26. */
  27. final class InstallViaComposerTest extends AbstractSmokeTest
  28. {
  29. /**
  30. * @var string[]
  31. */
  32. private array $stepsToVerifyInstallation = [
  33. // Confirm we can install.
  34. 'composer install -q',
  35. // Ensure that autoloader works.
  36. 'composer dump-autoload --optimize',
  37. 'php vendor/autoload.php',
  38. // Ensure basic commands work.
  39. 'vendor/bin/php-cs-fixer --version',
  40. 'vendor/bin/php-cs-fixer fix --help',
  41. ];
  42. public static function setUpBeforeClass(): void
  43. {
  44. parent::setUpBeforeClass();
  45. if ('\\' === \DIRECTORY_SEPARATOR) {
  46. static::markTestIncomplete('This test is broken on Windows');
  47. }
  48. try {
  49. CommandExecutor::create('php --version', __DIR__)->getResult();
  50. } catch (\RuntimeException $e) {
  51. static::markTestSkippedOrFail('Missing `php` env script. Details:'."\n".$e->getMessage());
  52. }
  53. try {
  54. CommandExecutor::create('composer --version', __DIR__)->getResult();
  55. } catch (\RuntimeException $e) {
  56. static::markTestSkippedOrFail('Missing `composer` env script. Details:'."\n".$e->getMessage());
  57. }
  58. try {
  59. CommandExecutor::create('composer check', __DIR__.'/../..')->getResult();
  60. } catch (\RuntimeException $e) {
  61. static::markTestSkippedOrFail('Composer check failed. Details:'."\n".$e->getMessage());
  62. }
  63. }
  64. public function testInstallationViaPathIsPossible(): void
  65. {
  66. $fs = new Filesystem();
  67. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  68. unlink($tmpPath);
  69. $fs->mkdir($tmpPath);
  70. $initialComposerFileState = [
  71. 'repositories' => [
  72. [
  73. 'type' => 'path',
  74. 'url' => __DIR__.'/../..',
  75. ],
  76. ],
  77. 'require' => [
  78. 'friendsofphp/php-cs-fixer' => '*@dev',
  79. ],
  80. ];
  81. file_put_contents(
  82. $tmpPath.'/composer.json',
  83. json_encode($initialComposerFileState, JSON_PRETTY_PRINT)
  84. );
  85. static::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  86. $fs->remove($tmpPath);
  87. }
  88. // test that respects `export-ignore` from `.gitattributes` file
  89. public function testInstallationViaArtifactIsPossible(): void
  90. {
  91. // Composer Artifact Repository requires `zip` extension
  92. if (!\extension_loaded('zip')) {
  93. static::markTestSkippedOrFail('No zip extension available.');
  94. }
  95. $fs = new Filesystem();
  96. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  97. unlink($tmpPath);
  98. $fs->mkdir($tmpPath);
  99. $tmpArtifactPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  100. unlink($tmpArtifactPath);
  101. $fs->mkdir($tmpArtifactPath);
  102. $fakeVersion = preg_replace('/\\-.+/', '', Application::VERSION, 1).'-alpha987654321';
  103. $initialComposerFileState = [
  104. 'repositories' => [
  105. [
  106. 'type' => 'artifact',
  107. 'url' => $tmpArtifactPath,
  108. ],
  109. ],
  110. 'require' => [
  111. 'friendsofphp/php-cs-fixer' => $fakeVersion,
  112. ],
  113. ];
  114. file_put_contents(
  115. $tmpPath.'/composer.json',
  116. json_encode($initialComposerFileState, JSON_PRETTY_PRINT)
  117. );
  118. $cwd = __DIR__.'/../..';
  119. $stepsToInitializeArtifact = [
  120. // Clone current version of project to new location, as we are going to modify it.
  121. // Warning! Only already committed changes will be cloned!
  122. "git clone --depth=1 . {$tmpArtifactPath}",
  123. ];
  124. $stepsToPrepareArtifact = [
  125. // Configure git user for new repo to not use global git user.
  126. // We need this, as global git user may not be set!
  127. 'git config user.name test && git config user.email test',
  128. // Adjust cloned project to expose version in `composer.json`.
  129. // Without that, it would not be possible to use it as Composer Artifact.
  130. "composer config version {$fakeVersion} && git add . && git commit --no-gpg-sign -m 'provide version'",
  131. // Create repo archive that will serve as Composer Artifact.
  132. 'git archive HEAD --format=zip -o archive.zip',
  133. // Drop the repo, keep the archive
  134. 'git rm -r . && rm -rf .git',
  135. ];
  136. static::assertCommandsWork($stepsToInitializeArtifact, $cwd);
  137. static::assertCommandsWork($stepsToPrepareArtifact, $tmpArtifactPath);
  138. static::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  139. $fs->remove($tmpPath);
  140. $fs->remove($tmpArtifactPath);
  141. }
  142. /**
  143. * @param list<string> $commands
  144. */
  145. private static function assertCommandsWork(array $commands, string $cwd): void
  146. {
  147. foreach ($commands as $command) {
  148. static::assertSame(0, CommandExecutor::create($command, $cwd)->getResult()->getCode());
  149. }
  150. }
  151. }