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

Added ConnectionAdapterProcessExecutor as intermediate between connection adapters and Chrono #66

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Deployment/Connection/ConnectionAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function isFile($remoteFilename);
*/
public function isDirectory($remoteDirectory);

/**
* Changes the current working directory.
*
* @param string $remoteDirectory
*
* @return bool
*/
public function changeWorkingDirectory($remoteDirectory);

/**
* Executes a command.
*
Expand All @@ -59,6 +68,13 @@ public function isDirectory($remoteDirectory);
*/
public function executeCommand($command);

/**
* Returns the current working directory.
*
* @return string|bool
*/
public function getWorkingDirectory();

/**
* Returns an array with files and directories within a remote directory.
*
Expand Down
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;
}
}
16 changes: 16 additions & 0 deletions src/Deployment/Connection/LocalConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public function isDirectory($remoteDirectory)
return is_dir($remoteDirectory);
}

/**
* {@inheritdoc}
*/
public function changeWorkingDirectory($remoteDirectory)
{
return @chdir($remoteDirectory);
}

/**
* {@inheritdoc}
*/
Expand All @@ -63,6 +71,14 @@ public function executeCommand($command)
return new ProcessExecutionResult($process->getExitCode(), $process->getOutput(), strval($process->getErrorOutput()));
}

/**
* {@inheritdoc}
*/
public function getWorkingDirectory()
{
return getcwd();
}

/**
* {@inheritdoc}
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Deployment/Connection/SSHConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ public function isDirectory($remoteDirectory)
return false;
}

/**
* {@inheritdoc}
*/
public function changeWorkingDirectory($remoteDirectory)
{
if ($this->isConnected()) {
return $this->connection->chdir($remoteDirectory);
}

return false;
}

/**
* {@inheritdoc}
*/
Expand All @@ -176,6 +188,18 @@ public function executeCommand($command)
return new ProcessExecutionResult(126, '', "Connection adapter not connected.\n");
}

/**
* {@inheritdoc}
*/
public function getWorkingDirectory()
{
if ($this->isConnected()) {
return $this->connection->pwd();
}

return false;
}

/**
* {@inheritdoc}
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Deployment/Connection/ConnectedConnectionAdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public function testIsDirectoryReturnsFalseWhenNotConnected()
$this->assertFalse($this->connectionAdapter->isDirectory($this->workspaceUtility->getWorkspacePath().'/existing-directory'));
}

/**
* Tests if ConnectionAdapterInterface::changeWorkingDirectory returns false without connection.
*/
public function testChangeWorkingDirectoryReturnsFalseWhenNotConnected()
{
$this->assertFalse($this->connectionAdapter->changeWorkingDirectory($this->workspaceUtility->getWorkspacePath()));
}

/**
* Tests if ConnectionAdapterInterface::executeCommand returns ProcessExecutionResult with failure exit code and error output without connection.
*/
Expand All @@ -57,6 +65,14 @@ public function testExecuteCommandReturnsFalseWhenNotConnected()
$this->assertSame("Connection adapter not connected.\n", $result->getErrorOutput());
}

/**
* Tests if ConnectionAdapterInterface::getWorkingDirectory returns false without connection.
*/
public function testGetWorkingDirectoryReturnsFalseWhenNotConnected()
{
$this->assertFalse($this->connectionAdapter->getWorkingDirectory());
}

/**
* Tests if ConnectionAdapterInterface::getDirectoryContentsList returns false without connection.
*/
Expand Down
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'));
}
}
37 changes: 37 additions & 0 deletions tests/Deployment/Connection/ConnectionAdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ public function testIsDirectoryReturnsTrueWhenDirectoryExists()
$this->assertTrue($this->connectionAdapter->isDirectory($this->workspaceUtility->getWorkspacePath().'/existing-directory'));
}

/**
* Tests if ConnectionAdapterInterface::changeWorkingDirectory returns true on successful change to a different working directory.
*
* @depends testConnectReturnsTrue
*/
public function testChangeWorkingDirectoryReturnsTrue()
{
$this->connectionAdapter->connect();

$this->assertTrue($this->connectionAdapter->changeWorkingDirectory($this->workspaceUtility->getWorkspacePath()));
}

/**
* Tests if ConnectionAdapterInterface::changeWorkingDirectory returns false when trying to change to a non-existing working directory.
*
* @depends testChangeWorkingDirectoryReturnsTrue
*/
public function testChangeWorkingDirectoryWithNonExistingDirectoryReturnsFalse()
{
$this->connectionAdapter->connect();

$this->assertFalse($this->connectionAdapter->changeWorkingDirectory($this->workspaceUtility->getWorkspacePath().'/non-existing-directory'));
}

/**
* Tests if ConnectionAdapterInterface::executeCommand returns the expected output.
*
Expand All @@ -160,6 +184,19 @@ public function testExecuteCommand()
$this->assertSame('', $result->getErrorOutput());
}

/**
* Tests if ConnectionAdapterInterface::getWorkingDirectory returns the expected working directory.
*
* @depends testChangeWorkingDirectoryWithNonExistingDirectoryReturnsFalse
*/
public function testGetWorkingDirectory()
{
$this->connectionAdapter->connect();
$this->connectionAdapter->changeWorkingDirectory($this->workspaceUtility->getWorkspacePath());

$this->assertSame(realpath($this->workspaceUtility->getWorkspacePath()), $this->connectionAdapter->getWorkingDirectory());
}

/**
* Tests if ConnectionAdapterInterface::getDirectoryContentsList returns the expected array.
*
Expand Down