InstallViaComposerTest.php 6.0 KB

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