InstallViaComposerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * @author Greg Korba <greg@codito.dev>
  19. *
  20. * @internal
  21. *
  22. * @coversNothing
  23. *
  24. * @group covers-nothing
  25. *
  26. * @large
  27. */
  28. final class InstallViaComposerTest extends AbstractSmokeTestCase
  29. {
  30. private Filesystem $fs;
  31. /** @var array<string, mixed> */
  32. private array $currentCodeAsComposerDependency = [
  33. 'repositories' => [
  34. [
  35. 'type' => 'path',
  36. 'url' => __DIR__.'/../..',
  37. 'options' => [
  38. 'symlink' => false,
  39. ],
  40. ],
  41. ],
  42. 'require' => [
  43. 'friendsofphp/php-cs-fixer' => '*@dev',
  44. ],
  45. ];
  46. /**
  47. * @var string[]
  48. */
  49. private array $stepsToVerifyInstallation = [
  50. // Confirm we can install.
  51. 'composer install -q',
  52. // Ensure that autoloader works.
  53. 'composer dump-autoload --optimize',
  54. 'php vendor/autoload.php',
  55. // Ensure basic commands work.
  56. 'vendor/bin/php-cs-fixer --version',
  57. 'vendor/bin/php-cs-fixer fix --help',
  58. ];
  59. public function __construct()
  60. {
  61. $this->fs = new Filesystem();
  62. parent::__construct();
  63. }
  64. public static function setUpBeforeClass(): void
  65. {
  66. parent::setUpBeforeClass();
  67. if ('\\' === \DIRECTORY_SEPARATOR) {
  68. self::markTestIncomplete('This test is broken on Windows');
  69. }
  70. try {
  71. CommandExecutor::create('php --version', __DIR__)->getResult();
  72. } catch (\RuntimeException $e) {
  73. self::markTestSkippedOrFail('Missing `php` env script. Details:'."\n".$e->getMessage());
  74. }
  75. try {
  76. CommandExecutor::create('composer --version', __DIR__)->getResult();
  77. } catch (\RuntimeException $e) {
  78. self::markTestSkippedOrFail('Missing `composer` env script. Details:'."\n".$e->getMessage());
  79. }
  80. try {
  81. CommandExecutor::create('composer check', __DIR__.'/../..')->getResult();
  82. } catch (\RuntimeException $e) {
  83. self::markTestSkippedOrFail('Composer check failed. Details:'."\n".$e->getMessage());
  84. }
  85. }
  86. public function testInstallationViaPathIsPossible(): void
  87. {
  88. $tmpPath = $this->createFakeComposerProject($this->currentCodeAsComposerDependency);
  89. self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  90. $this->fs->remove($tmpPath);
  91. }
  92. // test that respects `export-ignore` from `.gitattributes` file
  93. public function testInstallationViaArtifactIsPossible(): void
  94. {
  95. // Composer Artifact Repository requires `zip` extension
  96. if (!\extension_loaded('zip')) {
  97. self::markTestSkippedOrFail('No zip extension available.');
  98. }
  99. $tmpArtifactPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  100. unlink($tmpArtifactPath);
  101. $this->fs->mkdir($tmpArtifactPath);
  102. $fakeVersion = preg_replace('/\\-.+/', '', Application::VERSION, 1).'-alpha987654321';
  103. $tmpPath = $this->createFakeComposerProject([
  104. 'repositories' => [
  105. [
  106. 'type' => 'artifact',
  107. 'url' => $tmpArtifactPath,
  108. ],
  109. ],
  110. 'require' => [
  111. 'friendsofphp/php-cs-fixer' => $fakeVersion,
  112. ],
  113. ]);
  114. $cwd = __DIR__.'/../..';
  115. $stepsToInitializeArtifact = [
  116. // Clone current version of project to new location, as we are going to modify it.
  117. // Warning! Only already committed changes will be cloned!
  118. "git clone --depth=1 . {$tmpArtifactPath}",
  119. ];
  120. $stepsToPrepareArtifact = [
  121. // Configure git user for new repo to not use global git user.
  122. // We need this, as global git user may not be set!
  123. 'git config user.name test && git config user.email test',
  124. // Adjust cloned project to expose version in `composer.json`.
  125. // Without that, it would not be possible to use it as Composer Artifact.
  126. "composer config version {$fakeVersion} && git add . && git commit --no-gpg-sign -m 'provide version'",
  127. // Create repo archive that will serve as Composer Artifact.
  128. 'git archive HEAD --format=zip -o archive.zip',
  129. // Drop the repo, keep the archive
  130. 'git rm -r . && rm -rf .git',
  131. ];
  132. self::assertCommandsWork($stepsToInitializeArtifact, $cwd);
  133. self::assertCommandsWork($stepsToPrepareArtifact, $tmpArtifactPath);
  134. self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  135. $this->fs->remove($tmpPath);
  136. $this->fs->remove($tmpArtifactPath);
  137. }
  138. /**
  139. * @param list<string> $commands
  140. */
  141. private static function assertCommandsWork(array $commands, string $cwd): void
  142. {
  143. foreach ($commands as $command) {
  144. self::assertSame(0, CommandExecutor::create($command, $cwd)->getResult()->getCode());
  145. }
  146. }
  147. /**
  148. * @param array<string, mixed> $initialComposerFileState
  149. *
  150. * @return string Path to temporary directory containing Composer project
  151. */
  152. private function createFakeComposerProject(array $initialComposerFileState): string
  153. {
  154. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  155. if (false === $tmpPath) {
  156. throw new \RuntimeException('Creating directory for fake Composer project has failed.');
  157. }
  158. unlink($tmpPath);
  159. $this->fs->mkdir($tmpPath);
  160. try {
  161. file_put_contents(
  162. $tmpPath.'/composer.json',
  163. json_encode($initialComposerFileState, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
  164. );
  165. } catch (\JsonException $e) {
  166. throw new \InvalidArgumentException(
  167. 'Initial Composer file state could not be saved as composer.json',
  168. $e->getCode(),
  169. $e
  170. );
  171. }
  172. return $tmpPath;
  173. }
  174. }