From 1389111990f2e2d1422e0d7f00933ae7dd94be51 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 26 Oct 2016 17:41:09 +0100 Subject: [PATCH 01/18] Ignore the global plugin when updating local projects --- src/PackageVersions/Installer.php | 18 +++-- test/PackageVersionsTest/InstallerTest.php | 81 +++++++++++++++++++++- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/PackageVersions/Installer.php b/src/PackageVersions/Installer.php index 7756ce7..aa067d7 100644 --- a/src/PackageVersions/Installer.php +++ b/src/PackageVersions/Installer.php @@ -80,14 +80,20 @@ public static function getSubscribedEvents() */ public static function dumpVersionsClass(Event $composerEvent) { - $io = $composerEvent->getIO(); + $composer = $composerEvent->getComposer(); + $versions = iterator_to_array(self::getVersions($composer->getLocker(), $composer->getPackage())); - $io->write('ocramius/package-versions: Generating version class...'); + if (!isset($versions['ocramius/package-versions'])) { + //plugin must be globally installed - we only want to generate versions for projects which specifically + //require ocramius/package-versions + return; + } - $composer = $composerEvent->getComposer(); + $io = $composerEvent->getIO(); + $io->write('ocramius/package-versions: Generating version class...'); self::writeVersionClassToFile( - self::generateVersionsClass($composer), + self::generateVersionsClass($versions), $composer->getConfig(), $composer->getPackage() ); @@ -95,12 +101,12 @@ public static function dumpVersionsClass(Event $composerEvent) $io->write('ocramius/package-versions: ...done generating version class'); } - private static function generateVersionsClass(Composer $composer) : string + private static function generateVersionsClass(array $versions) : string { return sprintf( self::$generatedClassTemplate, 'fin' . 'al ' . 'cla' . 'ss ' . 'Versions', // note: workaround for regex-based code parsers :-( - var_export(iterator_to_array(self::getVersions($composer->getLocker(), $composer->getPackage())), true) + var_export($versions, true) ); } diff --git a/test/PackageVersionsTest/InstallerTest.php b/test/PackageVersionsTest/InstallerTest.php index c7cac80..bebd0ab 100644 --- a/test/PackageVersionsTest/InstallerTest.php +++ b/test/PackageVersionsTest/InstallerTest.php @@ -96,6 +96,10 @@ public function testDumpVersionsClass() ->method('getLockData') ->willReturn([ 'packages' => [ + [ + 'name' => 'ocramius/package-versions', + 'version' => '1.0.0', + ], [ 'name' => 'foo/bar', 'version' => '1.2.3', @@ -156,6 +160,7 @@ public function testDumpVersionsClass() final class Versions { const VERSIONS = array ( + 'ocramius/package-versions' => '1.0.0@', 'foo/bar' => '1.2.3@abc123', 'baz/tab' => '4.5.6@def456', 'tar/taz' => '7.8.9@ghi789', @@ -208,6 +213,10 @@ public function testDumpVersionsClassNoDev() ->method('getLockData') ->willReturn([ 'packages' => [ + [ + 'name' => 'ocramius/package-versions', + 'version' => '1.0.0', + ], [ 'name' => 'foo/bar', 'version' => '1.2.3', @@ -259,6 +268,7 @@ public function testDumpVersionsClassNoDev() final class Versions { const VERSIONS = array ( + 'ocramius/package-versions' => '1.0.0@', 'foo/bar' => '1.2.3@abc123', 'baz/tab' => '4.5.6@def456', 'root/package' => '1.3.5@aaabbbcccddd', @@ -315,6 +325,10 @@ public function testDumpVersionsWithoutPackageSourceDetails() ->method('getLockData') ->willReturn([ 'packages' => [ + [ + 'name' => 'ocramius/package-versions', + 'version' => '1.0.0', + ], [ 'name' => 'foo/bar', 'version' => '1.2.3', @@ -363,6 +377,7 @@ public function testDumpVersionsWithoutPackageSourceDetails() final class Versions { const VERSIONS = array ( + 'ocramius/package-versions' => '1.0.0@', 'foo/bar' => '1.2.3@abc123', 'baz/tab' => '4.5.6@', 'root/package' => '1.3.5@aaabbbcccddd', @@ -424,7 +439,12 @@ public function testDumpsVersionsClassToSpecificLocation(RootPackageInterface $r ->expects(self::any()) ->method('getLockData') ->willReturn([ - 'packages' => [], + 'packages' => [ + [ + 'name' => 'ocramius/package-versions', + 'version' => '1.0.0', + ] + ], 'packages-dev' => [], ]); @@ -497,6 +517,65 @@ public function rootPackageProvider() : array ]; } + public function testVersionsAreNotDumpedIfPackageVersionsNotExplicitlyRequired() + { + $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); + $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); + $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); + $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); + $repository = $this->createMock(InstalledRepositoryInterface::class); + $package = $this->createMock(RootPackageInterface::class); + + $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); + + $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; + + mkdir($expectedPath, 0777, true); + + $locker + ->expects(self::any()) + ->method('getLockData') + ->willReturn([ + 'packages' => [ + [ + 'name' => 'foo/bar', + 'version' => '1.2.3', + 'dist' => [ + 'reference' => 'abc123', // version defined in the dist, this time + ], + ], + [ + 'name' => 'baz/tab', + 'version' => '4.5.6', // source missing + ], + ], + ]); + + $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); + + $this->composer->expects(self::any())->method('getConfig')->willReturn($config); + $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); + $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); + $this->composer->expects(self::any())->method('getPackage')->willReturn($package); + $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); + + $package->expects(self::any())->method('getName')->willReturn('root/package'); + $package->expects(self::any())->method('getVersion')->willReturn('1.3.5'); + $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd'); + + $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); + + Installer::dumpVersionsClass(new Event( + 'post-install-cmd', + $this->composer, + $this->io + )); + + self::assertFileNotExists($expectedPath . '/Versions.php'); + + $this->rmDir($vendorDir); + } + /** * @param string $directory * From bb06fb0b32636778fe9844106f909d1ecadf315f Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 26 Oct 2016 20:42:34 +0100 Subject: [PATCH 02/18] E2E tests for global plugin --- test/PackageVersionsTest/E2EInstallerTest.php | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 test/PackageVersionsTest/E2EInstallerTest.php diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php new file mode 100644 index 0000000..eedc07f --- /dev/null +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -0,0 +1,188 @@ + + */ +class E2EInstaller extends PHPUnit_Framework_TestCase +{ + private $tempGlobalComposerHome; + private $tempLocalComposerHome; + private $tempArtifact; + + public function setUp() + { + $this->tempGlobalComposerHome = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/global'; + $this->tempLocalComposerHome = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/local'; + $this->tempArtifact = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/artifacts'; + mkdir($this->tempGlobalComposerHome, 0777, true); + mkdir($this->tempLocalComposerHome, 0777, true); + mkdir($this->tempArtifact, 0777, true); + + putenv('COMPOSER_HOME=' . $this->tempGlobalComposerHome); + } + + public function tearDown() + { + $this->rmDir($this->tempGlobalComposerHome); + $this->rmDir($this->tempLocalComposerHome); + $this->rmDir($this->tempArtifact); + } + + public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProject() + { + $this->createPackageVersionsArtifact(); + + file_put_contents($this->tempGlobalComposerHome . '/composer.json', json_encode([ + 'name' => 'package-versions/e2e-global', + 'require' => [ + 'ocramius/package-versions' => '1.0.0' + ], + 'repositories' => [ + [ + 'packagist' => false, + ], + [ + 'type' => 'artifact', + 'url' => $this->tempArtifact, + ] + ] + ], JSON_PRETTY_PRINT)); + + $this->exec(__DIR__ . '/../../vendor/bin/composer global update'); + + $this->createArtifact(); + file_put_contents($this->tempLocalComposerHome . '/composer.json', json_encode([ + 'name' => 'package-versions/e2e-local', + 'require' => [ + 'test/package' => '2.0.0' + ], + 'repositories' => [ + [ + 'packagist' => false, + ], + [ + 'type' => 'artifact', + 'url' => $this->tempArtifact, + ] + ] + ], JSON_PRETTY_PRINT)); + + $this->execInDir('composer update -vvv', $this->tempLocalComposerHome); + + $this->assertFileNotExists($this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'); + } + + private function createPackageVersionsArtifact() + { + $zip = new ZipArchive(); + + $zip->open($this->tempArtifact . '/ocramius-package-versions-1.0.0.zip', ZipArchive::CREATE); + + $files = array_filter( + iterator_to_array(new RecursiveIteratorIterator( + new RecursiveCallbackFilterIterator( + new RecursiveDirectoryIterator(realpath(__DIR__ . '/../../'), RecursiveDirectoryIterator::SKIP_DOTS), + function (SplFileInfo $file) { + $filePath = substr($file->getRealPath(), strlen(realpath(__DIR__ . '/../../')) + 1); + + if (substr($filePath, 0, 4) === '.git' + || substr($filePath, 0, 5) === '.idea' + || substr($filePath, 0, 6) === 'vendor' + ) { + return false; + } + + return true; + } + ), + RecursiveIteratorIterator::LEAVES_ONLY + )), + function (SplFileInfo $file) { + return !$file->isDir(); + } + ); + + array_walk( + $files, + function (SplFileInfo $file) use ($zip) { + if ($file->getFilename() === 'composer.json') { + $contents = json_decode(file_get_contents($file->getRealPath()), true); + $contents['version'] = '1.0.0'; + + return $zip->addFromString('composer.json', json_encode($contents)); + } + + $zip->addFile( + $file->getRealPath(), + substr($file->getRealPath(), strlen(realpath(__DIR__ . '/../../')) + 1) + ); + } + ); + + $zip->close(); + } + + private function createArtifact() + { + $zip = new ZipArchive(); + + $zip->open($this->tempArtifact . '/test-package-2.0.0.zip', ZipArchive::CREATE); + $zip->addFromString('composer.json', json_encode([ + 'name' => 'test/package', + 'version' => '2.0.0', + ], JSON_PRETTY_PRINT)). + $zip->close(); + } + + private function execInDir(string $command, string $dir) : array + { + $currentDir = getcwd(); + chdir($dir); + $output = $this->exec($command); + chdir($currentDir); + return $output; + } + + private function exec(string $command) : array + { + exec($command . ' 2> /dev/null', $output); + return $output; + } + + /** + * @param string $directory + * + * @return void + */ + private function rmDir(string $directory) + { + if (! is_dir($directory)) { + unlink($directory); + + return; + } + + array_map( + function ($item) use ($directory) { + $this->rmDir($directory . '/' . $item); + }, + array_filter( + scandir($directory), + function (string $dirItem) { + return ! in_array($dirItem, ['.', '..'], true); + } + ) + ); + + rmdir($directory); + } +} From c2b19bc84414699328a959c85ebd7354fa43fce8 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 26 Oct 2016 20:58:43 +0100 Subject: [PATCH 03/18] Add E2E test for removing plugin --- test/PackageVersionsTest/E2EInstallerTest.php | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index eedc07f..6159d9c 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -55,7 +55,7 @@ public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProjec 'url' => $this->tempArtifact, ] ] - ], JSON_PRETTY_PRINT)); + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $this->exec(__DIR__ . '/../../vendor/bin/composer global update'); @@ -74,11 +74,45 @@ public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProjec 'url' => $this->tempArtifact, ] ] - ], JSON_PRETTY_PRINT)); + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + + $this->execInDir('composer update', $this->tempLocalComposerHome); + $this->assertFileNotExists( + $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' + ); + } + + public function testRemovingPluginDoesNotAttemptToGenerateVersions() + { + $this->createPackageVersionsArtifact(); + $this->createArtifact(); + + file_put_contents($this->tempLocalComposerHome . '/composer.json', json_encode([ + 'name' => 'package-versions/e2e-local', + 'require' => [ + 'test/package' => '2.0.0', + 'ocramius/package-versions' => '1.0.0' + ], + 'repositories' => [ + [ + 'packagist' => false, + ], + [ + 'type' => 'artifact', + 'url' => $this->tempArtifact, + ] + ] + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $this->execInDir('composer update -vvv', $this->tempLocalComposerHome); + $this->assertFileExists( + $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' + ); - $this->assertFileNotExists($this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'); + $this->execInDir('composer remove ocramius/package-versions -vvv', $this->tempLocalComposerHome); + $this->assertFileNotExists( + $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' + ); } private function createPackageVersionsArtifact() From f41e139f069b3d5e2a26ef974e44c990d8c94786 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 12:47:28 +0000 Subject: [PATCH 04/18] require ext-zip in dev --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ae036f6..aca3a6e 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "require-dev": { "phpunit/phpunit": "^5.4.7", - "composer/composer": "^1.2.0" + "composer/composer": "^1.2.0", + "ext-zip": "*" }, "autoload": { "psr-4": { From e33915c8a6be83e80f4b4a4c4df1f004c358fa6b Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 12:48:41 +0000 Subject: [PATCH 05/18] @coversNothing for e2e installer tests --- test/PackageVersionsTest/E2EInstallerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 6159d9c..b8e77e5 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -10,7 +10,7 @@ use ZipArchive; /** - * @author Aydin Hassan + * @coversNothing */ class E2EInstaller extends PHPUnit_Framework_TestCase { From 287fd9107563fbdb57ca4d7ee2189e9dcc0e89c6 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 12:49:34 +0000 Subject: [PATCH 06/18] class var docblocks --- test/PackageVersionsTest/E2EInstallerTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index b8e77e5..4c1b8dd 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -14,8 +14,19 @@ */ class E2EInstaller extends PHPUnit_Framework_TestCase { + /** + * @var string + */ private $tempGlobalComposerHome; + + /** + * @var string + */ private $tempLocalComposerHome; + + /** + * @var string + */ private $tempArtifact; public function setUp() From b0e1aaef1a76900202d5ee5ca3be93b702081ef0 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 12:54:52 +0000 Subject: [PATCH 07/18] alignment --- test/PackageVersionsTest/E2EInstallerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 4c1b8dd..fffbc3b 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -113,7 +113,7 @@ public function testRemovingPluginDoesNotAttemptToGenerateVersions() 'url' => $this->tempArtifact, ] ] - ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $this->execInDir('composer update -vvv', $this->tempLocalComposerHome); $this->assertFileExists( From f12ab0812787753f8a9babbbb17ef9167482fe19 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 12:56:15 +0000 Subject: [PATCH 08/18] Use composer from vendor dir --- test/PackageVersionsTest/E2EInstallerTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index fffbc3b..af54af4 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -87,7 +87,7 @@ public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProjec ] ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); - $this->execInDir('composer update', $this->tempLocalComposerHome); + $this->execInDir(__DIR__ . '/../../vendor/bin/composer update', $this->tempLocalComposerHome); $this->assertFileNotExists( $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' ); @@ -115,12 +115,16 @@ public function testRemovingPluginDoesNotAttemptToGenerateVersions() ] ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); - $this->execInDir('composer update -vvv', $this->tempLocalComposerHome); + $this->execInDir(__DIR__ . '/../../vendor/bin/composer update', $this->tempLocalComposerHome); $this->assertFileExists( $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' ); - $this->execInDir('composer remove ocramius/package-versions -vvv', $this->tempLocalComposerHome); + $this->execInDir( + __DIR__ . '/../../vendor/bin/composer remove ocramius/package-versions', + $this->tempLocalComposerHome + ); + $this->assertFileNotExists( $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' ); From 7ec5c0e92c69199bbe8406a845fd33dd0eda5d27 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 14:24:09 +0000 Subject: [PATCH 09/18] Simplify iterator --- test/PackageVersionsTest/E2EInstallerTest.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index af54af4..2ab7dc1 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -140,17 +140,8 @@ private function createPackageVersionsArtifact() iterator_to_array(new RecursiveIteratorIterator( new RecursiveCallbackFilterIterator( new RecursiveDirectoryIterator(realpath(__DIR__ . '/../../'), RecursiveDirectoryIterator::SKIP_DOTS), - function (SplFileInfo $file) { - $filePath = substr($file->getRealPath(), strlen(realpath(__DIR__ . '/../../')) + 1); - - if (substr($filePath, 0, 4) === '.git' - || substr($filePath, 0, 5) === '.idea' - || substr($filePath, 0, 6) === 'vendor' - ) { - return false; - } - - return true; + function (SplFileInfo $file, $key, RecursiveDirectoryIterator $iterator) { + return $iterator->getSubPathname()[0] !== '.' && $iterator->getSubPathname() !== 'vendor'; } ), RecursiveIteratorIterator::LEAVES_ONLY From 5c62f98c905717471da32fe219d1021ce609822c Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 14:28:54 +0000 Subject: [PATCH 10/18] Assert exec exit code --- test/PackageVersionsTest/E2EInstallerTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 2ab7dc1..8d0ebd8 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -194,7 +194,8 @@ private function execInDir(string $command, string $dir) : array private function exec(string $command) : array { - exec($command . ' 2> /dev/null', $output); + exec($command . ' 2> /dev/null', $output, $exitCode); + $this->assertEquals(0, $exitCode); return $output; } From ef6de8cd134dabd46af6d5a3bc34da86d00c3668 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 14:47:38 +0000 Subject: [PATCH 11/18] fix alignment and introduce method for writing composer files --- test/PackageVersionsTest/E2EInstallerTest.php | 115 +++++++++++------- 1 file changed, 69 insertions(+), 46 deletions(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 8d0ebd8..56b5a33 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -52,40 +52,46 @@ public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProjec { $this->createPackageVersionsArtifact(); - file_put_contents($this->tempGlobalComposerHome . '/composer.json', json_encode([ - 'name' => 'package-versions/e2e-global', - 'require' => [ - 'ocramius/package-versions' => '1.0.0' - ], - 'repositories' => [ - [ - 'packagist' => false, + $this->writeComposerJsonFile( + [ + 'name' => 'package-versions/e2e-global', + 'require' => [ + 'ocramius/package-versions' => '1.0.0' ], - [ - 'type' => 'artifact', - 'url' => $this->tempArtifact, + 'repositories' => [ + [ + 'packagist' => false, + ], + [ + 'type' => 'artifact', + 'url' => $this->tempArtifact, + ] ] - ] - ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + ], + $this->tempGlobalComposerHome + ); $this->exec(__DIR__ . '/../../vendor/bin/composer global update'); $this->createArtifact(); - file_put_contents($this->tempLocalComposerHome . '/composer.json', json_encode([ - 'name' => 'package-versions/e2e-local', - 'require' => [ - 'test/package' => '2.0.0' - ], - 'repositories' => [ - [ - 'packagist' => false, + $this->writeComposerJsonFile( + [ + 'name' => 'package-versions/e2e-local', + 'require' => [ + 'test/package' => '2.0.0' ], - [ - 'type' => 'artifact', - 'url' => $this->tempArtifact, + 'repositories' => [ + [ + 'packagist' => false, + ], + [ + 'type' => 'artifact', + 'url' => $this->tempArtifact, + ] ] - ] - ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + ], + $this->tempLocalComposerHome + ); $this->execInDir(__DIR__ . '/../../vendor/bin/composer update', $this->tempLocalComposerHome); $this->assertFileNotExists( @@ -98,22 +104,25 @@ public function testRemovingPluginDoesNotAttemptToGenerateVersions() $this->createPackageVersionsArtifact(); $this->createArtifact(); - file_put_contents($this->tempLocalComposerHome . '/composer.json', json_encode([ - 'name' => 'package-versions/e2e-local', - 'require' => [ - 'test/package' => '2.0.0', - 'ocramius/package-versions' => '1.0.0' - ], - 'repositories' => [ - [ - 'packagist' => false, - ], - [ - 'type' => 'artifact', - 'url' => $this->tempArtifact, - ] - ] - ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + $this->writeComposerJsonFile( + [ + 'name' => 'package-versions/e2e-local', + 'require' => [ + 'test/package' => '2.0.0', + 'ocramius/package-versions' => '1.0.0' + ], + 'repositories' => [ + [ + 'packagist' => false, + ], + [ + 'type' => 'artifact', + 'url' => $this->tempArtifact, + ] + ] + ], + $this->tempLocalComposerHome + ); $this->execInDir(__DIR__ . '/../../vendor/bin/composer update', $this->tempLocalComposerHome); $this->assertFileExists( @@ -176,13 +185,27 @@ private function createArtifact() $zip = new ZipArchive(); $zip->open($this->tempArtifact . '/test-package-2.0.0.zip', ZipArchive::CREATE); - $zip->addFromString('composer.json', json_encode([ - 'name' => 'test/package', - 'version' => '2.0.0', - ], JSON_PRETTY_PRINT)). + $zip->addFromString( + 'composer.json', + json_encode( + [ + 'name' => 'test/package', + 'version' => '2.0.0' + ], + JSON_PRETTY_PRINT + ) + ); $zip->close(); } + private function writeComposerJsonFile(array $config, string $directory) + { + file_put_contents( + $directory . '/composer.json', + json_encode($config, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) + ); + } + private function execInDir(string $command, string $dir) : array { $currentDir = getcwd(); From 10e40bd3c19c174d3b47debc5d6776209a11e8da Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 30 Oct 2016 14:52:23 +0000 Subject: [PATCH 12/18] refactor exec --- test/PackageVersionsTest/E2EInstallerTest.php | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 56b5a33..5555d6f 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -71,7 +71,7 @@ public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProjec $this->tempGlobalComposerHome ); - $this->exec(__DIR__ . '/../../vendor/bin/composer global update'); + $this->execComposerInDir('global update', $this->tempGlobalComposerHome); $this->createArtifact(); $this->writeComposerJsonFile( @@ -93,7 +93,7 @@ public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProjec $this->tempLocalComposerHome ); - $this->execInDir(__DIR__ . '/../../vendor/bin/composer update', $this->tempLocalComposerHome); + $this->execComposerInDir('update', $this->tempLocalComposerHome); $this->assertFileNotExists( $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' ); @@ -124,15 +124,12 @@ public function testRemovingPluginDoesNotAttemptToGenerateVersions() $this->tempLocalComposerHome ); - $this->execInDir(__DIR__ . '/../../vendor/bin/composer update', $this->tempLocalComposerHome); + $this->execComposerInDir('update', $this->tempLocalComposerHome); $this->assertFileExists( $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' ); - $this->execInDir( - __DIR__ . '/../../vendor/bin/composer remove ocramius/package-versions', - $this->tempLocalComposerHome - ); + $this->execComposerInDir('remove ocramius/package-versions', $this->tempLocalComposerHome); $this->assertFileNotExists( $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php' @@ -206,19 +203,13 @@ private function writeComposerJsonFile(array $config, string $directory) ); } - private function execInDir(string $command, string $dir) : array + private function execComposerInDir(string $command, string $dir) : array { $currentDir = getcwd(); chdir($dir); - $output = $this->exec($command); - chdir($currentDir); - return $output; - } - - private function exec(string $command) : array - { - exec($command . ' 2> /dev/null', $output, $exitCode); + exec(__DIR__ . '/../../vendor/bin/composer ' . $command . ' 2> /dev/null', $output, $exitCode); $this->assertEquals(0, $exitCode); + chdir($currentDir); return $output; } From 19da477a1ce931f8f4101145e91d12a64513531d Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Mon, 31 Oct 2016 08:56:09 +0000 Subject: [PATCH 13/18] Bump symfony/process to ^2.3 which is actually minimum for composer/composer. Refs: https://github.com/composer/composer/issues/5828 --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index aca3a6e..25641a8 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "require-dev": { "phpunit/phpunit": "^5.4.7", "composer/composer": "^1.2.0", - "ext-zip": "*" + "ext-zip": "*", + "symfony/process": "^2.3" }, "autoload": { "psr-4": { From 3879b3ccd86a7b36212c9c5eb5ec56ec78106b2b Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Thu, 3 Nov 2016 17:29:52 +0000 Subject: [PATCH 14/18] bump composer version --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 25641a8..f34a5d1 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,8 @@ }, "require-dev": { "phpunit/phpunit": "^5.4.7", - "composer/composer": "^1.2.0", - "ext-zip": "*", - "symfony/process": "^2.3" + "composer/composer": "^1.2.2", + "ext-zip": "*" }, "autoload": { "psr-4": { From c054b5e33c33ae626ad9250a4d1ea0204605bcf6 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Sun, 25 Dec 2016 17:05:44 +0100 Subject: [PATCH 15/18] bump composer to 1.3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f34a5d1..3f4931f 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ }, "require-dev": { "phpunit/phpunit": "^5.4.7", - "composer/composer": "^1.2.2", + "composer/composer": "^1.3", "ext-zip": "*" }, "autoload": { From c03754cc1375fbc174f9910d9b852d10ef0333e3 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Thu, 29 Dec 2016 10:35:53 +0100 Subject: [PATCH 16/18] array_key_exists instead of isset --- src/PackageVersions/Installer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PackageVersions/Installer.php b/src/PackageVersions/Installer.php index aa067d7..81ff5b4 100644 --- a/src/PackageVersions/Installer.php +++ b/src/PackageVersions/Installer.php @@ -83,7 +83,7 @@ public static function dumpVersionsClass(Event $composerEvent) $composer = $composerEvent->getComposer(); $versions = iterator_to_array(self::getVersions($composer->getLocker(), $composer->getPackage())); - if (!isset($versions['ocramius/package-versions'])) { + if (!array_key_exists('ocramius/package-versions', $versions)) { //plugin must be globally installed - we only want to generate versions for projects which specifically //require ocramius/package-versions return; From b0e4201f7291c6236b7cfc5e70a0d41ff4c2a5ef Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Thu, 29 Dec 2016 10:36:50 +0100 Subject: [PATCH 17/18] 0700 permissions --- test/PackageVersionsTest/E2EInstallerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 5555d6f..ffd132e 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -34,9 +34,9 @@ public function setUp() $this->tempGlobalComposerHome = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/global'; $this->tempLocalComposerHome = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/local'; $this->tempArtifact = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/artifacts'; - mkdir($this->tempGlobalComposerHome, 0777, true); - mkdir($this->tempLocalComposerHome, 0777, true); - mkdir($this->tempArtifact, 0777, true); + mkdir($this->tempGlobalComposerHome, 0700, true); + mkdir($this->tempLocalComposerHome, 0700, true); + mkdir($this->tempArtifact, 0700, true); putenv('COMPOSER_HOME=' . $this->tempGlobalComposerHome); } From 0180eabe736ab83d6cc17c1e6e6270c9837076fe Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Thu, 29 Dec 2016 10:39:18 +0100 Subject: [PATCH 18/18] Unset COMPOSER_HOME env variable in tearDown --- test/PackageVersionsTest/E2EInstallerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index ffd132e..8694edd 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -46,6 +46,8 @@ public function tearDown() $this->rmDir($this->tempGlobalComposerHome); $this->rmDir($this->tempLocalComposerHome); $this->rmDir($this->tempArtifact); + + putenv('COMPOSER_HOME'); } public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProject()