[Nostromo] Refactor Composer Update, organize task tests better

This commit is contained in:
Andrés Montañez 2017-02-11 01:48:26 -03:00
parent 5054fb45d0
commit d8f2359d12
12 changed files with 769 additions and 726 deletions

View file

@ -33,10 +33,10 @@ class DumpAutoloadTask extends AbstractTask
public function execute()
{
$options = $this->getOptions();
$command = sprintf('%s dump-autoload %s', $options['path'], $options['flags']);
$cmd = sprintf('%s dump-autoload %s', $options['path'], $options['flags']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($command));
$process = $this->runtime->runCommand(trim($cmd));
return $process->isSuccessful();
}

View file

@ -33,10 +33,10 @@ class InstallTask extends AbstractTask
public function execute()
{
$options = $this->getOptions();
$command = sprintf('%s install %s', $options['path'], $options['flags']);
$cmd = sprintf('%s install %s', $options['path'], $options['flags']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($command));
$process = $this->runtime->runCommand(trim($cmd));
return $process->isSuccessful();
}

View file

@ -10,8 +10,10 @@
namespace Mage\Task\BuiltIn\Composer;
use Mage\Task\Exception\SkipException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
use DateTime;
/**
* Composer Task - Self update
@ -20,123 +22,68 @@ use Mage\Task\AbstractTask;
*/
class SelfUpdateTask extends AbstractTask
{
/**
* Only used for unit tests.
*
* @var \DateTime
*/
private $dateToCompare;
/**
* @return string
*/
public function getName()
{
return 'composer/selfupdate';
return 'composer/self-update';
}
/**
* @return string
*/
public function getDescription()
{
return '[Composer] Selfupdate';
return '[Composer] Self Update';
}
/**
* @return bool
*/
public function execute()
{
$options = $this->getOptions();
$days = $options['days'];
$versionCommand = sprintf('%s --version', $options['path']);
$cmdVersion = sprintf('%s --version', $options['path']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($versionCommand));
$process = $this->runtime->runCommand(trim($cmdVersion));
if (!$process->isSuccessful()) {
return false;
}
$dt = $this->extractDate($process->getOutput());
// Date could not be extracted, always run update
if (false === $dt) {
return $this->selfUpdate($options);
$buildDate = $this->getBuildDate($process->getOutput());
if (!$buildDate instanceof DateTime) {
return false;
}
// Check age
if (!$this->isOlderThan($dt, $days)) {
return true;
$compareDate = $this->getCompareDate();
if ($buildDate >= $compareDate) {
throw new SkipException();
}
return $this->selfUpdate($options);
}
/**
* This tasks obviously always takes the current date to compare the age
* of the composer.phar. This method is used for unit test purposes
* only.
*
* @param \DateTime $dateToCompare
*/
public function setDateToCompare(\DateTime $dateToCompare)
{
$this->dateToCompare = $dateToCompare;
}
/**
* @param \DateTime $dt
* @param int $days
*
* @return bool
*/
protected function isOlderThan(\DateTime $dt, $days)
{
$dtComp = new \DateTime($days . ' days ago');
if (null !== $this->dateToCompare) {
$dtComp = $this->dateToCompare;
}
return $dt < $dtComp;
}
/**
* @param array $options
*
* @return bool
*/
protected function selfUpdate(array $options)
{
$selfupdateCommand = sprintf('%s selfupdate %s', $options['path'], $options['release']);
$cmdUpdate = sprintf('%s self-update %s', $options['path'], $options['release']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($selfupdateCommand));
$process = $this->runtime->runCommand(trim($cmdUpdate));
return $process->isSuccessful();
}
/**
* @param string $output
*
* @return \DateTime|false
*/
protected function extractDate($output)
protected function getBuildDate($output)
{
$date = substr($output, -19);
$buildDate = null;
$output = explode(PHP_EOL, $output);
foreach ($output as $row) {
if (strpos($row, 'Composer version ') === 0) {
$buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19));
}
}
return \DateTime::createFromFormat('Y-m-d H:i:s', $date);
return $buildDate;
}
protected function getCompareDate()
{
$options = $this->getOptions();
$compareDate = new DateTime();
$compareDate->modify(sprintf('now -%d days', $options['days']));
return $compareDate;
}
/**
* @return array
*/
protected function getOptions()
{
$options = array_merge(
['path' => 'composer', 'release' => '', 'days' => 30],
['path' => 'composer', 'release' => '', 'days' => 60],
$this->runtime->getMergedOption('composer'),
$this->options
);

View file

@ -1,157 +0,0 @@
<?php
namespace Mage\Tests\Command\BuiltIn\Composer;
use Mage\Runtime\Runtime;
use Mage\Task\BuiltIn\Composer\SelfUpdateTask;
use PHPUnit_Framework_TestCase as TestCase;
use Symfony\Component\Process\Process;
class SelfUpdateTaskTest extends TestCase
{
public function testBasics()
{
$task = new SelfUpdateTask();
$this->assertSame('composer/selfupdate', $task->getName());
$this->assertSame('[Composer] Selfupdate', $task->getDescription());
}
public function testExecuteWithFailingVersionDoesNotCallSelfupdate()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runCommand'])
->getMock();
$runtime
->expects($this->once())
->method('runCommand')
->with('composer --version')
->willReturn($this->mockProcess(false));
$task = $this->getTask($runtime);
$this->assertFalse($task->execute());
}
public function testExecuteWithNoDateVersionDoesCallSelfupdate()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runCommand'])
->getMock();
$runtime
->expects($this->exactly(2))
->method('runCommand')
->withConsecutive(
['composer --version'],
['composer selfupdate']
)
->willReturnOnConsecutiveCalls(
$this->mockProcess(true, 'whatever-without-valid-date'),
$this->mockProcess(true)
);
$task = $this->getTask($runtime);
$this->assertTrue($task->execute());
}
public function testExecuteShouldUpdate()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runCommand'])
->getMock();
$runtime
->expects($this->exactly(2))
->method('runCommand')
->withConsecutive(
['composer --version'],
['composer selfupdate']
)
->willReturnOnConsecutiveCalls(
$this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'),
$this->mockProcess(true)
);
$task = $this->getTask($runtime);
$task->setOptions(['days' => 30]);
$this->assertTrue($task->execute());
}
public function testExecuteShouldNotUpdate()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runCommand'])
->getMock();
$runtime
->expects($this->exactly(1))
->method('runCommand')
->with('composer --version')
->willReturn($this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'));
$task = $this->getTask($runtime);
$task->setDateToCompare(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-12-10 18:23:41'));
$task->setOptions(['days' => 30]);
$this->assertTrue($task->execute());
}
public function testWithRelease()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runCommand'])
->getMock();
$runtime
->expects($this->exactly(2))
->method('runCommand')
->withConsecutive(
['composer --version'],
['composer selfupdate 1.3.1']
)
->willReturnOnConsecutiveCalls(
$this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'),
$this->mockProcess(true)
);
$task = $this->getTask($runtime);
$task->setOptions(['days' => 30, 'release' => '1.3.1']);
$this->assertTrue($task->execute());
}
private function getTask($runtime)
{
$config = [
'magephp' => [
'composer' => [
'path' => 'composer.phar'
]
]
];
/** @var Runtime $runtime */
$runtime->setConfiguration($config);
$task = new SelfUpdateTask();
$task->setRuntime($runtime);
return $task;
}
private function mockProcess($successful, $output = '')
{
$process = $this->getMockBuilder(Process::class)
->disableOriginalConstructor()
->getMock();
$process
->expects($this->any())
->method('isSuccessful')
->willReturn($successful);
$process
->expects($this->any())
->method('getOutput')
->willReturn($output);
return $process;
}
}

View file

@ -68,6 +68,29 @@ class ProcessMockup extends Process
return '* master';
}
// Make composer build 20 days old
if ($this->commandline == 'composer --version') {
$date = date('Y-m-d H:i:s', strtotime('now -20 days'));
return 'Composer version 1.3.0 ' . $date;
}
// Make ./composer build 20 days old
if ($this->commandline == './composer --version') {
$date = date('Y-m-d H:i:s', strtotime('now -20 days'));
return 'Do not run Composer as root/super user! See https://getcomposer.org/root for details' . PHP_EOL . 'Composer version 1.3.0 ' . $date;
}
// Make composer.phar build 90 days old
if ($this->commandline == 'composer.phar --version') {
$date = date('Y-m-d H:i:s', strtotime('now -90 days'));
return 'Composer version 1.3.0 ' . $date;
}
// Make php composer has wrong output
if ($this->commandline == 'php composer --version') {
return 'Composer version 1.3.0 no build';
}
if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"') {
return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']);
}

View file

@ -0,0 +1,173 @@
<?php
namespace Mage\Tests\Task\BuiltIn\Composer;
use Mage\Tests\Runtime\RuntimeMockup;
use Mage\Task\BuiltIn\Composer\SelfUpdateTask;
use Mage\Task\Exception\SkipException;
use Exception;
use PHPUnit_Framework_TestCase as TestCase;
class SelfUpdateTaskTest extends TestCase
{
public function testSelfUpdateTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new SelfUpdateTask();
$task->setOptions(['path' => 'composer']);
$task->setRuntime($runtime);
try {
$task->execute();
} catch (Exception $exception) {
$this->assertTrue($exception instanceof SkipException, 'Update should been skipped');
}
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'composer --version',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testSelfUpdateAsRootTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new SelfUpdateTask();
$task->setOptions(['path' => './composer']);
$task->setRuntime($runtime);
try {
$task->execute();
} catch (Exception $exception) {
$this->assertTrue($exception instanceof SkipException, 'Update should been skipped');
}
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => './composer --version',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testSelfUpdateMustUpdateTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new SelfUpdateTask();
$task->setOptions(['path' => 'composer.phar']);
$task->setRuntime($runtime);
try {
$result = $task->execute();
$this->assertTrue($result, 'Result should be successful');
} catch (Exception $exception) {
if ($exception instanceof SkipException) {
$this->assertTrue(false, 'Update should not have been skipped');
}
}
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'composer.phar --version',
1 => 'composer.phar self-update',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testSelfUpdateWrongOutputTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new SelfUpdateTask();
$task->setOptions(['path' => 'php composer']);
$task->setRuntime($runtime);
try {
$result = $task->execute();
$this->assertFalse($result, 'Result should be failure');
} catch (Exception $exception) {
if ($exception instanceof SkipException) {
$this->assertTrue(false, 'Update should not have been skipped');
}
}
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'php composer --version',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testSelfUpdateFailExecTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new SelfUpdateTask();
$task->setOptions(['path' => 'composer']);
$task->setRuntime($runtime);
$runtime->forceFail('composer --version');
try {
$result = $task->execute();
$this->assertFalse($result, 'Result should be failure');
} catch (Exception $exception) {
if ($exception instanceof SkipException) {
$this->assertTrue(false, 'Update should not have been skipped');
}
}
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'composer --version',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
}

View file

@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn;
namespace Mage\Tests\Task\BuiltIn\FileSystem;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\FS\ChangeModeTask;

View file

@ -0,0 +1,158 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn\FileSystem;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\FS\CopyTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
class CopyTaskTest extends TestCase
{
public function testCopyTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -p "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyTaskWithFlags()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-rp']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -rp "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -p "test.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyMultipleReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$runtime->setReleaseId('1234');
$runtime->setWorkingHost('localhost');
$task = new CopyTask();
$task->setOptions(['from' => '%host%.txt', 'to' => '%release%.yml']);
$task->setRuntime($runtime);
$this->assertContains('localhost.txt', $task->getDescription());
$this->assertContains('1234.yml', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -p "localhost.txt" "1234.yml"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "from" is not defined', $exception->getMessage());
}
}
}

View file

@ -0,0 +1,127 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn\FileSystem;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\FS\LinkTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
class LinkTaskTest extends TestCase
{
public function testLinkTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ln -snf "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testLinkTaskWithFlags()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-P']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ln -P "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testLinkReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ln -snf "test.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testLinkBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "from" is not defined', $exception->getMessage());
}
}
}

View file

@ -0,0 +1,127 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn\FileSystem;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\FS\MoveTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
class MoveTaskTest extends TestCase
{
public function testMoveTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'mv "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testMoveWithFlagsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-n']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'mv -n "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testMoveReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'mv "test.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testMoveBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "from" is not defined', $exception->getMessage());
}
}
}

View file

@ -0,0 +1,124 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn\FileSystem;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\FS\RemoveTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
class RemoveTaskTest extends TestCase
{
public function testRemoveTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['file' => 'a.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'rm "a.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testRemoveWithFlagsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['file' => 'a.txt', 'flags' => '-fr']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'rm -fr "a.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testRemoveReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['file' => '%environment%.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'rm "test.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testRemoveBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['from' => 'a.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "file" is not defined', $exception->getMessage());
}
}
}

View file

@ -1,479 +0,0 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\FS\CopyTask;
use Mage\Task\BuiltIn\FS\LinkTask;
use Mage\Task\BuiltIn\FS\MoveTask;
use Mage\Task\BuiltIn\FS\RemoveTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
class FileSystemTaskTest extends TestCase
{
public function testCopyTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -p "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyTaskWithFlags()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-rp']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -rp "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -p "test.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyMultipleReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$runtime->setReleaseId('1234');
$runtime->setWorkingHost('localhost');
$task = new CopyTask();
$task->setOptions(['from' => '%host%.txt', 'to' => '%release%.yml']);
$task->setRuntime($runtime);
$this->assertContains('localhost.txt', $task->getDescription());
$this->assertContains('1234.yml', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'cp -p "localhost.txt" "1234.yml"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCopyBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new CopyTask();
$task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "from" is not defined', $exception->getMessage());
}
}
public function testMoveTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'mv "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testMoveWithFlagsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-n']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'mv -n "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testMoveReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'mv "test.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testMoveBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new MoveTask();
$task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "from" is not defined', $exception->getMessage());
}
}
public function testRemoveTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['file' => 'a.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'rm "a.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testRemoveWithFlagsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['file' => 'a.txt', 'flags' => '-fr']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'rm -fr "a.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testRemoveReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['file' => '%environment%.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'rm "test.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testRemoveBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new RemoveTask();
$task->setOptions(['from' => 'a.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "file" is not defined', $exception->getMessage());
}
}
public function testLinkTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ln -snf "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testLinkTaskWithFlags()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-P']);
$task->setRuntime($runtime);
$this->assertContains('a.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ln -P "a.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testLinkReplaceTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
$this->assertContains('test.txt', $task->getDescription());
$this->assertContains('b.txt', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ln -snf "test.txt" "b.txt"',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testLinkBadOptionsTask()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new LinkTask();
$task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']);
$task->setRuntime($runtime);
try {
$this->assertContains('[missing parameters]', $task->getDescription());
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "from" is not defined', $exception->getMessage());
}
}
}