diff --git a/CHANGELOG.md b/CHANGELOG.md index baaecf0..f642a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ -CHANGELOG for 3.x +CHANGELOG for 3.X ================= +* 3.1.0 (2017-02-25) + * Add new Exec task to execute arbitrary shell commands + * Add new Composer task, to update phar (composer/self-update) + * [#344] Allow to flag Filesystem tasks + * [PR#346] Add new File System task, to change file's modes (fs/chmod) + * [BUGFIX] [PR#342] Ignore empty exclude lines + * [PR#330] Allow Composer task options to be overwritten at environment level + * [PR#330] Add new method Runtime::getMergedOption to merge ConfigOption and EnvOption + * [Documentation] [PR#333] Improve example config file + * 3.0.1 (2017-01-10) * [BUGFIX] [#350] [#353] Fix escape issue when commands are sent through SSH diff --git a/docs/example-config.yml b/docs/example-config.yml index 86bcdd3..a3036b7 100644 --- a/docs/example-config.yml +++ b/docs/example-config.yml @@ -19,9 +19,9 @@ magephp: - composer/install - composer/generate-autoload on-deploy: - - symfony/cache-warmup: { env: 'dev' } - - symfony/assets-install: { env: 'dev' } - - symfony/assetic-dump: { env: 'dev' } + - symfony/cache-warmup: { env: 'prod' } + - symfony/assets-install: { env: 'prod' } + - symfony/assetic-dump: { env: 'prod' } on-release: post-release: post-deploy: diff --git a/src/Mage.php b/src/Mage.php index 4e82512..4944c52 100644 --- a/src/Mage.php +++ b/src/Mage.php @@ -17,6 +17,6 @@ namespace Mage; */ class Mage { - const VERSION = '3.0.1'; + const VERSION = '3.x-dev'; const CODENAME = 'Nostromo'; } diff --git a/src/MageApplication.php b/src/MageApplication.php index 70b144c..cb20d30 100644 --- a/src/MageApplication.php +++ b/src/MageApplication.php @@ -77,7 +77,6 @@ class MageApplication extends Application } if (array_key_exists('magephp', $config) && is_array($config['magephp'])) { - $logger = null; if (array_key_exists('log_dir', $config['magephp']) && file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])) { $logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His')); diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index 8383c5d..b42826d 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -229,6 +229,26 @@ class Runtime return $default; } + /** + * Shortcut to get the the configuration option for a specific environment and merge it with + * the global one (environment specific overrides the global one if present). + * + * @param $key + * @param array $defaultEnv + * + * @return array + */ + public function getMergedOption($key, $defaultEnv = []) + { + $userGlobalOptions = $this->getConfigOption($key, $defaultEnv); + $userEnvOptions = $this->getEnvOption($key, $defaultEnv); + + return array_merge( + (is_array($userGlobalOptions) ? $userGlobalOptions : []), + (is_array($userEnvOptions) ? $userEnvOptions : []) + ); + } + /** * Overwrites an Environment Configuration Option * diff --git a/src/Task/AbstractTask.php b/src/Task/AbstractTask.php index 0ee7c2f..ee03134 100644 --- a/src/Task/AbstractTask.php +++ b/src/Task/AbstractTask.php @@ -61,7 +61,8 @@ abstract class AbstractTask if (!is_array($options)) { $options = []; } - $this->options = $options; + + $this->options = array_merge($this->getDefaults(), $options); return $this; } @@ -76,4 +77,13 @@ abstract class AbstractTask $this->runtime = $runtime; return $this; } + + /** + * Return Default options + * @return array + */ + public function getDefaults() + { + return []; + } } diff --git a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php index b778413..af66d1e 100644 --- a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php +++ b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php @@ -33,20 +33,19 @@ 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(); } protected function getOptions() { - $userOptions = $this->runtime->getConfigOption('composer', []); $options = array_merge( ['path' => 'composer', 'flags' => '--optimize'], - (is_array($userOptions) ? $userOptions : []), + $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/src/Task/BuiltIn/Composer/InstallTask.php b/src/Task/BuiltIn/Composer/InstallTask.php index ce97361..4229992 100644 --- a/src/Task/BuiltIn/Composer/InstallTask.php +++ b/src/Task/BuiltIn/Composer/InstallTask.php @@ -33,20 +33,19 @@ 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(); } protected function getOptions() { - $userOptions = $this->runtime->getConfigOption('composer', []); $options = array_merge( ['path' => 'composer', 'flags' => '--optimize-autoloader'], - (is_array($userOptions) ? $userOptions : []), + $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/src/Task/BuiltIn/Composer/SelfUpdateTask.php b/src/Task/BuiltIn/Composer/SelfUpdateTask.php new file mode 100644 index 0000000..f07fd51 --- /dev/null +++ b/src/Task/BuiltIn/Composer/SelfUpdateTask.php @@ -0,0 +1,93 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +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 + * + * @author Yanick Witschi + */ +class SelfUpdateTask extends AbstractTask +{ + public function getName() + { + return 'composer/self-update'; + } + + public function getDescription() + { + return '[Composer] Self Update'; + } + + public function execute() + { + $options = $this->getOptions(); + $cmdVersion = sprintf('%s --version', $options['path']); + /** @var Process $process */ + $process = $this->runtime->runCommand(trim($cmdVersion)); + if (!$process->isSuccessful()) { + return false; + } + + $buildDate = $this->getBuildDate($process->getOutput()); + if (!$buildDate instanceof DateTime) { + return false; + } + + $compareDate = $this->getCompareDate(); + if ($buildDate >= $compareDate) { + throw new SkipException(); + } + + $cmdUpdate = sprintf('%s self-update', $options['path']); + /** @var Process $process */ + $process = $this->runtime->runCommand(trim($cmdUpdate)); + + return $process->isSuccessful(); + } + + protected function getBuildDate($output) + { + $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 $buildDate; + } + + protected function getCompareDate() + { + $options = $this->getOptions(); + $compareDate = new DateTime(); + $compareDate->modify(sprintf('now -%d days', $options['days'])); + return $compareDate; + } + + protected function getOptions() + { + $options = array_merge( + ['path' => 'composer', 'days' => 60], + $this->runtime->getMergedOption('composer'), + $this->options + ); + + return $options; + } +} diff --git a/src/Task/BuiltIn/Deploy/RsyncTask.php b/src/Task/BuiltIn/Deploy/RsyncTask.php index f3343b6..1dce74e 100644 --- a/src/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Task/BuiltIn/Deploy/RsyncTask.php @@ -55,7 +55,7 @@ class RsyncTask extends AbstractTask protected function getExcludes() { $excludes = $this->runtime->getEnvOption('exclude', []); - $excludes = array_merge(['.git'], $excludes); + $excludes = array_merge(['.git'], array_filter($excludes)); foreach ($excludes as &$exclude) { $exclude = '--exclude=' . $exclude; diff --git a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php index d3b9d28..e694f8f 100644 --- a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php +++ b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php @@ -52,7 +52,7 @@ class PrepareTask extends AbstractTask protected function getExcludes() { $excludes = $this->runtime->getEnvOption('exclude', []); - $excludes = array_merge(['.git'], $excludes); + $excludes = array_merge(['.git'], array_filter($excludes)); foreach ($excludes as &$exclude) { $exclude = '--exclude="' . $exclude . '"'; diff --git a/src/Task/BuiltIn/ExecTask.php b/src/Task/BuiltIn/ExecTask.php new file mode 100644 index 0000000..0c7fc17 --- /dev/null +++ b/src/Task/BuiltIn/ExecTask.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Task\BuiltIn; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\AbstractTask; +use Symfony\Component\Process\Process; + +/** + * Exec task. Allows you to execute arbitrary commands. + * + * @author Yanick Witschi + */ +class ExecTask extends AbstractTask +{ + /** + * @return string + */ + public function getName() + { + return 'exec'; + } + + /** + * @return string + */ + public function getDescription() + { + $options = $this->getOptions(); + + if ($options['desc']) { + return '[Exec] ' . $options['desc']; + } + + return '[Exec] Custom command'; + } + + /** + * @return bool + * + * @throws ErrorException + */ + public function execute() + { + $options = $this->getOptions(); + + if (!$options['cmd']) { + throw new ErrorException('Parameter "cmd" is not defined'); + } + + /** @var Process $process */ + $process = $this->runtime->runCommand($options['cmd'], $options['timeout']); + return $process->isSuccessful(); + } + + /** + * @return array + */ + protected function getOptions() + { + $options = array_merge( + ['cmd' => '', 'desc' => '', 'timeout' => 120], + $this->options + ); + + return $options; + } +} diff --git a/src/Task/BuiltIn/FS/AbstractFileTask.php b/src/Task/BuiltIn/FS/AbstractFileTask.php index c2e4685..eb782b2 100644 --- a/src/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Task/BuiltIn/FS/AbstractFileTask.php @@ -29,8 +29,10 @@ abstract class AbstractFileTask extends AbstractTask protected function getOptions() { $mandatory = $this->getParameters(); + $defaults = array_keys($this->getDefaults()); + $missing = array_diff($mandatory, $defaults); - foreach ($mandatory as $parameter) { + foreach ($missing as $parameter) { if (!array_key_exists($parameter, $this->options)) { throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter)); } diff --git a/src/Task/BuiltIn/FS/ChangeModeTask.php b/src/Task/BuiltIn/FS/ChangeModeTask.php new file mode 100644 index 0000000..1422c3f --- /dev/null +++ b/src/Task/BuiltIn/FS/ChangeModeTask.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Task\BuiltIn\FS; + +use Symfony\Component\Process\Process; +use Exception; + +/** + * File System Task - Copy a File + * + * @author Marian Bäuerle + */ +class ChangeModeTask extends AbstractFileTask +{ + public function getName() + { + return 'fs/chmod'; + } + + public function getDescription() + { + try { + return sprintf('[FS] Change mode of "%s" to "%s"', $this->getFile('file'), $this->options['mode']); + } catch (Exception $exception) { + return '[FS] Chmod [missing parameters]'; + } + } + + public function execute() + { + $file = $this->getFile('file'); + $mode = $this->options['mode']; + $flags = $this->options['flags']; + + $cmd = sprintf('chmod %s %s "%s"', $flags, $mode, $file); + + /** @var Process $process */ + $process = $this->runtime->runCommand($cmd); + + return $process->isSuccessful(); + } + + protected function getParameters() + { + return ['file', 'mode', 'flags']; + } + + public function getDefaults() + { + return ['flags' => null]; + } +} diff --git a/src/Task/BuiltIn/FS/CopyTask.php b/src/Task/BuiltIn/FS/CopyTask.php index fc7eaf4..469b19c 100644 --- a/src/Task/BuiltIn/FS/CopyTask.php +++ b/src/Task/BuiltIn/FS/CopyTask.php @@ -38,8 +38,9 @@ class CopyTask extends AbstractFileTask { $copyFrom = $this->getFile('from'); $copyTo = $this->getFile('to'); + $flags = $this->options['flags']; - $cmd = sprintf('cp -p %s %s', $copyFrom, $copyTo); + $cmd = sprintf('cp %s "%s" "%s"', $flags, $copyFrom, $copyTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -49,6 +50,11 @@ class CopyTask extends AbstractFileTask protected function getParameters() { - return ['from', 'to']; + return ['from', 'to', 'flags']; + } + + public function getDefaults() + { + return ['flags' => '-p']; } } diff --git a/src/Task/BuiltIn/FS/LinkTask.php b/src/Task/BuiltIn/FS/LinkTask.php index 9cf6994..12721e0 100644 --- a/src/Task/BuiltIn/FS/LinkTask.php +++ b/src/Task/BuiltIn/FS/LinkTask.php @@ -38,8 +38,9 @@ class LinkTask extends AbstractFileTask { $linkFrom = $this->getFile('from'); $linkTo = $this->getFile('to'); + $flags = $this->options['flags']; - $cmd = sprintf('ln -snf %s %s', $linkFrom, $linkTo); + $cmd = sprintf('ln %s "%s" "%s"', $flags, $linkFrom, $linkTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -49,6 +50,11 @@ class LinkTask extends AbstractFileTask protected function getParameters() { - return ['from', 'to']; + return ['from', 'to', 'flags']; + } + + public function getDefaults() + { + return ['flags' => '-snf']; } } diff --git a/src/Task/BuiltIn/FS/MoveTask.php b/src/Task/BuiltIn/FS/MoveTask.php index 4da7146..a13ecc1 100644 --- a/src/Task/BuiltIn/FS/MoveTask.php +++ b/src/Task/BuiltIn/FS/MoveTask.php @@ -38,8 +38,9 @@ class MoveTask extends AbstractFileTask { $moveFrom = $this->getFile('from'); $moveTo = $this->getFile('to'); + $flags = $this->options['flags']; - $cmd = sprintf('mv %s %s', $moveFrom, $moveTo); + $cmd = sprintf('mv %s "%s" "%s"', $flags, $moveFrom, $moveTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -49,6 +50,11 @@ class MoveTask extends AbstractFileTask protected function getParameters() { - return ['from', 'to']; + return ['from', 'to', 'flags']; + } + + public function getDefaults() + { + return ['flags' => null]; } } diff --git a/src/Task/BuiltIn/FS/RemoveTask.php b/src/Task/BuiltIn/FS/RemoveTask.php index c55c4a2..1f90724 100644 --- a/src/Task/BuiltIn/FS/RemoveTask.php +++ b/src/Task/BuiltIn/FS/RemoveTask.php @@ -37,8 +37,9 @@ class RemoveTask extends AbstractFileTask public function execute() { $file = $this->getFile('file'); + $flags = $this->options['flags']; - $cmd = sprintf('rm %s', $file); + $cmd = sprintf('rm %s "%s"', $flags, $file); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -48,6 +49,11 @@ class RemoveTask extends AbstractFileTask protected function getParameters() { - return ['file']; + return ['file', 'flags']; + } + + public function getDefaults() + { + return ['flags' => null]; } } diff --git a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php index 4c4404c..3f23d19 100644 --- a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php +++ b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php @@ -43,12 +43,9 @@ class AsseticDumpTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php index a6a63c4..001d100 100644 --- a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php +++ b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php @@ -43,12 +43,9 @@ class AssetsInstallTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'target' => 'web', 'flags' => '--symlink --relative'], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/CacheClearTask.php b/src/Task/BuiltIn/Symfony/CacheClearTask.php index bd5b77a..a0b6475 100644 --- a/src/Task/BuiltIn/Symfony/CacheClearTask.php +++ b/src/Task/BuiltIn/Symfony/CacheClearTask.php @@ -43,12 +43,9 @@ class CacheClearTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php index a2e30f0..8584beb 100644 --- a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php +++ b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php @@ -43,12 +43,9 @@ class CacheWarmupTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php b/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php index a5b0646..ae13f8b 100644 --- a/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php +++ b/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php @@ -80,6 +80,36 @@ class DeployCommandMiscTasksTest extends TestCase $this->assertEquals(0, $tester->getStatusCode()); } + public function testComposerEnvFlags() + { + $application = new MageApplicationMockup(__DIR__ . '/../../Resources/composer-env.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $ranCommands = $application->getRuntime()->getRanCommands(); + + $testCase = array( + 0 => '/usr/foobar/composer install --prefer-source', + 1 => '/usr/foobar/composer dump-autoload --no-scripts', + 2 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=./var/cache/* --exclude=./var/log/* --exclude=./web/app_dev.php ./ tester@testhost:/var/www/test', + ); + + // 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 testInvalidTaskName() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/invalid-task.yml'); diff --git a/tests/Resources/composer-env.yml b/tests/Resources/composer-env.yml new file mode 100644 index 0000000..2421fdf --- /dev/null +++ b/tests/Resources/composer-env.yml @@ -0,0 +1,19 @@ +magephp: + log_dir: /tmp + composer: + path: /usr/bin/composer.phar + environments: + test: + composer: + path: /usr/foobar/composer + user: tester + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - composer/install: { flags: '--prefer-source' } + - composer/dump-autoload: { flags: '--no-scripts' } diff --git a/tests/Resources/testhost.yml b/tests/Resources/testhost.yml index c20a614..f55f9e9 100644 --- a/tests/Resources/testhost.yml +++ b/tests/Resources/testhost.yml @@ -10,6 +10,8 @@ magephp: - ./var/cache/* - ./var/log/* - ./web/app_dev.php + - + - hosts: - testhost pre-deploy: diff --git a/tests/Runtime/ProcessMockup.php b/tests/Runtime/ProcessMockup.php index 7e88048..918a127 100644 --- a/tests/Runtime/ProcessMockup.php +++ b/tests/Runtime/ProcessMockup.php @@ -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']); } diff --git a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php new file mode 100644 index 0000000..1721e8b --- /dev/null +++ b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php @@ -0,0 +1,174 @@ +setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => 'composer']); + $task->setRuntime($runtime); + $this->assertEquals('[Composer] Self Update', $task->getDescription()); + + 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]); + } + } +} diff --git a/tests/Task/BuiltIn/ExecTaskTest.php b/tests/Task/BuiltIn/ExecTaskTest.php new file mode 100644 index 0000000..d548176 --- /dev/null +++ b/tests/Task/BuiltIn/ExecTaskTest.php @@ -0,0 +1,97 @@ + + * + * 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\ExecTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class ExecTest extends TestCase +{ + public function testSimpleCommand() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ExecTask(); + $task->setOptions(['cmd' => 'ls -l', 'desc' => 'Loading docker']); + $task->setRuntime($runtime); + + $this->assertContains('[Exec] Loading docker', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ls -l', + ); + + // 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 testCommandWithoutDescription() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ExecTask(); + $task->setOptions(['cmd' => 'ls -la']); + $task->setRuntime($runtime); + + $this->assertContains('[Exec] Custom command', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ls -la', + ); + + // 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 testWithoutCommand() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ExecTask(); + $task->setOptions(['desc' => 'Loading docker']); + $task->setRuntime($runtime); + + $this->assertContains('[Exec] Loading docker', $task->getDescription()); + + try { + $task->execute(); + $this->assertTrue(false, 'Task did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "cmd" is not defined', $exception->getMessage()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/ChangeModeTaskTest.php b/tests/Task/BuiltIn/FileSystem/ChangeModeTaskTest.php new file mode 100644 index 0000000..79db322 --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/ChangeModeTaskTest.php @@ -0,0 +1,127 @@ + + * + * 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\ChangeModeTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class ChangeModeTest extends TestCase +{ + public function testChangeModeTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['file' => 'a.txt', 'flags' => '-R', 'mode' => 'o+w']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('o+w', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'chmod -R o+w "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 testChangeModeTaskWithoutFlags() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['file' => 'a.txt', 'mode' => 'o+w']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('o+w', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'chmod o+w "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 testChangeModeReplaceTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['file' => '%environment%.txt', 'flags' => '-R', 'mode' => 'o+w']); + $task->setRuntime($runtime); + + $this->assertContains('test.txt', $task->getDescription()); + $this->assertContains('o+w', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'chmod -R o+w "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 testChangeModeBadOptionsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['from' => 'a.txt', 'flags' => '-R', 'mode' => 'o+w']); + $task->setRuntime($runtime); + + try { + $this->assertContains('[missing parameters]', $task->getDescription()); + $task->execute(); + $this->assertTrue(false, 'Task should have raised an exception'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "file" is not defined', $exception->getMessage()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/CopyTaskTest.php b/tests/Task/BuiltIn/FileSystem/CopyTaskTest.php new file mode 100644 index 0000000..1d91d1a --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/CopyTaskTest.php @@ -0,0 +1,158 @@ + + * + * 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()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/LinkTaskTest.php b/tests/Task/BuiltIn/FileSystem/LinkTaskTest.php new file mode 100644 index 0000000..5869709 --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/LinkTaskTest.php @@ -0,0 +1,127 @@ + + * + * 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()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/MoveTaskTest.php b/tests/Task/BuiltIn/FileSystem/MoveTaskTest.php new file mode 100644 index 0000000..4fdab3d --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/MoveTaskTest.php @@ -0,0 +1,127 @@ + + * + * 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()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php b/tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php new file mode 100644 index 0000000..4b050a4 --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php @@ -0,0 +1,124 @@ + + * + * 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()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystemTaskTest.php b/tests/Task/BuiltIn/FileSystemTaskTest.php deleted file mode 100644 index c28c3d5..0000000 --- a/tests/Task/BuiltIn/FileSystemTaskTest.php +++ /dev/null @@ -1,364 +0,0 @@ - - * - * 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 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 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 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 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()); - } - } -}