Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Unpacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Composer $composer, PackageResolver $resolver)
$this->versionParser = new VersionParser();
}

public function unpack(Operation $op, ?Result $result = null, &$links = [], bool $devRequire = false): Result
public function unpack(Operation $op, ?Result $result = null, &$links = [], bool $devRequire = false, &$replaces = [], &$provides = []): Result
{
if (null === $result) {
$result = new Result();
Expand Down Expand Up @@ -95,7 +95,7 @@ public function unpack(Operation $op, ?Result $result = null, &$links = [], bool
if ('symfony-pack' === $subPkg->getType()) {
$subOp = new Operation(true, $op->shouldSort());
$subOp->addPackage($subPkg->getName(), $constraint, $dev);
$result = $this->unpack($subOp, $result, $links, $dev);
$result = $this->unpack($subOp, $result, $links, $dev, $replaces, $provides);
continue;
}

Expand Down Expand Up @@ -127,6 +127,13 @@ public function unpack(Operation $op, ?Result $result = null, &$links = [], bool
}
}
}

foreach ($pkg->getReplaces() as $link) {
$replaces[$link->getTarget()] = $link->getPrettyConstraint();
}
foreach ($pkg->getProvides() as $link) {
$provides[$link->getTarget()] = $link->getPrettyConstraint();
}
}

if (1 < \func_num_args()) {
Expand Down Expand Up @@ -170,6 +177,18 @@ public function unpack(Operation $op, ?Result $result = null, &$links = [], bool
}
}

foreach ($replaces as $name => $constraint) {
if (!isset($jsonStored['replace'][$name])) {
$jsonManipulator->addLink('replace', $name, $constraint, $op->shouldSort());
}
}

foreach ($provides as $name => $constraint) {
if (!isset($jsonStored['provide'][$name])) {
$jsonManipulator->addLink('provide', $name, $constraint, $op->shouldSort());
}
}

file_put_contents($jsonPath, $jsonManipulator->getContents());

return $result;
Expand Down
8 changes: 7 additions & 1 deletion tests/Command/DumpEnvCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Composer\Config;
use Composer\Console\Application;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Flex\Command\DumpEnvCommand;
Expand Down Expand Up @@ -109,10 +111,14 @@ public function testEnvCanBeReferenced()

$this->assertFileExists($envLocal);

// With dotenv >= 6.4.35/7.4.7/8.0.7, variable resolution is deferred so BAR=$FOO
// resolves using .env's FOO value. With older versions, eager resolution uses system env.
$deferredResolution = InstalledVersions::satisfies(new VersionParser(), 'symfony/dotenv', '^6.4.35 | ^7.4.7 | >=8.0.7');

$vars = require $envLocal;
$this->assertSame([
'APP_ENV' => 'prod',
'BAR' => 'Foo',
'BAR' => $deferredResolution ? '123' : 'Foo',
'FOO' => '123',
], $vars);

Expand Down
78 changes: 77 additions & 1 deletion tests/UnpackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UnpackerTest extends TestCase
*
* - "real" package MUST be present ONLY in "require" section
*/
public function testDoNotDuplicateEntry(): void
public function testDoNotDuplicateEntry()
{
// Setup project

Expand Down Expand Up @@ -98,4 +98,80 @@ public function testDoNotDuplicateEntry(): void
putenv('COMPOSER='.$originalEnvComposer);
@unlink($composerJsonPath);
}

/**
* When unpacking a pack, its "replace" and "provide" entries must be
* copied to the root composer.json.
*/
public function testUnpackCopiesReplaceAndProvide()
{
// Setup project

$composerJsonPath = FLEX_TEST_DIR.'/composer.json';

@mkdir(FLEX_TEST_DIR);
@unlink($composerJsonPath);
file_put_contents($composerJsonPath, '{}');

$originalEnvComposer = $_SERVER['COMPOSER'];
$_SERVER['COMPOSER'] = $composerJsonPath;
// composer 2.1 and lower support
putenv('COMPOSER='.$composerJsonPath);

// Setup packages

$realPkg = new Package('real', '1.0.0', '1.0.0');
$realPkgLink = new Link('lorem', 'real', new MatchAllConstraint(), 'wraps', '1.0.0');

$virtualPkg = new Package('pack_foo', '1.0.0', '1.0.0');
$virtualPkg->setType('symfony-pack');
$virtualPkg->setRequires(['real' => $realPkgLink]);
$virtualPkg->setReplaces([
'old/package' => new Link('pack_foo', 'old/package', new MatchAllConstraint(), 'replaces', 'self.version'),
]);
$virtualPkg->setProvides([
'some/capability' => new Link('pack_foo', 'some/capability', new MatchAllConstraint(), 'provides', 'self.version'),
]);

$packages = [$realPkg, $virtualPkg];

// Setup Composer

$repManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
$repManager->expects($this->any())->method('getLocalRepository')->willReturn(new InstalledArrayRepository($packages));

$composer = new Composer();
$composer->setRepositoryManager($repManager);

// Unpack

$resolver = $this->getMockBuilder(PackageResolver::class)->disableOriginalConstructor()->getMock();

$unpacker = new Unpacker($composer, $resolver);

$operation = new Operation(true, false);
$operation->addPackage('pack_foo', '*', false);

$unpacker->unpack($operation);

// Check

$composerJson = json_decode(file_get_contents($composerJsonPath), true);

$this->assertArrayHasKey('replace', $composerJson);
$this->assertArrayHasKey('old/package', $composerJson['replace']);
$this->assertArrayHasKey('provide', $composerJson);
$this->assertArrayHasKey('some/capability', $composerJson['provide']);

// Restore

if ($originalEnvComposer) {
$_SERVER['COMPOSER'] = $originalEnvComposer;
} else {
unset($_SERVER['COMPOSER']);
}
// composer 2.1 and lower support
putenv('COMPOSER='.$originalEnvComposer);
@unlink($composerJsonPath);
}
}
Loading