1313namespace PhpCsFixer \Tests \Smoke ;
1414
1515use Keradus \CliExecutor \CommandExecutor ;
16+ use PhpCsFixer \Console \Application ;
1617use PhpCsFixer \Utils ;
17- use PHPUnit \Framework \TestCase ;
1818use Symfony \Component \Filesystem \Filesystem ;
1919
2020/**
2626 * @group covers-nothing
2727 * @large
2828 */
29- final class InstallViaComposerTest extends TestCase
29+ final class InstallViaComposerTest extends AbstractSmokeTest
3030{
31+ private $ stepsToVerifyInstallation = [
32+ // Confirm we can install.
33+ 'composer install -q ' ,
34+ // Ensure that autoloader works.
35+ 'composer dump-autoload --optimize ' ,
36+ 'php vendor/autoload.php ' ,
37+ // Ensure basic commands work.
38+ 'vendor/bin/php-cs-fixer --version ' ,
39+ 'vendor/bin/php-cs-fixer fix --help ' ,
40+ ];
41+
3142 public static function setUpBeforeClass ()
3243 {
44+ parent ::setUpBeforeClass ();
45+
3346 try {
3447 CommandExecutor::create ('php --version ' , __DIR__ )->getResult ();
3548 } catch (\RuntimeException $ e ) {
36- self ::markTestSkipped ('Missing `php` env script. Details: ' ."\n" .$ e ->getMessage ());
49+ self ::markTestSkippedOrFail ('Missing `php` env script. Details: ' ."\n" .$ e ->getMessage ());
3750 }
3851
3952 try {
4053 CommandExecutor::create ('composer --version ' , __DIR__ )->getResult ();
4154 } catch (\RuntimeException $ e ) {
42- self ::markTestSkipped ('Missing `composer` env script. Details: ' ."\n" .$ e ->getMessage ());
55+ self ::markTestSkippedOrFail ('Missing `composer` env script. Details: ' ."\n" .$ e ->getMessage ());
56+ }
57+
58+ try {
59+ CommandExecutor::create ('composer check ' , __DIR__ .'/../.. ' )->getResult ();
60+ } catch (\RuntimeException $ e ) {
61+ self ::markTestSkippedOrFail ('Composer check failed. Details: ' ."\n" .$ e ->getMessage ());
4362 }
4463 }
4564
46- public function testInstallationIsPossible ()
65+ public function testInstallationViaPathIsPossible ()
4766 {
4867 $ fs = new Filesystem ();
4968
@@ -68,11 +87,80 @@ public function testInstallationIsPossible()
6887 json_encode ($ initialComposerFileState , Utils::calculateBitmask (['JSON_PRETTY_PRINT ' ]))
6988 );
7089
71- $ this ->assertSame (0 , CommandExecutor::create ('composer install -q ' , $ tmpPath )->getResult ()->getCode ());
72- $ this ->assertSame (0 , CommandExecutor::create ('composer dump-autoload --optimize ' , $ tmpPath )->getResult ()->getCode ());
73- $ this ->assertSame (0 , CommandExecutor::create ('php vendor/autoload.php ' , $ tmpPath )->getResult ()->getCode ());
74- $ this ->assertSame (0 , CommandExecutor::create ('vendor/bin/php-cs-fixer --version ' , $ tmpPath )->getResult ()->getCode ());
90+ self ::assertCommandsWork ($ this ->stepsToVerifyInstallation , $ tmpPath );
7591
7692 $ fs ->remove ($ tmpPath );
7793 }
94+
95+ // test that respects `export-ignore` from `.gitattributes` file
96+ public function testInstallationViaArtifactIsPossible ()
97+ {
98+ // Composer Artifact Repository requires `zip` extension
99+ if (!\extension_loaded ('zip ' )) {
100+ $ this ->markTestSkippedOrFail ('No zip extension available. ' );
101+ }
102+
103+ $ fs = new Filesystem ();
104+
105+ $ tmpPath = tempnam (sys_get_temp_dir (), 'cs_fixer_tmp_ ' );
106+ unlink ($ tmpPath );
107+ $ fs ->mkdir ($ tmpPath );
108+
109+ $ tmpArtifactPath = tempnam (sys_get_temp_dir (), 'cs_fixer_tmp_ ' );
110+ unlink ($ tmpArtifactPath );
111+ $ fs ->mkdir ($ tmpArtifactPath );
112+
113+ $ fakeVersion = preg_replace ('/ \\-.+/ ' , '' , Application::VERSION , 1 ).'-alpha987654321 ' ;
114+
115+ $ initialComposerFileState = [
116+ 'repositories ' => [
117+ [
118+ 'type ' => 'artifact ' ,
119+ 'url ' => $ tmpArtifactPath ,
120+ ],
121+ ],
122+ 'require ' => [
123+ 'friendsofphp/php-cs-fixer ' => $ fakeVersion ,
124+ ],
125+ ];
126+
127+ file_put_contents (
128+ $ tmpPath .'/composer.json ' ,
129+ json_encode ($ initialComposerFileState , Utils::calculateBitmask (['JSON_PRETTY_PRINT ' ]))
130+ );
131+
132+ $ cwd = __DIR__ .'/../.. ' ;
133+
134+ $ stepsToInitializeArtifact = [
135+ // Clone current version of project to new location, as we gonna modify it.
136+ // Warning! Only already committed changes will be cloned!
137+ "git clone . $ {tmpArtifactPath}" ,
138+ ];
139+ $ stepsToPrepareArtifact = [
140+ // Configure git user for new repo to not use global git user.
141+ // We need this, as global git user may not be set!
142+ 'git config user.name test && git config user.email test ' ,
143+ // Adjust cloned project to expose version in `composer.json`.
144+ // Without that, it would not be possible to use it as Composer Artifact.
145+ "composer config version $ {fakeVersion} && git add . && git commit -m 'provide version' " ,
146+ // Create repo archive that will serve as Composer Artifact.
147+ 'git archive HEAD --format=zip -o archive.zip ' ,
148+ // Drop the repo, keep the archive
149+ 'git rm -r . && rm -rf .git ' ,
150+ ];
151+
152+ self ::assertCommandsWork ($ stepsToInitializeArtifact , $ cwd );
153+ self ::assertCommandsWork ($ stepsToPrepareArtifact , $ tmpArtifactPath );
154+ self ::assertCommandsWork ($ this ->stepsToVerifyInstallation , $ tmpPath );
155+
156+ $ fs ->remove ($ tmpPath );
157+ $ fs ->remove ($ tmpArtifactPath );
158+ }
159+
160+ private static function assertCommandsWork (array $ commands , $ cwd )
161+ {
162+ foreach ($ commands as $ command ) {
163+ self ::assertSame (0 , CommandExecutor::create ($ command , $ cwd )->getResult ()->getCode ());
164+ }
165+ }
78166}
0 commit comments