Skip to content

Commit 81ae53f

Browse files
[10.x] Fixes incorrect method visibility and add unit tests for Illuminate\Support\Composer (#48104)
* [10.x] Fixes incorrect method visibility and add unit tests for Illuminate\Support\Composer Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
1 parent e766200 commit 81ae53f

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

src/Illuminate/Support/Composer.php

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,18 @@ public function __construct(Filesystem $files, $workingPath = null)
4646
* @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
4747
* @return bool
4848
*/
49-
protected function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
49+
public function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
5050
{
51-
$composer = $this->findComposer();
52-
53-
$command = explode(' ', $composer);
54-
55-
array_push($command, 'require');
56-
57-
$command = array_merge(
58-
$command,
59-
$packages,
60-
$dev ? ['--dev'] : [],
61-
);
62-
63-
return 0 === (new Process($command, cwd: $this->workingPath, env: ['COMPOSER_MEMORY_LIMIT' => '-1']))
64-
->setTimeout(null)
51+
$command = collect([
52+
...$this->findComposer(),
53+
'require',
54+
...$packages,
55+
])
56+
->when($dev, function ($command) {
57+
$command->push('--dev');
58+
})->all();
59+
60+
return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
6561
->run(
6662
$output instanceof OutputInterface
6763
? function ($type, $line) use ($output) {
@@ -78,22 +74,18 @@ protected function requirePackages(array $packages, bool $dev = false, Closure|O
7874
* @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
7975
* @return bool
8076
*/
81-
protected function removePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
77+
public function removePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
8278
{
83-
$composer = $this->findComposer();
84-
85-
$command = explode(' ', $composer);
86-
87-
array_push($command, 'remove');
88-
89-
$command = array_merge(
90-
$command,
91-
$packages,
92-
$dev ? ['--dev'] : [],
93-
);
94-
95-
return 0 === (new Process($command, cwd: $this->workingPath, env: ['COMPOSER_MEMORY_LIMIT' => '-1']))
96-
->setTimeout(null)
79+
$command = collect([
80+
...$this->findComposer(),
81+
'remove',
82+
...$packages,
83+
])
84+
->when($dev, function ($command) {
85+
$command->push('--dev');
86+
})->all();
87+
88+
return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
9789
->run(
9890
$output instanceof OutputInterface
9991
? function ($type, $line) use ($output) {
@@ -182,11 +174,12 @@ protected function phpBinary()
182174
* Get a new Symfony process instance.
183175
*
184176
* @param array $command
177+
* @param array $env
185178
* @return \Symfony\Component\Process\Process
186179
*/
187-
protected function getProcess(array $command)
180+
protected function getProcess(array $command, array $env = [])
188181
{
189-
return (new Process($command, $this->workingPath))->setTimeout(null);
182+
return (new Process($command, $this->workingPath, $env))->setTimeout(null);
190183
}
191184

192185
/**

tests/Support/SupportComposerTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,21 @@ public function testDumpOptimizedTheCorrectCommand()
4848
$composer->dumpOptimized();
4949
}
5050

51-
private function mockComposer(array $expectedProcessArguments, $customComposerPhar = false)
51+
public function testRequirePackagesRunsTheCorrectCommand()
52+
{
53+
$composer = $this->mockComposer(['composer', 'require', 'pestphp/pest:^2.0', 'pestphp/pest-plugin-laravel:^2.0', '--dev']);
54+
55+
$composer->requirePackages(['pestphp/pest:^2.0', 'pestphp/pest-plugin-laravel:^2.0'], true);
56+
}
57+
58+
public function testRemovePackagesRunsTheCorrectCommand()
59+
{
60+
$composer = $this->mockComposer(['composer', 'remove', 'phpunit/phpunit', '--dev']);
61+
62+
$composer->removePackages(['phpunit/phpunit'], true);
63+
}
64+
65+
private function mockComposer(array $expectedProcessArguments, $customComposerPhar = false, array $environmentVariables = [])
5266
{
5367
$directory = __DIR__;
5468

@@ -60,7 +74,7 @@ private function mockComposer(array $expectedProcessArguments, $customComposerPh
6074

6175
$composer = $this->getMockBuilder(Composer::class)
6276
->onlyMethods(['getProcess'])
63-
->setConstructorArgs([$files, $directory])
77+
->setConstructorArgs([$files, $directory, $environmentVariables])
6478
->getMock();
6579
$composer->expects($this->once())
6680
->method('getProcess')

0 commit comments

Comments
 (0)