Skip to content

Commit

Permalink
[10.x] Fixes incorrect method visibility and add unit tests for `Illu…
Browse files Browse the repository at this point in the history
…minate\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]>
  • Loading branch information
crynobone and StyleCIBot authored Aug 18, 2023
1 parent e766200 commit 81ae53f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
57 changes: 25 additions & 32 deletions src/Illuminate/Support/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,18 @@ public function __construct(Filesystem $files, $workingPath = null)
* @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
* @return bool
*/
protected function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
public function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
{
$composer = $this->findComposer();

$command = explode(' ', $composer);

array_push($command, 'require');

$command = array_merge(
$command,
$packages,
$dev ? ['--dev'] : [],
);

return 0 === (new Process($command, cwd: $this->workingPath, env: ['COMPOSER_MEMORY_LIMIT' => '-1']))
->setTimeout(null)
$command = collect([
...$this->findComposer(),
'require',
...$packages,
])
->when($dev, function ($command) {
$command->push('--dev');
})->all();

return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
->run(
$output instanceof OutputInterface
? function ($type, $line) use ($output) {
Expand All @@ -78,22 +74,18 @@ protected function requirePackages(array $packages, bool $dev = false, Closure|O
* @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
* @return bool
*/
protected function removePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
public function removePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
{
$composer = $this->findComposer();

$command = explode(' ', $composer);

array_push($command, 'remove');

$command = array_merge(
$command,
$packages,
$dev ? ['--dev'] : [],
);

return 0 === (new Process($command, cwd: $this->workingPath, env: ['COMPOSER_MEMORY_LIMIT' => '-1']))
->setTimeout(null)
$command = collect([
...$this->findComposer(),
'remove',
...$packages,
])
->when($dev, function ($command) {
$command->push('--dev');
})->all();

return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
->run(
$output instanceof OutputInterface
? function ($type, $line) use ($output) {
Expand Down Expand Up @@ -182,11 +174,12 @@ protected function phpBinary()
* Get a new Symfony process instance.
*
* @param array $command
* @param array $env
* @return \Symfony\Component\Process\Process
*/
protected function getProcess(array $command)
protected function getProcess(array $command, array $env = [])
{
return (new Process($command, $this->workingPath))->setTimeout(null);
return (new Process($command, $this->workingPath, $env))->setTimeout(null);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions tests/Support/SupportComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ public function testDumpOptimizedTheCorrectCommand()
$composer->dumpOptimized();
}

private function mockComposer(array $expectedProcessArguments, $customComposerPhar = false)
public function testRequirePackagesRunsTheCorrectCommand()
{
$composer = $this->mockComposer(['composer', 'require', 'pestphp/pest:^2.0', 'pestphp/pest-plugin-laravel:^2.0', '--dev']);

$composer->requirePackages(['pestphp/pest:^2.0', 'pestphp/pest-plugin-laravel:^2.0'], true);
}

public function testRemovePackagesRunsTheCorrectCommand()
{
$composer = $this->mockComposer(['composer', 'remove', 'phpunit/phpunit', '--dev']);

$composer->removePackages(['phpunit/phpunit'], true);
}

private function mockComposer(array $expectedProcessArguments, $customComposerPhar = false, array $environmentVariables = [])
{
$directory = __DIR__;

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

$composer = $this->getMockBuilder(Composer::class)
->onlyMethods(['getProcess'])
->setConstructorArgs([$files, $directory])
->setConstructorArgs([$files, $directory, $environmentVariables])
->getMock();
$composer->expects($this->once())
->method('getProcess')
Expand Down

0 comments on commit 81ae53f

Please sign in to comment.