Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Added ConnectionAdapterProcessExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
niels-nijens committed Nov 26, 2015
1 parent d1e61f8 commit abd4142
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Deployment/Connection/ConnectionAdapterProcessExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Accompli\Deployment\Connection;

use Accompli\Chrono\Process\ProcessExecutorInterface;

/**
* ConnectionAdapterProcessExecutor.
*
* @author Niels Nijens <nijens.niels@gmail.com>
*/
class ConnectionAdapterProcessExecutor implements ProcessExecutorInterface
{
/**
* The connection adapter instance.
*
* @var ConnectionAdapterInterface
*/
private $connectionAdapter;

/**
* Constructs a new ConnectionAdapterProcessExecutor.
*
* @param ConnectionAdapterInterface $connectionAdapter
*/
public function __construct(ConnectionAdapterInterface $connectionAdapter)
{
$this->connectionAdapter = $connectionAdapter;
}

/**
* {@inheritdoc}
*/
public function isDirectory($path)
{
return $this->connectionAdapter->isDirectory($path);
}

/**
* {@inheritdoc}
*/
public function execute($command, $workingDirectory = null)
{
$previousWorkingDirectory = null;
if ($workingDirectory !== null) {
$previousWorkingDirectory = $this->connectionAdapter->getWorkingDirectory();

$this->connectionAdapter->changeWorkingDirectory($workingDirectory);
}

$result = $this->connectionAdapter->executeCommand($command);

if ($previousWorkingDirectory !== null) {
$this->connectionAdapter->changeWorkingDirectory($previousWorkingDirectory);
}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Accompli\Test;

use Accompli\Chrono\Process\ProcessExecutionResult;
use Accompli\Deployment\Connection\ConnectionAdapterProcessExecutor;
use PHPUnit_Framework_TestCase;

/**
* ConnectionAdapterProcessExecutorTest.
*
* @author Niels Nijens <nijens.niels@gmail.com>
*/
class ConnectionAdapterProcessExecutorTest extends PHPUnit_Framework_TestCase
{
/**
* Tests if constructing a new ConnectionAdapterProcessExecutor instance sets the instance properties.
*/
public function testConstruct()
{
$connectionAdapterMock = $this->getMockBuilder('Accompli\Deployment\Connection\ConnectionAdapterInterface')->getMock();

$processExecutor = new ConnectionAdapterProcessExecutor($connectionAdapterMock);

$this->assertAttributeSame($connectionAdapterMock, 'connectionAdapter', $processExecutor);
}

/**
* Tests if ConnectionAdapterProcessExecutor::isDirectory returns the expected result.
*
* @depends testConstruct
*/
public function testIsDirectory()
{
$connectionAdapterMock = $this->getMockBuilder('Accompli\Deployment\Connection\ConnectionAdapterInterface')->getMock();
$connectionAdapterMock->expects($this->once())->method('isDirectory')->willReturn(true);

$processExecutor = new ConnectionAdapterProcessExecutor($connectionAdapterMock);

$this->assertTrue($processExecutor->isDirectory('/test/path'));
}

/**
* Tests if ConnectionAdapterProcessExecutor::execute returns the expected result.
*
* @depends testConstruct
*/
public function testExecute()
{
$connectionAdapterMock = $this->getMockBuilder('Accompli\Deployment\Connection\ConnectionAdapterInterface')->getMock();
$connectionAdapterMock->expects($this->once())->method('executeCommand')->willReturn(new ProcessExecutionResult(0, '', ''));

$processExecutor = new ConnectionAdapterProcessExecutor($connectionAdapterMock);

$this->assertInstanceOf('Accompli\Chrono\Process\ProcessExecutionResult', $processExecutor->execute('echo test'));
}

/**
* Tests if ConnectionAdapterProcessExecutor::execute changes the working directory and returns the expected result.
*
* @depends testExecute
*/
public function testExecuteWithWorkingDirectory()
{
$connectionAdapterMock = $this->getMockBuilder('Accompli\Deployment\Connection\ConnectionAdapterInterface')->getMock();
$connectionAdapterMock->expects($this->once())
->method('getWorkingDirectory')
->willReturn('/previous/directory');
$connectionAdapterMock->expects($this->exactly(2))
->method('changeWorkingDirectory')
->withConsecutive(
array('/test/path'),
array('/previous/directory')
)
->willReturn(true);
$connectionAdapterMock->expects($this->once())
->method('executeCommand')
->willReturn(new ProcessExecutionResult(0, '', ''));

$processExecutor = new ConnectionAdapterProcessExecutor($connectionAdapterMock);

$this->assertInstanceOf('Accompli\Chrono\Process\ProcessExecutionResult', $processExecutor->execute('echo test', '/test/path'));
}
}

0 comments on commit abd4142

Please sign in to comment.