Allow to pre-register Custom Tasks

This commit is contained in:
Andrés Montañez
2017-04-14 20:36:45 -03:00
parent c14e3bc710
commit aa9793999a
11 changed files with 314 additions and 4 deletions
@@ -0,0 +1,13 @@
magephp:
environments:
production:
user: app
host_path: /var/www/myapp
hosts:
- webserver
pre-deploy:
on-deploy:
- custom-invalid-class
post-deploy:
custom_tasks:
- Mage\Tests\Task\Custom\InvalidClass
@@ -0,0 +1,13 @@
magephp:
environments:
production:
user: app
host_path: /var/www/myapp
hosts:
- webserver
pre-deploy:
on-deploy:
- custom-invalid-inheritance
post-deploy:
custom_tasks:
- Mage\Tests\Task\Custom\InvalidInheritanceTask
@@ -0,0 +1,13 @@
magephp:
environments:
production:
user: app
host_path: /var/www/myapp
hosts:
- webserver
pre-deploy:
on-deploy:
- custom-not-instantiable
post-deploy:
custom_tasks:
- Mage\Tests\Task\Custom\NotInstantiableTask
+13
View File
@@ -0,0 +1,13 @@
magephp:
environments:
production:
user: app
host_path: /var/www/myapp
hosts:
- webserver
pre-deploy:
on-deploy:
- custom-valid
post-deploy:
custom_tasks:
- Mage\Tests\Task\Custom\ValidTask
@@ -0,0 +1,47 @@
<?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\Custom;
use Symfony\Component\Process\Process;
/**
* Custom PreRegistered Task for Testing
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class InvalidInheritanceTask
{
/**
* @return string
*/
public function getName()
{
return 'custom-invalid-inheritance';
}
/**
* @return string
*/
public function getDescription()
{
return '[Custom] Invalid Inheritance*';
}
/**
* @return bool
*/
public function execute()
{
/** @var Process $process */
$process = $this->runtime->runCommand('echo "custom-invalid-inheritance"');
return $process->isSuccessful();
}
}
+48
View File
@@ -0,0 +1,48 @@
<?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\Custom;
use Mage\Task\AbstractTask;
use Symfony\Component\Process\Process;
/**
* Custom PreRegistered Task for Testing
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
abstract class NotInstantiableTask extends AbstractTask
{
/**
* @return string
*/
public function getName()
{
return 'custom-not-instantiable';
}
/**
* @return string
*/
public function getDescription()
{
return '[Custom] Not Instantiable*';
}
/**
* @return bool
*/
public function execute()
{
/** @var Process $process */
$process = $this->runtime->runCommand('echo "custom-not-instantiable"');
return $process->isSuccessful();
}
}
+48
View File
@@ -0,0 +1,48 @@
<?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\Custom;
use Mage\Task\AbstractTask;
use Symfony\Component\Process\Process;
/**
* Custom PreRegistered Task for Testing
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ValidTask extends AbstractTask
{
/**
* @return string
*/
public function getName()
{
return 'custom-valid';
}
/**
* @return string
*/
public function getDescription()
{
return '[Custom] Valid*';
}
/**
* @return bool
*/
public function execute()
{
/** @var Process $process */
$process = $this->runtime->runCommand('echo "custom-valid"');
return $process->isSuccessful();
}
}
+83
View File
@@ -10,11 +10,15 @@
namespace Mage\Tests\Task;
use Mage\Command\AbstractCommand;
use Mage\Command\BuiltIn\DeployCommand;
use Mage\Task\TaskFactory;
use Mage\Runtime\Runtime;
use Mage\Runtime\Exception\RuntimeException;
use Exception;
use Mage\Tests\MageApplicationMockup;
use PHPUnit_Framework_TestCase as TestCase;
use Symfony\Component\Console\Tester\CommandTester;
class TaskFactoryTest extends TestCase
{
@@ -43,4 +47,83 @@ class TaskFactoryTest extends TestCase
$this->assertEquals('Invalid task name "stdClass"', $exception->getMessage());
}
}
public function testPreRegisteredCustomTask()
{
$application = new MageApplicationMockup(__DIR__ . '/../Resources/custom-task.yml');
/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'production']);
$this->assertContains('[Custom] Valid*', $tester->getDisplay());
$ranCommands = $application->getRuntime()->getRanCommands();
$testCase = array(
0 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git ./ app@webserver:/var/www/myapp',
1 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no app@webserver "cd /var/www/myapp && echo \"custom-valid\""',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
$this->assertEquals(0, $tester->getStatusCode());
}
public function testPreRegisteredCustomTaskInvalidClass()
{
$application = new MageApplicationMockup(__DIR__ . '/../Resources/custom-task-invalid-class.yml');
/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'production']);
$this->assertContains('Custom Task "Mage\Tests\Task\Custom\InvalidClass" does not exists.', $tester->getDisplay());
$this->assertNotEquals(0, $tester->getStatusCode());
}
public function testPreRegisteredCustomTaskNonInstantiable()
{
$application = new MageApplicationMockup(__DIR__ . '/../Resources/custom-task-not-instantiable.yml');
/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'production']);
$this->assertContains('Custom Task "Mage\Tests\Task\Custom\NotInstantiableTask" can not be instantiated.', $tester->getDisplay());
$this->assertNotEquals(0, $tester->getStatusCode());
}
public function testPreRegisteredCustomTaskInvalidInheritance()
{
$application = new MageApplicationMockup(__DIR__ . '/../Resources/custom-task-invalid-inheritance.yml');
/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'production']);
$this->assertContains('Custom Task "Mage\Tests\Task\Custom\InvalidInheritanceTask" must inherit "Mage\Task\AbstractTask".', $tester->getDisplay());
$this->assertNotEquals(0, $tester->getStatusCode());
}
}