1
0
Fork 0

feat: improve Composer's output reproducibility (#11663)

* AutoloadGenerator: add `Locker` parameter to the `dump` method
* AutoloadGenerator: do not create a random hash, re-use the one from the lock file if it exists
* FileSystem: make sure `safeCopy` copy also the file time metadata
pull/11670/head
Pol Dellaiera 2023-09-28 11:43:52 +02:00 committed by GitHub
parent 77fadf0e1e
commit b608b8e87e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 187 additions and 56 deletions

View File

@ -152,6 +152,13 @@ a Composer `install` to make sure the vendor directory is up in sync with your
php composer.phar install php composer.phar install
``` ```
Composer enables reproducible builds by default. This means that running the
same command multiple times will produce a `vendor/` directory containing files
that are identical (*except their timestamps*), including the autoloader files.
It is especially beneficial for environments that require strict
verification processes, as well as for Linux distributions aiming to package PHP
applications in a secure and predictable manner.
## Updating dependencies to their latest versions ## Updating dependencies to their latest versions
As mentioned above, the `composer.lock` file prevents you from automatically getting As mentioned above, the `composer.lock` file prevents you from automatically getting

View File

@ -365,8 +365,10 @@ with other autoloaders.
## autoloader-suffix ## autoloader-suffix
Defaults to `null`. Non-empty string to be used as a suffix for the generated Defaults to `null`. When set to a non-empty string, this value will be used as a
Composer autoloader. When null a random one will be generated. suffix for the generated Composer autoloader. If set to `null`, the
`content-hash` value from the `composer.lock` file will be used if available;
otherwise, a random suffix will be generated.
## optimize-autoloader ## optimize-autoloader

View File

@ -33,6 +33,7 @@ use Composer\Util\Platform;
use Composer\Script\ScriptEvents; use Composer\Script\ScriptEvents;
use Composer\Util\PackageSorter; use Composer\Util\PackageSorter;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Package\Locker;
/** /**
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
@ -172,7 +173,7 @@ class AutoloadGenerator
* @throws \Seld\JsonLint\ParsingException * @throws \Seld\JsonLint\ParsingException
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, string $targetDir, bool $scanPsrPackages = false, ?string $suffix = null) public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, Locker $locker, string $targetDir, bool $scanPsrPackages = false, ?string $suffix = null)
{ {
if ($this->classMapAuthoritative) { if ($this->classMapAuthoritative) {
// Force scanPsrPackages when classmap is authoritative // Force scanPsrPackages when classmap is authoritative
@ -405,9 +406,8 @@ EOF;
} }
} }
// generate one if we still haven't got a suffix
if (null === $suffix) { if (null === $suffix) {
$suffix = md5(uniqid('', true)); $suffix = $locker->isLocked() ? $locker->getLockData()['content-hash'] : md5(uniqid('', true));
} }
} }

View File

@ -100,7 +100,15 @@ EOT
$generator->setRunScripts(true); $generator->setRunScripts(true);
$generator->setApcu($apcu, $apcuPrefix); $generator->setApcu($apcu, $apcuPrefix);
$generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input));
$classMap = $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); $classMap = $generator->dump(
$config,
$localRepo,
$package,
$installationManager,
$composer->getLocker(),
'composer',
$optimize
);
$numberOfClasses = count($classMap); $numberOfClasses = count($classMap);
if ($authoritative) { if ($authoritative) {

View File

@ -163,7 +163,15 @@ EOT
$generator->setClassMapAuthoritative($authoritative); $generator->setClassMapAuthoritative($authoritative);
$generator->setApcu($apcu, $apcuPrefix); $generator->setApcu($apcu, $apcuPrefix);
$generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input));
$generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); $generator->dump(
$config,
$localRepo,
$package,
$installationManager,
$composer->getLocker(),
'composer',
$optimize
);
} }
$eventDispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, $devMode); $eventDispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, $devMode);

View File

@ -349,7 +349,17 @@ class Installer
$this->autoloadGenerator->setApcu($this->apcuAutoloader, $this->apcuAutoloaderPrefix); $this->autoloadGenerator->setApcu($this->apcuAutoloader, $this->apcuAutoloaderPrefix);
$this->autoloadGenerator->setRunScripts($this->runScripts); $this->autoloadGenerator->setRunScripts($this->runScripts);
$this->autoloadGenerator->setPlatformRequirementFilter($this->platformRequirementFilter); $this->autoloadGenerator->setPlatformRequirementFilter($this->platformRequirementFilter);
$this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader); $this
->autoloadGenerator
->dump(
$this->config,
$localRepo,
$this->package,
$this->installationManager,
$this->locker,
'composer',
$this->optimizeAutoloader
);
} }
if ($this->install && $this->executeOperations) { if ($this->install && $this->executeOperations) {

View File

@ -874,10 +874,8 @@ class Filesystem
/** /**
* Copy file using stream_copy_to_stream to work around https://bugs.php.net/bug.php?id=6463 * Copy file using stream_copy_to_stream to work around https://bugs.php.net/bug.php?id=6463
*
* @return void
*/ */
public function safeCopy(string $source, string $target) public function safeCopy(string $source, string $target): void
{ {
if (!file_exists($target) || !file_exists($source) || !$this->filesAreEqual($source, $target)) { if (!file_exists($target) || !file_exists($source) || !$this->filesAreEqual($source, $target)) {
$sourceHandle = fopen($source, 'r'); $sourceHandle = fopen($source, 'r');
@ -888,6 +886,8 @@ class Filesystem
stream_copy_to_stream($sourceHandle, $targetHandle); stream_copy_to_stream($sourceHandle, $targetHandle);
fclose($sourceHandle); fclose($sourceHandle);
fclose($targetHandle); fclose($targetHandle);
touch($target, (int) filemtime($source), (int) fileatime($source));
} }
} }

View File

@ -15,7 +15,6 @@ namespace Composer\Test\Autoload;
use Composer\Autoload\AutoloadGenerator; use Composer\Autoload\AutoloadGenerator;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory; use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\IO\BufferIO; use Composer\IO\BufferIO;
use Composer\IO\IOInterface;
use Composer\Package\CompletePackage; use Composer\Package\CompletePackage;
use Composer\Package\Link; use Composer\Package\Link;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
@ -31,6 +30,7 @@ use Composer\Repository\InstalledRepositoryInterface;
use Composer\Installer\InstallationManager; use Composer\Installer\InstallationManager;
use Composer\Config; use Composer\Config;
use Composer\EventDispatcher\EventDispatcher; use Composer\EventDispatcher\EventDispatcher;
use Composer\Package\Locker;
use Composer\Util\Platform; use Composer\Util\Platform;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -81,6 +81,11 @@ class AutoloadGeneratorTest extends TestCase
*/ */
private $io; private $io;
/**
* @var Locker
*/
private $locker;
/** /**
* @var EventDispatcher&MockObject * @var EventDispatcher&MockObject
*/ */
@ -159,6 +164,10 @@ class AutoloadGeneratorTest extends TestCase
->getMock(); ->getMock();
$this->generator = new AutoloadGenerator($this->eventDispatcher, $this->io); $this->generator = new AutoloadGenerator($this->eventDispatcher, $this->io);
$this->locker = $this->getMockBuilder('Composer\Package\Locker')
->disableOriginalConstructor()
->getMock();
} }
protected function tearDown(): void protected function tearDown(): void
@ -208,7 +217,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->workingDir.'/composersrc'); $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}'); file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_1');
// Assert that autoload_namespaces.php was correctly generated. // Assert that autoload_namespaces.php was correctly generated.
$this->assertAutoloadFiles('main', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
@ -248,7 +257,7 @@ class AutoloadGeneratorTest extends TestCase
// generate autoload files with the dev mode set to true // generate autoload files with the dev mode set to true
$this->generator->setDevMode(true); $this->generator->setDevMode(true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_1');
// check standard autoload // check standard autoload
$this->assertAutoloadFiles('main5', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main5', $this->vendorDir.'/composer');
@ -281,7 +290,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->workingDir.'/devfiles'); $this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }'); file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_1');
// check standard autoload // check standard autoload
$this->assertAutoloadFiles('main4', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main4', $this->vendorDir.'/composer');
@ -316,7 +325,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->vendorDir.'/composersrc'); $this->fs->ensureDirectoryExists($this->vendorDir.'/composersrc');
file_put_contents($this->vendorDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}'); file_put_contents($this->vendorDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_2');
$this->assertAutoloadFiles('main3', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('psr4_3', $this->vendorDir.'/composer', 'psr4'); $this->assertAutoloadFiles('psr4_3', $this->vendorDir.'/composer', 'psr4');
$this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
@ -345,7 +354,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->workingDir.'/composersrc'); $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc');
file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}'); file_put_contents($this->workingDir.'/composersrc/foo.php', '<?php class ClassMapFoo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_3');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('psr4_2', $this->vendorDir.'/composer', 'psr4'); $this->assertAutoloadFiles('psr4_2', $this->vendorDir.'/composer', 'psr4');
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
@ -374,7 +383,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}'); file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}'); file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'TargetDir'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, 'TargetDir');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_target_dir.php', $this->vendorDir.'/composer/autoload_static.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_target_dir.php', $this->vendorDir.'/composer/autoload_static.php');
@ -400,7 +409,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}'); file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}'); file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesWarning'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, 'FilesWarning');
self::assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_duplicates.php', $this->vendorDir.'/composer/autoload_files.php'); self::assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_duplicates.php', $this->vendorDir.'/composer/autoload_files.php');
$expected = '<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>'.PHP_EOL. $expected = '<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>'.PHP_EOL.
'<warning> - $baseDir . \'/foo.php\'</warning>'.PHP_EOL; '<warning> - $baseDir . \'/foo.php\'</warning>'.PHP_EOL;
@ -431,7 +440,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib'); $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src'); $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_5');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
} }
@ -465,7 +474,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src'); $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib'); $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_5');
$this->assertAutoloadFiles('vendors_meta', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('vendors_meta', $this->vendorDir.'/composer');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
} }
@ -498,7 +507,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib'); $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src'); $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_5');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
} }
@ -526,7 +535,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src/C'); $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src/C');
file_put_contents($this->vendorDir.'/b/b/src/C/C.php', '<?php namespace B\\C; class C {}'); file_put_contents($this->vendorDir.'/b/b/src/C/C.php', '<?php namespace B\\C; class C {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_5'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_5');
$this->assertEquals( $this->assertEquals(
[ [
@ -565,7 +574,7 @@ class AutoloadGeneratorTest extends TestCase
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib'); $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src'); $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_5');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated, even if empty.");
} }
@ -617,7 +626,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->vendorDir.'/d/d/src/D.php', '<?php class D {}'); file_put_contents($this->vendorDir.'/d/d/src/D.php', '<?php class D {}');
file_put_contents($this->vendorDir.'/e/e/src/E.php', '<?php class E {}'); file_put_contents($this->vendorDir.'/e/e/src/E.php', '<?php class E {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_5');
$this->assertAutoloadFiles('classmap9', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap9', $this->vendorDir.'/composer', 'classmap');
} }
@ -656,7 +665,7 @@ class AutoloadGeneratorTest extends TestCase
->method('getCanonicalPackages') ->method('getCanonicalPackages')
->will($this->returnValue([$vendorPackage])); ->will($this->returnValue([$vendorPackage]));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, 'Phar'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, 'Phar');
$this->assertAutoloadFiles('phar', $this->vendorDir . '/composer'); $this->assertAutoloadFiles('phar', $this->vendorDir . '/composer');
$this->assertAutoloadFiles('phar_psr4', $this->vendorDir . '/composer', 'psr4'); $this->assertAutoloadFiles('phar_psr4', $this->vendorDir . '/composer', 'psr4');
@ -676,7 +685,7 @@ class AutoloadGeneratorTest extends TestCase
->method('getCanonicalPackages') ->method('getCanonicalPackages')
->will($this->returnValue([])); ->will($this->returnValue([]));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_8');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
[ [
@ -706,7 +715,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->workingDir.'/psr4/match.php', '<?php namespace psr4; class match {}'); file_put_contents($this->workingDir.'/psr4/match.php', '<?php namespace psr4; class match {}');
file_put_contents($this->workingDir.'/psr4/badfile.php', '<?php namespace psr4; class badclass {}'); file_put_contents($this->workingDir.'/psr4/badfile.php', '<?php namespace psr4; class badclass {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_1');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$expectedClassmap = <<<EOF $expectedClassmap = <<<EOF
@ -753,7 +762,7 @@ EOF;
file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}'); file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}'); file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_6');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
[ [
@ -794,7 +803,7 @@ EOF;
file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}'); file_put_contents($this->vendorDir.'/a/a/target/lib/b.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}'); file_put_contents($this->vendorDir.'/b/b/src/c.php', '<?php class ClassMapBaz {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_6');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
[ [
@ -836,7 +845,7 @@ EOF;
file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}'); file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}'); file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_7');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
[ [
@ -883,7 +892,7 @@ EOF;
$this->generator->setClassMapAuthoritative(true); $this->generator->setClassMapAuthoritative(true);
$this->generator->setApcu(true); $this->generator->setApcu(true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_7');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
@ -932,7 +941,7 @@ EOF;
$this->generator->setClassMapAuthoritative(true); $this->generator->setClassMapAuthoritative(true);
$this->generator->setApcu(true, 'custom\'Prefix'); $this->generator->setApcu(true, 'custom\'Prefix');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_7');
$this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated."); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
@ -982,7 +991,7 @@ EOF;
file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}'); file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}'); file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, 'FilesAutoload');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions.php', $this->vendorDir.'/composer/autoload_static.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions.php', $this->vendorDir.'/composer/autoload_static.php');
@ -1047,20 +1056,20 @@ EOF;
file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}'); file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}'); file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
$this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload'); $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, $this->locker, 'composer', false, 'FilesAutoload');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_static.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_static.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php');
$this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, 'composer', false, 'FilesAutoload'); $this->generator->dump($this->config, $this->repository, $autoloadPackage, $this->im, $this->locker, 'composer', false, 'FilesAutoload');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php');
$this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload'); $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, $this->locker, 'composer', false, 'FilesAutoload');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_static.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_static.php');
@ -1124,7 +1133,7 @@ EOF;
file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}'); file_put_contents($this->vendorDir . '/e/e/testE.php', '<?php function testFilesAutoloadOrderByDependency5() {}');
file_put_contents($this->workingDir . '/root2.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}'); file_put_contents($this->workingDir . '/root2.php', '<?php function testFilesAutoloadOrderByDependencyRoot() {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, 'FilesAutoloadOrder');
$this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php'); $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php');
$this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_static_files_by_dependency.php', $this->vendorDir . '/composer/autoload_static.php'); $this->assertFileContentEquals(__DIR__ . '/Fixtures/autoload_static_files_by_dependency.php', $this->vendorDir . '/composer/autoload_static.php');
@ -1235,7 +1244,7 @@ return array(
EOF; EOF;
$this->generator->dump($this->config, $this->repository, $rootPackage, $this->im, 'composer', true, '_9'); $this->generator->dump($this->config, $this->repository, $rootPackage, $this->im, $this->locker, 'composer', true, '_9');
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace); $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4); $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap); $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
@ -1265,7 +1274,7 @@ EOF;
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_10');
$this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
$this->assertEquals( $this->assertEquals(
@ -1294,7 +1303,7 @@ EOF;
mkdir($this->vendorDir."/composer", 0777, true); mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_11');
$oldIncludePath = get_include_path(); $oldIncludePath = get_include_path();
@ -1323,7 +1332,7 @@ EOF;
mkdir($this->vendorDir."/composer", 0777, true); mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_12');
$oldIncludePath = get_include_path(); $oldIncludePath = get_include_path();
@ -1352,7 +1361,7 @@ EOF;
mkdir($this->vendorDir."/composer", 0777, true); mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, '_12');
$this->assertFileDoesNotExist($this->vendorDir."/composer/include_paths.php"); $this->assertFileDoesNotExist($this->vendorDir."/composer/include_paths.php");
} }
@ -1381,7 +1390,7 @@ EOF;
->will($this->returnValue([])); ->will($this->returnValue([]));
$this->generator->setRunScripts(true); $this->generator->setRunScripts(true);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_8');
} }
public function testUseGlobalIncludePath(): void public function testUseGlobalIncludePath(): void
@ -1400,7 +1409,7 @@ EOF;
$this->fs->ensureDirectoryExists($this->vendorDir.'/a'); $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, 'IncludePath');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php');
$this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_include_path.php', $this->vendorDir.'/composer/autoload_static.php'); $this->assertFileContentEquals(__DIR__.'/Fixtures/autoload_static_include_path.php', $this->vendorDir.'/composer/autoload_static.php');
} }
@ -1461,7 +1470,7 @@ EOF;
$oldVendorDir = $this->vendorDir; $oldVendorDir = $this->vendorDir;
$this->vendorDir = $vendorDir; $this->vendorDir = $vendorDir;
$this->generator->dump($this->config, $this->repository, $package, $im, 'composer', true, '_13'); $this->generator->dump($this->config, $this->repository, $package, $im, $this->locker, 'composer', true, '_13');
$this->vendorDir = $oldVendorDir; $this->vendorDir = $oldVendorDir;
$expectedNamespace = <<<'EOF' $expectedNamespace = <<<'EOF'
@ -1551,7 +1560,7 @@ EOF;
file_put_contents($this->workingDir.'/working-dir/classmap4/foo/classes.php', '<?php namespace Foo; class Boo4 {}'); file_put_contents($this->workingDir.'/working-dir/classmap4/foo/classes.php', '<?php namespace Foo; class Boo4 {}');
file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}'); file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_14');
$expectedNamespace = <<<'EOF' $expectedNamespace = <<<'EOF'
<?php <?php
@ -1618,7 +1627,7 @@ EOF;
$dep->setAutoload([ $dep->setAutoload([
'psr-0' => ['Foo' => './src'], 'psr-0' => ['Foo' => './src'],
]); ]);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_19');
$expectedNamespace = <<<'EOF' $expectedNamespace = <<<'EOF'
<?php <?php
@ -1638,7 +1647,7 @@ EOF;
$dep->setAutoload([ $dep->setAutoload([
'psr-4' => ['Acme\Foo\\' => './src-psr4'], 'psr-4' => ['Acme\Foo\\' => './src-psr4'],
]); ]);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_19');
$expectedPsr4 = <<<'EOF' $expectedPsr4 = <<<'EOF'
<?php <?php
@ -1659,7 +1668,7 @@ EOF;
'classmap' => ['classmap'], 'classmap' => ['classmap'],
]); ]);
try { try {
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_19');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$this->assertSame('Could not scan for classes inside "'.$this->vendorDir.'/dep/a/classmap" which does not appear to be a file nor a folder', $e->getMessage()); $this->assertSame('Could not scan for classes inside "'.$this->vendorDir.'/dep/a/classmap" which does not appear to be a file nor a folder', $e->getMessage());
} }
@ -1667,7 +1676,7 @@ EOF;
$dep->setAutoload([ $dep->setAutoload([
'files' => ['./test.php'], 'files' => ['./test.php'],
]); ]);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_19');
$this->assertStringContainsString("\$vendorDir . '/dep/a/test.php',\n", (string) file_get_contents($this->vendorDir.'/composer/autoload_files.php')); $this->assertStringContainsString("\$vendorDir . '/dep/a/test.php',\n", (string) file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
$package->setAutoload([ $package->setAutoload([
@ -1701,7 +1710,7 @@ EOF;
file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}'); file_put_contents($this->workingDir.'/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}'); file_put_contents($this->workingDir.'/class.php', '<?php namespace Classmap; class Foo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_15'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_15');
$expectedNamespace = <<<'EOF' $expectedNamespace = <<<'EOF'
<?php <?php
@ -1794,7 +1803,7 @@ return array(
EOF; EOF;
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', false, 'VendorSubstring');
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace); $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4); $this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
} }
@ -1862,7 +1871,7 @@ EOF;
: 'ln -s "' . $target . '" "' . $link . '"'; : 'ln -s "' . $target . '" "' . $link . '"';
exec($command); exec($command);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_1');
// Assert that autoload_classmap.php was correctly generated. // Assert that autoload_classmap.php was correctly generated.
$this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
@ -1894,7 +1903,7 @@ EOF;
->will($this->returnValue([])); ->will($this->returnValue([]));
$this->generator->setPlatformRequirementFilter(PlatformRequirementFilterFactory::fromBoolOrList($ignorePlatformReqs)); $this->generator->setPlatformRequirementFilter(PlatformRequirementFilterFactory::fromBoolOrList($ignorePlatformReqs));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1'); $this->generator->dump($this->config, $this->repository, $package, $this->im, $this->locker, 'composer', true, '_1');
if (null === $expectedFixture) { if (null === $expectedFixture) {
$this->assertFileDoesNotExist($this->vendorDir . '/composer/platform_check.php'); $this->assertFileDoesNotExist($this->vendorDir . '/composer/platform_check.php');

View File

@ -102,4 +102,86 @@ class DumpAutoloadCommandTest extends TestCase
$this->expectExceptionMessage('You can not use both --no-dev and --dev as they conflict with each other.'); $this->expectExceptionMessage('You can not use both --no-dev and --dev as they conflict with each other.');
$appTester->run(['command' => 'dump-autoload', '--dev' => true, '--no-dev' => true]); $appTester->run(['command' => 'dump-autoload', '--dev' => true, '--no-dev' => true]);
} }
public function testWithCustomAutoloaderSuffix(): void
{
$dir = $this->initTempComposer([
'config' => [
'autoloader-suffix' => 'Foobar',
],
]);
$appTester = $this->getApplicationTester();
$this->assertSame(0, $appTester->run(['command' => 'dump-autoload']));
self::assertStringContainsString('ComposerAutoloaderInitFoobar', (string) file_get_contents($dir . '/vendor/autoload.php'));
}
public function testWithExistingComposerLockAndAutoloaderSuffix(): void
{
$dir = $this->initTempComposer(
[
'config' => [
'autoloader-suffix' => 'Foobar',
],
],
[],
[
"_readme" => [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash" => "d751713988987e9331980363e24189ce",
"packages" => [],
"packages-dev" => [],
"aliases" => [],
"minimum-stability" => "stable",
"stability-flags" => [],
"prefer-stable" => false,
"prefer-lowest" => false,
"platform" => [],
"platform-dev" => [],
"plugin-api-version" => "2.6.0"
]
);
$appTester = $this->getApplicationTester();
$this->assertSame(0, $appTester->run(['command' => 'dump-autoload']));
self::assertStringContainsString('ComposerAutoloaderInitFoobar', (string) file_get_contents($dir . '/vendor/autoload.php'));
}
public function testWithExistingComposerLockWithoutAutoloaderSuffix(): void
{
$dir = $this->initTempComposer(
[
'name' => 'foo/bar',
],
[],
[
"_readme" => [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash" => "2d4a6be9a93712c5d6a119b26734a047",
"packages" => [],
"packages-dev" => [],
"aliases" => [],
"minimum-stability" => "stable",
"stability-flags" => [],
"prefer-stable" => false,
"prefer-lowest" => false,
"platform" => [],
"platform-dev" => [],
"plugin-api-version" => "2.6.0"
]
);
$appTester = $this->getApplicationTester();
$this->assertSame(0, $appTester->run(['command' => 'dump-autoload']));
self::assertStringContainsString('ComposerAutoloaderInit2d4a6be9a93712c5d6a119b26734a047', (string) file_get_contents($dir . '/vendor/autoload.php'));
}
} }

View File

@ -122,9 +122,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
* @see getApplicationTester * @see getApplicationTester
* @param mixed[] $composerJson * @param mixed[] $composerJson
* @param mixed[] $authJson * @param mixed[] $authJson
* @param mixed[] $composerLock
* @return string the newly created temp dir * @return string the newly created temp dir
*/ */
public function initTempComposer(array $composerJson = [], array $authJson = []): string public function initTempComposer(array $composerJson = [], array $authJson = [], array $composerLock = []): string
{ {
$dir = self::getUniqueTmpDirectory(); $dir = self::getUniqueTmpDirectory();
@ -150,6 +151,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
file_put_contents($dir.'/composer.json', JsonFile::encode($composerJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); file_put_contents($dir.'/composer.json', JsonFile::encode($composerJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
file_put_contents($dir.'/auth.json', JsonFile::encode($authJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); file_put_contents($dir.'/auth.json', JsonFile::encode($authJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
if ($composerLock !== []) {
file_put_contents($dir.'/composer.lock', JsonFile::encode($composerLock, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
return $dir; return $dir;
} }