-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
UnpackerTest.php
101 lines (77 loc) · 3.28 KB
/
UnpackerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Symfony\Flex\Tests;
use Composer\Composer;
use Composer\Package\Link;
use Composer\Package\Package;
use Composer\Repository\InstalledArrayRepository;
use Composer\Repository\RepositoryManager;
use Composer\Semver\Constraint\MatchAllConstraint;
use PHPUnit\Framework\TestCase;
use Symfony\Flex\PackageResolver;
use Symfony\Flex\Unpack\Operation;
use Symfony\Flex\Unpacker;
class UnpackerTest extends TestCase
{
/**
* Shared dependencies must be present once in the composer.json file.
*
* Context:
*
* - There are two packs: "pack_foo" and "pack_bar"
* - Both points to a package named "real"
* - "pack_foo" is present in the "require" section
* - "pack_bar" is present in the "require-dev" section
*
* Expected result:
*
* - "real" package MUST be present ONLY in "require" section
*/
public function testDoNotDuplicateEntry(): void
{
// 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', class_exists(MatchAllConstraint::class) ? new MatchAllConstraint() : null, 'wraps', '1.0.0');
$virtualPkgFoo = new Package('pack_foo', '1.0.0', '1.0.0');
$virtualPkgFoo->setType('symfony-pack');
$virtualPkgFoo->setRequires(['real' => $realPkgLink]);
$virtualPkgBar = new Package('pack_bar', '1.0.0', '1.0.0');
$virtualPkgBar->setType('symfony-pack');
$virtualPkgBar->setRequires(['real' => $realPkgLink]);
$packages = [$realPkg, $virtualPkgFoo, $virtualPkgBar];
// 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, false);
$operation = new Operation(true, false);
$operation->addPackage('pack_foo', '*', false);
$operation->addPackage('pack_bar', '*', true);
$unpacker->unpack($operation);
// Check
$composerJson = json_decode(file_get_contents($composerJsonPath), true);
$this->assertArrayHasKey('require', $composerJson);
$this->assertArrayHasKey('real', $composerJson['require']);
$this->assertArrayNotHasKey('require-dev', $composerJson);
// Restore
if ($originalEnvComposer) {
$_SERVER['COMPOSER'] = $originalEnvComposer;
} else {
unset($_SERVER['COMPOSER']);
}
// composer 2.1 and lower support
putenv('COMPOSER='.$originalEnvComposer);
@unlink($composerJsonPath);
}
}