Skip to content

Commit b11f06e

Browse files
Extra bundles tests
1 parent cf7aa3d commit b11f06e

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

tests/FlexExtraBundlesTest.php

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Flex\Tests;
13+
14+
use Composer\Composer;
15+
use Composer\DependencyResolver\Operation\InstallOperation;
16+
use Composer\Installer\PackageEvent;
17+
use Composer\IO\BufferIO;
18+
use Composer\Package\Locker;
19+
use Composer\Package\Package;
20+
use Composer\Package\RootPackageInterface;
21+
use Composer\Script\Event;
22+
use PHPUnit\Framework\TestCase;
23+
use Symfony\Component\Console\Output\OutputInterface;
24+
use Symfony\Flex\Configurator;
25+
use Symfony\Flex\Downloader;
26+
use Symfony\Flex\Flex;
27+
use Symfony\Flex\Lock;
28+
use Symfony\Flex\Options;
29+
use Symfony\Flex\Recipe;
30+
31+
class FlexExtraBundlesTest extends TestCase
32+
{
33+
/**
34+
* @dataProvider getPackagesWithExtraBundles
35+
*/
36+
public function testExtraBundles(Package $package, array $expectedManifest, string $expectedExceptionClass = null)
37+
{
38+
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
39+
$event = $this->mockPackageEvent(new InstallOperation($package));
40+
41+
$installCalls = [];
42+
if (\count($expectedManifest)) {
43+
$installCalls['install'] = new Recipe($package, $package->getName(), 'install', $expectedManifest);
44+
}
45+
$configurator = $this->mockConfigurator($installCalls);
46+
if ($expectedExceptionClass) {
47+
$this->expectException($expectedExceptionClass);
48+
}
49+
50+
$downloader = $this->mockDownloader();
51+
$locker = $this->mockLocker();
52+
$composer = $this->getComposer($locker, $package);
53+
$lock = $this->mockLock();
54+
55+
$flex = \Closure::bind(function () use ($configurator, $downloader, $io, $composer, $lock) {
56+
$flex = new Flex();
57+
$flex->composer = $composer;
58+
$flex->io = $io;
59+
$flex->configurator = $configurator;
60+
$flex->downloader = $downloader;
61+
$flex->runningCommand = function () {
62+
};
63+
$flex->options = new Options(['config-dir' => 'config', 'var-dir' => 'var']);
64+
$flex->lock = $lock;
65+
66+
return $flex;
67+
}, null, Flex::class)->__invoke();
68+
69+
$flex->record($event);
70+
$flex->install($this->getMockBuilder(Event::class)->disableOriginalConstructor()->getMock());
71+
}
72+
73+
public function getPackagesWithExtraBundles(): array
74+
{
75+
$extraBundle = new Package('dummy/dummy', '1.0.0', '1.0.0');
76+
$extraBundle->setExtra(['symfony' => ['bundles' => ['Dummy\Dummy' => ['all']]]]);
77+
78+
$emptyExtraBundles = new Package('dummy/dummy2', '1.0.0', '1.0.0');
79+
$emptyExtraBundles->setExtra(['symfony' => ['bundles' => []]]);
80+
81+
$noExtraBundle = new Package('symfony/debug-bundle', '1.0.0', '1.0.0');
82+
$noExtraBundle->setAutoload(['psr-4' => ['Symfony\\Bundle\\DebugBundle\\' => '']]);
83+
84+
$invalidExtraBundle = new Package('dummy/dummy3', '1.0.0', '1.0.0');
85+
$invalidExtraBundle->setExtra(['symfony' => ['bundles' => 'BundleName']]);
86+
87+
$invalidExtraBundleEnvs = new Package('dummy/dummy4', '1.0.0', '1.0.0');
88+
$invalidExtraBundleEnvs->setExtra(['symfony' => ['bundles' => ['Dummy\Dummy' => 'all']]]);
89+
90+
$invalidExtraBundleNonStringEnv = new Package('dummy/dummy5', '1.0.0', '1.0.0');
91+
$invalidExtraBundleNonStringEnv->setExtra(['symfony' => ['bundles' => ['Dummy\Dummy' => [['all']]]]]);
92+
93+
return [
94+
[
95+
$extraBundle,
96+
[
97+
'origin' => sprintf('%s:%s@auto-generated recipe', $extraBundle->getName(), $extraBundle->getPrettyVersion()),
98+
'manifest' => [
99+
'bundles' => [
100+
'Dummy\Dummy' => ['all'],
101+
],
102+
],
103+
],
104+
],
105+
[
106+
$emptyExtraBundles,
107+
[],
108+
],
109+
[
110+
$noExtraBundle,
111+
[
112+
'origin' => sprintf('%s:%s@auto-generated recipe', $noExtraBundle->getName(), $noExtraBundle->getPrettyVersion()),
113+
'manifest' => [
114+
'bundles' => [
115+
'Symfony\Bundle\DebugBundle\DebugBundle' => ['all'],
116+
],
117+
],
118+
],
119+
],
120+
[
121+
$invalidExtraBundle,
122+
[],
123+
\InvalidArgumentException::class,
124+
],
125+
[
126+
$invalidExtraBundleEnvs,
127+
[],
128+
\InvalidArgumentException::class,
129+
],
130+
[
131+
$invalidExtraBundleNonStringEnv,
132+
[],
133+
\InvalidArgumentException::class,
134+
],
135+
];
136+
}
137+
138+
private function mockPackageEvent(InstallOperation $operation): PackageEvent
139+
{
140+
$event = $this->getMockBuilder(PackageEvent::class, ['getOperation'])->disableOriginalConstructor()->getMock();
141+
142+
$event->expects($this->any())->method('getOperation')->willReturn($operation);
143+
144+
return $event;
145+
}
146+
147+
private function mockConfigurator(array $expectedCalls = []): Configurator
148+
{
149+
$configurator = $this->getMockBuilder(Configurator::class)->disableOriginalConstructor()->getMock();
150+
151+
foreach ($expectedCalls as $call => $param) {
152+
$configurator->expects($this->once())->method($call)->with($this->equalTo($param));
153+
}
154+
155+
return $configurator;
156+
}
157+
158+
private function mockDownloader(): Downloader
159+
{
160+
$downloader = $this->getMockBuilder(Downloader::class)->disableOriginalConstructor()->getMock();
161+
162+
$downloader->expects($this->once())->method('getRecipes')->willReturn([]);
163+
$downloader->expects($this->once())->method('isEnabled')->willReturn(true);
164+
165+
return $downloader;
166+
}
167+
168+
private function mockLocker(array $lockData = []): Locker
169+
{
170+
$locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock();
171+
172+
$locker->expects($this->any())->method('getLockData')->willReturn(['content-hash' => 'random', 'packages-dev' => []] + $lockData);
173+
174+
return $locker;
175+
}
176+
177+
private function getComposer(Locker $locker, Package $package): Composer
178+
{
179+
$config = $this->getMockBuilder('Composer\Config')->getMock();
180+
$config->expects($this->any())->method('get')->willReturn(__DIR__.'/Fixtures/vendor');
181+
182+
$composer = new Composer();
183+
$composer->setLocker($locker);
184+
$composer->setConfig($config);
185+
186+
$rootPackage = $this->getMockBuilder(RootPackageInterface::class)->disableOriginalConstructor()->getMock();
187+
$rootPackage->expects($this->any())->method('getExtra')->willReturn($package->getExtra());
188+
$composer->setPackage($rootPackage);
189+
190+
return $composer;
191+
}
192+
193+
private function mockLock(): Lock
194+
{
195+
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
196+
$lock->expects($this->any())->method('has')->willReturn(false);
197+
198+
return $lock;
199+
}
200+
}

0 commit comments

Comments
 (0)