InstallViaComposerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 doSetUpBeforeClass()
  39. {
  40. parent::doSetUpBeforeClass();
  41. if ('\\' === \DIRECTORY_SEPARATOR) {
  42. static::markTestIncomplete('This test is broken on Windows');
  43. }
  44. try {
  45. CommandExecutor::create('php --version', __DIR__)->getResult();
  46. } catch (\RuntimeException $e) {
  47. static::markTestSkippedOrFail('Missing `php` env script. Details:'."\n".$e->getMessage());
  48. }
  49. try {
  50. CommandExecutor::create('composer --version', __DIR__)->getResult();
  51. } catch (\RuntimeException $e) {
  52. static::markTestSkippedOrFail('Missing `composer` env script. Details:'."\n".$e->getMessage());
  53. }
  54. try {
  55. CommandExecutor::create('composer check', __DIR__.'/../..')->getResult();
  56. } catch (\RuntimeException $e) {
  57. static::markTestSkippedOrFail('Composer check failed. Details:'."\n".$e->getMessage());
  58. }
  59. }
  60. public function testInstallationViaPathIsPossible()
  61. {
  62. $fs = new Filesystem();
  63. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  64. unlink($tmpPath);
  65. $fs->mkdir($tmpPath);
  66. $initialComposerFileState = [
  67. 'repositories' => [
  68. [
  69. 'type' => 'path',
  70. 'url' => __DIR__.'/../..',
  71. ],
  72. ],
  73. 'require' => [
  74. 'friendsofphp/php-cs-fixer' => '*@dev',
  75. ],
  76. ];
  77. file_put_contents(
  78. $tmpPath.'/composer.json',
  79. json_encode($initialComposerFileState, Utils::calculateBitmask(['JSON_PRETTY_PRINT']))
  80. );
  81. static::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  82. $fs->remove($tmpPath);
  83. }
  84. // test that respects `export-ignore` from `.gitattributes` file
  85. public function testInstallationViaArtifactIsPossible()
  86. {
  87. // Composer Artifact Repository requires `zip` extension
  88. if (!\extension_loaded('zip')) {
  89. static::markTestSkippedOrFail('No zip extension available.');
  90. }
  91. $fs = new Filesystem();
  92. $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  93. unlink($tmpPath);
  94. $fs->mkdir($tmpPath);
  95. $tmpArtifactPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
  96. unlink($tmpArtifactPath);
  97. $fs->mkdir($tmpArtifactPath);
  98. $fakeVersion = preg_replace('/\\-.+/', '', Application::VERSION, 1).'-alpha987654321';
  99. $initialComposerFileState = [
  100. 'repositories' => [
  101. [
  102. 'type' => 'artifact',
  103. 'url' => $tmpArtifactPath,
  104. ],
  105. ],
  106. 'require' => [
  107. 'friendsofphp/php-cs-fixer' => $fakeVersion,
  108. ],
  109. ];
  110. file_put_contents(
  111. $tmpPath.'/composer.json',
  112. json_encode($initialComposerFileState, Utils::calculateBitmask(['JSON_PRETTY_PRINT']))
  113. );
  114. $cwd = __DIR__.'/../..';
  115. $stepsToInitializeArtifact = [
  116. // Clone current version of project to new location, as we gonna 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. static::assertCommandsWork($stepsToInitializeArtifact, $cwd);
  133. static::assertCommandsWork($stepsToPrepareArtifact, $tmpArtifactPath);
  134. static::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);
  135. // ensure that files from "tests" directory in release are autoloaded
  136. $finder = Finder::create()
  137. ->files()
  138. ->in($tmpPath.'/vendor/friendsofphp/php-cs-fixer')
  139. ->path('/tests/')
  140. ->sortByName()
  141. ;
  142. $filesInRelease = [];
  143. foreach ($finder as $file) {
  144. $filesInRelease[] = $file->getRelativePathname();
  145. }
  146. $composer = json_decode(file_get_contents(__DIR__.'/../../composer.json'), true);
  147. $autoloadFiles = $composer['autoload']['classmap'];
  148. static::assertSame($filesInRelease, $autoloadFiles, 'Expected all files in "./tests" directory to be in "classmap" "composer.json", update the "classmap" or ".gitattributes".');
  149. $fs->remove($tmpPath);
  150. $fs->remove($tmpArtifactPath);
  151. }
  152. private static function assertCommandsWork(array $commands, $cwd)
  153. {
  154. foreach ($commands as $command) {
  155. static::assertSame(0, CommandExecutor::create($command, $cwd)->getResult()->getCode());
  156. }
  157. }
  158. }