InstallViaComposerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 list<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. parent::tearDown();
  89. }
  90. public function testInstallationViaPathIsPossible(): void
  91. {
  92. $tmpPath = $this->createFakeComposerProject($this->currentCodeAsComposerDependency);
  93. self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  94. $this->fs->remove($tmpPath);
  95. }
  96. // test that respects `export-ignore` from `.gitattributes` file
  97. public function testInstallationViaArtifactIsPossible(): void
  98. {
  99. // Composer Artifact Repository requires `zip` extension
  100. if (!\extension_loaded('zip')) {
  101. // We do not want to mark test as skipped, because we explicitly want to test this and `zip` is required
  102. self::fail('No zip extension available.');
  103. }
  104. $tmpArtifactPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  105. unlink($tmpArtifactPath);
  106. $this->fs->mkdir($tmpArtifactPath);
  107. $fakeVersion = preg_replace('/\-.+/', '', Application::VERSION, 1).'-alpha987654321';
  108. $tmpPath = $this->createFakeComposerProject([
  109. 'repositories' => [
  110. [
  111. 'type' => 'artifact',
  112. 'url' => $tmpArtifactPath,
  113. ],
  114. ],
  115. 'require' => [
  116. 'friendsofphp/php-cs-fixer' => $fakeVersion,
  117. ],
  118. ]);
  119. $cwd = __DIR__.'/../..';
  120. $stepsToInitializeArtifact = [
  121. // Clone current version of project to new location, as we are going to modify it.
  122. // Warning! Only already committed changes will be cloned!
  123. "git clone --depth=1 . {$tmpArtifactPath}",
  124. ];
  125. $stepsToPrepareArtifact = [
  126. // Configure git user for new repo to not use global git user.
  127. // We need this, as global git user may not be set!
  128. 'git config user.name test && git config user.email test',
  129. // Adjust cloned project to expose version in `composer.json`.
  130. // Without that, it would not be possible to use it as Composer Artifact.
  131. "composer config version {$fakeVersion} && git add . && git commit --no-gpg-sign -m 'provide version'",
  132. // Create repo archive that will serve as Composer Artifact.
  133. 'git archive HEAD --format=zip -o archive.zip',
  134. // Drop the repo, keep the archive
  135. 'git rm -r . && rm -rf .git',
  136. ];
  137. self::assertCommandsWork($stepsToInitializeArtifact, $cwd);
  138. self::assertCommandsWork($stepsToPrepareArtifact, $tmpArtifactPath);
  139. self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  140. $this->fs->remove($tmpPath);
  141. $this->fs->remove($tmpArtifactPath);
  142. }
  143. /**
  144. * @param list<string> $commands
  145. */
  146. private static function assertCommandsWork(array $commands, string $cwd): void
  147. {
  148. foreach ($commands as $command) {
  149. self::assertSame(0, CommandExecutor::create($command, $cwd)->getResult()->getCode());
  150. }
  151. }
  152. /**
  153. * @param array<string, mixed> $initialComposerFileState
  154. *
  155. * @return string Path to temporary directory containing Composer project
  156. */
  157. private function createFakeComposerProject(array $initialComposerFileState): string
  158. {
  159. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  160. if (false === $tmpPath) {
  161. throw new \RuntimeException('Creating directory for fake Composer project has failed.');
  162. }
  163. unlink($tmpPath);
  164. $this->fs->mkdir($tmpPath);
  165. try {
  166. file_put_contents(
  167. $tmpPath.'/composer.json',
  168. json_encode($initialComposerFileState, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
  169. );
  170. } catch (\JsonException $e) {
  171. throw new \InvalidArgumentException(
  172. 'Initial Composer file state could not be saved as composer.json',
  173. $e->getCode(),
  174. $e
  175. );
  176. }
  177. return $tmpPath;
  178. }
  179. }