This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ConnectionAdapterProcessExecutor
- Loading branch information
1 parent
d1e61f8
commit abd4142
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/Deployment/Connection/ConnectionAdapterProcessExecutor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
tests/Deployment/Connection/ConnectionAdapterProcessExecutorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |