[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
@@ -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;
}
}
+23
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']);
}
@@ -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]);
}
}
}
@@ -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;
@@ -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());
}
}
}
@@ -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());
}
}
}
@@ -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());
}
}
}
@@ -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());
}
}
}
-479
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());
}
}
}