InstallViaComposerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 static function setUpBeforeClass(): void
  60. {
  61. parent::setUpBeforeClass();
  62. if ('\\' === \DIRECTORY_SEPARATOR) {
  63. self::markTestIncomplete('This test is broken on Windows');
  64. }
  65. try {
  66. CommandExecutor::create('php --version', __DIR__)->getResult();
  67. } catch (\RuntimeException $e) {
  68. self::fail('Missing `php` env script. Details:'."\n".$e->getMessage());
  69. }
  70. try {
  71. CommandExecutor::create('composer --version', __DIR__)->getResult();
  72. } catch (\RuntimeException $e) {
  73. self::fail('Missing `composer` env script. Details:'."\n".$e->getMessage());
  74. }
  75. try {
  76. CommandExecutor::create('composer check', __DIR__.'/../..')->getResult();
  77. } catch (\RuntimeException $e) {
  78. self::fail('Composer check failed. Details:'."\n".$e->getMessage());
  79. }
  80. }
  81. protected function setUp(): void
  82. {
  83. $this->fs = new Filesystem();
  84. }
  85. protected function tearDown(): void
  86. {
  87. $this->fs = null;
  88. }
  89. public function testInstallationViaPathIsPossible(): void
  90. {
  91. $tmpPath = $this->createFakeComposerProject($this->currentCodeAsComposerDependency);
  92. self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  93. $this->fs->remove($tmpPath);
  94. }
  95. // test that respects `export-ignore` from `.gitattributes` file
  96. public function testInstallationViaArtifactIsPossible(): void
  97. {
  98. // Composer Artifact Repository requires `zip` extension
  99. if (!\extension_loaded('zip')) {
  100. // We do not want to mark test as skipped, because we explicitly want to test this and `zip` is required
  101. self::fail('No zip extension available.');
  102. }
  103. $tmpArtifactPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  104. unlink($tmpArtifactPath);
  105. $this->fs->mkdir($tmpArtifactPath);
  106. $fakeVersion = preg_replace('/\\-.+/', '', Application::VERSION, 1).'-alpha987654321';
  107. $tmpPath = $this->createFakeComposerProject([
  108. 'repositories' => [
  109. [
  110. 'type' => 'artifact',
  111. 'url' => $tmpArtifactPath,
  112. ],
  113. ],
  114. 'require' => [
  115. 'friendsofphp/php-cs-fixer' => $fakeVersion,
  116. ],
  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. self::assertCommandsWork($stepsToInitializeArtifact, $cwd);
  137. self::assertCommandsWork($stepsToPrepareArtifact, $tmpArtifactPath);
  138. self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  139. $this->fs->remove($tmpPath);
  140. $this->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. self::assertSame(0, CommandExecutor::create($command, $cwd)->getResult()->getCode());
  149. }
  150. }
  151. /**
  152. * @param array<string, mixed> $initialComposerFileState
  153. *
  154. * @return string Path to temporary directory containing Composer project
  155. */
  156. private function createFakeComposerProject(array $initialComposerFileState): string
  157. {
  158. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  159. if (false === $tmpPath) {
  160. throw new \RuntimeException('Creating directory for fake Composer project has failed.');
  161. }
  162. unlink($tmpPath);
  163. $this->fs->mkdir($tmpPath);
  164. try {
  165. file_put_contents(
  166. $tmpPath.'/composer.json',
  167. json_encode($initialComposerFileState, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
  168. );
  169. } catch (\JsonException $e) {
  170. throw new \InvalidArgumentException(
  171. 'Initial Composer file state could not be saved as composer.json',
  172. $e->getCode(),
  173. $e
  174. );
  175. }
  176. return $tmpPath;
  177. }
  178. }