InstallViaComposerTest.php 5.5 KB

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