Skip to content

Commit

Permalink
Merge pull request #48 from botman/composer-install
Browse files Browse the repository at this point in the history
Fix process parameters.
  • Loading branch information
crynobone authored Sep 4, 2020
2 parents 3c42a8f + adb4c87 commit a1506ae
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BotMan\Studio;

use Illuminate\Foundation\Application;
use Illuminate\Support\Composer as BaseComposer;

class Composer extends BaseComposer
Expand All @@ -14,12 +15,30 @@ class Composer extends BaseComposer
*/
public function install($package, callable $callback)
{
$process = $this->getProcess();

$process->setCommandLine(trim($this->findComposer().' require '.$package));
$process = $this->installationCommandProcess($package);

$process->run($callback);

return $process->isSuccessful();
}

/**
* Get installation command for process.
*
* @param string $package
* @return \Symfony\Component\Process\Process
*/
protected function installationCommandProcess($package)
{
if (version_compare(Application::VERSION, '5.8.0', '<')) {
$process = $this->getProcess();

return $process->setCommandLine(trim($this->findComposer().' require '.$package));
}

$command = $this->findComposer();
array_push($command, 'require', $package);

return $this->getProcess($command);
}
}
46 changes: 46 additions & 0 deletions tests/ComposerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class ComposerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

/** @test */
public function it_install_package()
{
$composer = $this->getMockedComposer();

$composer->install('foo/bar', function () {
$this->assertTrue(true);
});
}

protected function getMockedComposer()
{
$composer = m::mock('BotMan\Studio\Composer[getProcess]', [new Filesystem(), __DIR__])
->shouldAllowMockingProtectedMethods();
$process = m::mock('Symfony\Component\Process\Process');

if (version_compare(Application::VERSION, '5.8.0', '<')) {
$process->shouldReceive('setCommandLine')->with('composer require foo/bar')->andReturnSelf();
$composer->shouldReceive('getProcess')->once()->andReturn($process);
} else {
$composer->shouldReceive('getProcess')->once()->with(['composer', 'require', 'foo/bar'])->andReturn($process);
}

$process->shouldReceive('run')->once()->with(m::type('Closure'))->andReturnUsing(function ($callable) {
$callable();

return 0;
})->shouldReceive('isSuccessful')->once()->andReturnTrue();

return $composer;
}
}

0 comments on commit a1506ae

Please sign in to comment.