From b56e1c378c64b5b2fbfc7cd86b289f2d12cc53de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Wed, 4 Jan 2017 21:46:40 -0300 Subject: [PATCH] [Nostromo] Refactor Tests --- src/Mage/MageApplication.php | 22 +- .../Task/BuiltIn/Composer/InstallTask.php | 4 +- .../BuiltIn/Config/DumpCommandTest.php | 9 +- .../Config/EnvironmentsCommandTest.php | 75 +- .../Command/BuiltIn/DeployCommandMiscTest.php | 249 ++++++ .../Command/BuiltIn/DeployCommandTest.php | 785 ------------------ .../BuiltIn/DeployCommandWithReleasesTest.php | 71 ++ .../DeployCommandWithoutReleasesTest.php | 59 ++ .../BuiltIn/Releases/ListCommandTest.php | 77 +- .../BuiltIn/Releases/RollbackCommandTest.php | 77 +- .../Command/BuiltIn/VersionCommandTest.php | 11 +- src/Mage/Tests/MageApplicationMockup.php | 27 + src/Mage/Tests/MageTestApplication.php | 17 - .../Tests/Resources/testhost-custom-task.yml | 20 + .../Tests/Resources/testhost-skipping.yml | 24 + src/Mage/Tests/Resources/testhost-sudo.yml | 25 + .../Tests/Resources/testhost-with-error.yml | 23 + .../testhost-with-postdeploy-error.yml | 23 + .../Resources/testhost-without-releases.yml | 24 + src/Mage/Tests/Resources/testhost.yml | 25 + src/Mage/Tests/Runtime/RuntimeMockup.php | 12 + src/Mage/Tests/Runtime/RuntimeTest.php | 11 + src/Mage/Tests/UtilsTest.php | 1 + 23 files changed, 640 insertions(+), 1031 deletions(-) create mode 100644 src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php delete mode 100644 src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php create mode 100644 src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php create mode 100644 src/Mage/Tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php create mode 100644 src/Mage/Tests/MageApplicationMockup.php delete mode 100644 src/Mage/Tests/MageTestApplication.php create mode 100644 src/Mage/Tests/Resources/testhost-custom-task.yml create mode 100644 src/Mage/Tests/Resources/testhost-skipping.yml create mode 100644 src/Mage/Tests/Resources/testhost-sudo.yml create mode 100644 src/Mage/Tests/Resources/testhost-with-error.yml create mode 100644 src/Mage/Tests/Resources/testhost-with-postdeploy-error.yml create mode 100644 src/Mage/Tests/Resources/testhost-without-releases.yml create mode 100644 src/Mage/Tests/Resources/testhost.yml diff --git a/src/Mage/MageApplication.php b/src/Mage/MageApplication.php index 4c0fd9f..13ae34a 100644 --- a/src/Mage/MageApplication.php +++ b/src/Mage/MageApplication.php @@ -74,7 +74,7 @@ class MageApplication extends Application $logger->pushHandler(new StreamHandler($logfile)); } - $this->runtime = new Runtime(); + $this->runtime = $this->instantiateRuntime(); $this->runtime->setConfiguration($config); $this->runtime->setLogger($logger); @@ -105,4 +105,24 @@ class MageApplication extends Application } } } + + /** + * Gets the Runtime instance to use + * + * @return Runtime + */ + protected function instantiateRuntime() + { + return new Runtime(); + } + + /** + * Get the Runtime instance + * + * @return Runtime + */ + public function getRuntime() + { + return $this->runtime; + } } diff --git a/src/Mage/Task/BuiltIn/Composer/InstallTask.php b/src/Mage/Task/BuiltIn/Composer/InstallTask.php index f449e40..37d8723 100644 --- a/src/Mage/Task/BuiltIn/Composer/InstallTask.php +++ b/src/Mage/Task/BuiltIn/Composer/InstallTask.php @@ -33,7 +33,7 @@ class InstallTask extends AbstractTask public function execute() { $options = $this->getOptions(); - $command = $options['path'] . ' install ' . $options['flags']; + $command = trim($options['path'] . ' install ' . $options['flags']); /** @var Process $process */ $process = $this->runtime->runCommand($command); @@ -45,7 +45,7 @@ class InstallTask extends AbstractTask { $userOptions = $this->runtime->getConfigOptions('composer', []); $options = array_merge( - ['path' => 'composer', 'flags' => '--dev'], + ['path' => 'composer', 'flags' => ''], (is_array($userOptions) ? $userOptions : []), $this->options ); diff --git a/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php index 6440ad0..f7ad66e 100644 --- a/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php @@ -12,8 +12,7 @@ namespace Mage\Tests\Command\BuiltIn\Config; use Mage\Command\BuiltIn\Config\DumpCommand; use Mage\Command\AbstractCommand; -use Mage\Tests\MageTestApplication; -use Mage\Tests\Runtime\RuntimeMockup; +use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit_Framework_TestCase as TestCase; @@ -21,12 +20,12 @@ class DumpCommandTest extends TestCase { public function testConfigDumpTermination() { - $application = new MageTestApplication(); - $application->add(new DumpCommand()); + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/basic.yml'); /** @var AbstractCommand $command */ $command = $application->find('config:dump'); - $command->setRuntime(new RuntimeMockup()); + $this->assertTrue($command instanceof DumpCommand); $tester = new CommandTester($command); $tester->execute(['command' => $command->getName()]); diff --git a/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php index 9bbf63f..79f78ae 100644 --- a/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php @@ -12,8 +12,7 @@ namespace Mage\Tests\Command\BuiltIn\Config; use Mage\Command\BuiltIn\Config\EnvironmentsCommand; use Mage\Command\AbstractCommand; -use Mage\Tests\MageTestApplication; -use Mage\Tests\Runtime\RuntimeMockup; +use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit_Framework_TestCase as TestCase; @@ -21,78 +20,12 @@ class EnvironmentsCommandTest extends TestCase { public function testConfigDumpTermination() { - $application = new MageTestApplication(); - $application->add(new EnvironmentsCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'releases' => 4, - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/basic.yml'); /** @var AbstractCommand $command */ $command = $application->find('config:environments'); - $command->setRuntime($runtime); + $this->assertTrue($command instanceof EnvironmentsCommand); $tester = new CommandTester($command); $tester->execute(['command' => $command->getName()]); diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php new file mode 100644 index 0000000..af708d9 --- /dev/null +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php @@ -0,0 +1,249 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Command\BuiltIn; + +use Mage\Command\BuiltIn\DeployCommand; +use Mage\Tests\MageApplicationMockup; +use Mage\Command\AbstractCommand; +use Symfony\Component\Console\Tester\CommandTester; +use PHPUnit_Framework_TestCase as TestCase; + +class DeployCommandMiscTest extends TestCase +{ + public function testDeploymentWithSudo() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-sudo.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 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install', + 4 => 'composer dumpautoload --optimize', + 5 => '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', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console cache:warmup --env=dev \\"', + 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console assets:install --env=dev --symlink --relative web\\"', + 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console assetic:dump --env=dev \\"', + 9 => 'git checkout master', + ); + + // 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 testDeploymentWithBranchOverwrite() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-without-releases.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test', '--branch' => 'maintenance']); + + $ranCommands = $application->getRuntime()->getRanCommands(); + + $testCase = array( + 0 => 'git branch | grep "*"', + 1 => 'git checkout maintenance', + 2 => 'git pull', + 3 => 'composer install', + 4 => 'composer dumpautoload --optimize', + 5 => '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', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"', + 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', + 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"', + 9 => 'git checkout master', + ); + + // 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 testDeploymentWithCustomTask() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-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' => 'test']); + + $ranCommands = $application->getRuntime()->getRanCommands(); + + $testCase = array( + 0 => 'composer install', + 1 => 'composer dumpautoload --optimize', + 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->assertTrue(strpos($tester->getDisplay(), '[Custom] Dummy Task') !== false); + + $this->assertEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentWithErrorTaskCommands() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-with-error.yml'); + + $application->getRuntime()->setReleaseId('20170101015120'); + + /** @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 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install', + 4 => 'composer dumpautoload --optimize', + 5 => 'tar cfz /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"mkdir -p /var/www/test/releases/1234567890\\"', + 7 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ', + 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& tar xfz mageXYZ\\"', + 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm /var/www/test/releases/1234567890/mageXYZ\\"', + ); + + // 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->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false); + + $this->assertNotEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentWithFailingPostDeployTaskCommands() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-with-postdeploy-error.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 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install', + 4 => 'composer dumpautoload --optimize', + 5 => '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', + 6 => 'git checkout master', + ); + + // 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->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false); + + $this->assertNotEquals(0, $tester->getStatusCode()); + } + + + public function testDeploymentWithSkippingTask() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-skipping.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 => 'git branch | grep "*"', + 1 => 'git pull', + 2 => 'composer install', + 3 => 'composer dumpautoload --optimize', + 4 => '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', + 5 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', + 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"', + 8 => 'git branch | grep "*"', + ); + + // 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->assertTrue(strpos($tester->getDisplay(), 'SKIPPED') !== false); + + $this->assertEquals(0, $tester->getStatusCode()); + } +} diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php deleted file mode 100644 index 9051b0a..0000000 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php +++ /dev/null @@ -1,785 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Mage\Tests\Command\BuiltIn; - -use Mage\Command\BuiltIn\DeployCommand; -use Mage\Command\AbstractCommand; -use Mage\Tests\MageTestApplication; -use Mage\Tests\Runtime\RuntimeMockup; -use Symfony\Component\Console\Tester\CommandTester; -use PHPUnit_Framework_TestCase as TestCase; - -class DeployCommandTest extends TestCase -{ - public function testDeploymentWithReleasesCommands() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'releases' => 4, - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - $runtime->setReleaseId('20170101015120'); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git checkout test', - 2 => 'git pull', - 3 => 'composer install --dev', - 4 => 'composer dumpautoload --optimize', - 5 => 'tar cfz /tmp/mageXYZ --exclude=".git" --exclude="vendor" --exclude="app/cache" --exclude="app/log" --exclude="web/app_dev.php" ./', - 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"mkdir -p /var/www/test/releases/1234567890\\"', - 7 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ', - 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& tar xfz mageXYZ\\"', - 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm /var/www/test/releases/1234567890/mageXYZ\\"', - 10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:clear --env=dev \\"', - 11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:warmup --env=dev \\"', - 12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', - 13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assetic:dump --env=dev \\"', - 14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& ln -snf releases/1234567890 current\\"', - 15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"', - 16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015110\\"', - 17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015111\\"', - 18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015112\\"', - 19 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015113\\"', - 20 => 'rm /tmp/mageXYZ', - 21 => 'git checkout master', - ); - - // 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 testDeploymentWithErrorTaskCommands() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'releases' => 4, - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => 'deploy/rsync', - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - $runtime->setReleaseId('20170101015120'); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git checkout test', - 2 => 'git pull', - 3 => 'composer install --dev', - 4 => 'composer dumpautoload --optimize', - 5 => 'tar cfz /tmp/mageXYZ --exclude=".git" --exclude="vendor" --exclude="app/cache" --exclude="app/log" --exclude="web/app_dev.php" ./', - 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"mkdir -p /var/www/test/releases/1234567890\\"', - 7 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ', - 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& tar xfz mageXYZ\\"', - 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm /var/www/test/releases/1234567890/mageXYZ\\"', - ); - - // 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->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false); - - $this->assertNotEquals(0, $tester->getStatusCode()); - } - - public function testDeploymentWithFailingPostDeployTaskCommands() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => 'deploy/rsync', - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => - array( - 0 => 'deploy/targz/cleanup', - ), - ), - ), - ) - ); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git checkout test', - 2 => 'git pull', - 3 => 'composer install --dev', - 4 => 'composer dumpautoload --optimize', - 5 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test', - 6 => 'git checkout master', - ); - - // 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->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false); - - $this->assertNotEquals(0, $tester->getStatusCode()); - } - - public function testDeploymentWithoutReleasesCommands() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git checkout test', - 2 => 'git pull', - 3 => 'composer install --dev', - 4 => 'composer dumpautoload --optimize', - 5 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test', - 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:clear --env=dev \\"', - 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"', - 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', - 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"', - 10 => 'git checkout master', - ); - - // 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 testDeploymentWithSudo() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'sudo' => true, - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git checkout test', - 2 => 'git pull', - 3 => 'composer install --dev', - 4 => 'composer dumpautoload --optimize', - 5 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test', - 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console cache:clear --env=dev \\"', - 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console cache:warmup --env=dev \\"', - 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console assets:install --env=dev --symlink --relative web\\"', - 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& sudo bin/console assetic:dump --env=dev \\"', - 10 => 'git checkout master', - ); - - // 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 testDeploymentWithBranchOverwrite() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test', '--branch' => 'maintenance']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git checkout maintenance', - 2 => 'git pull', - 3 => 'composer install --dev', - 4 => 'composer dumpautoload --optimize', - 5 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test', - 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:clear --env=dev \\"', - 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"', - 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', - 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"', - 10 => 'git checkout master', - ); - - // 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 testDeploymentWithSkippingTask() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'host_path' => '/var/www/test', - 'branch' => 'master', - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'git branch | grep "*"', - 1 => 'git pull', - 2 => 'composer install --dev', - 3 => 'composer dumpautoload --optimize', - 4 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test', - 5 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:clear --env=dev \\"', - 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"', - 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', - 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"', - 9 => 'git branch | grep "*"', - ); - - // 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->assertTrue(strpos($tester->getDisplay(), 'SKIPPED') !== false); - - $this->assertEquals(0, $tester->getStatusCode()); - } - - public function testDeploymentWithCustomTask() - { - $application = new MageTestApplication(); - $application->add(new DeployCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'host_path' => '/var/www/test', - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => 'Mage\\Tests\\Task\\CustomTask', - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); - - /** @var AbstractCommand $command */ - $command = $application->find('deploy'); - $command->setRuntime($runtime); - - $tester = new CommandTester($command); - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'composer install --dev', - 1 => 'composer dumpautoload --optimize', - 2 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/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->assertTrue(strpos($tester->getDisplay(), '[Custom] Dummy Task') !== false); - - $this->assertEquals(0, $tester->getStatusCode()); - } -} diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php new file mode 100644 index 0000000..e643d69 --- /dev/null +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Command\BuiltIn; + +use Mage\Command\BuiltIn\DeployCommand; +use Mage\Command\AbstractCommand; +use Mage\Tests\MageApplicationMockup; +use Symfony\Component\Console\Tester\CommandTester; +use PHPUnit_Framework_TestCase as TestCase; + +class DeployCommandWithReleasesTest extends TestCase +{ + public function testDeploymentWithReleasesCommands() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost.yml'); + + $application->getRuntime()->setReleaseId('20170101015120'); + + /** @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 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install', + 4 => 'composer dumpautoload --optimize', + 5 => 'tar cfz /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"mkdir -p /var/www/test/releases/1234567890\\"', + 7 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ', + 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& tar xfz mageXYZ\\"', + 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm /var/www/test/releases/1234567890/mageXYZ\\"', + 10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:warmup --env=dev \\"', + 11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', + 12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assetic:dump --env=dev \\"', + 13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& ln -snf releases/1234567890 current\\"', + 14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"', + 15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015110\\"', + 16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015111\\"', + 17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015112\\"', + 18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015113\\"', + 19 => 'rm /tmp/mageXYZ', + 20 => 'git checkout master', + ); + + // 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()); + } +} diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php new file mode 100644 index 0000000..c601d37 --- /dev/null +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Command\BuiltIn; + +use Mage\Command\BuiltIn\DeployCommand; +use Mage\Command\AbstractCommand; +use Mage\Tests\MageApplicationMockup; +use Symfony\Component\Console\Tester\CommandTester; +use PHPUnit_Framework_TestCase as TestCase; + +class DeployCommandWithoutReleasesTest extends TestCase +{ + public function testDeploymentWithoutReleasesCommands() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-without-releases.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 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install', + 4 => 'composer dumpautoload --optimize', + 5 => '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', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"', + 7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"', + 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"', + 9 => 'git checkout master', + ); + + // 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()); + } + + } diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php index 7c2465c..9bfa109 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php @@ -12,8 +12,7 @@ namespace Mage\Tests\Command\BuiltIn\Releases; use Mage\Command\BuiltIn\Releases\ListCommand; use Mage\Command\AbstractCommand; -use Mage\Tests\MageTestApplication; -use Mage\Tests\Runtime\RuntimeMockup; +use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit_Framework_TestCase as TestCase; @@ -21,83 +20,17 @@ class ListCommandTest extends TestCase { public function testListReleasesCommands() { - $application = new MageTestApplication(); - $application->add(new ListCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'releases' => 4, - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost.yml'); /** @var AbstractCommand $command */ $command = $application->find('releases:list'); - $command->setRuntime($runtime); + $this->assertTrue($command instanceof ListCommand); $tester = new CommandTester($command); $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - $ranCommands = $runtime->getRanCommands(); + $ranCommands = $application->getRuntime()->getRanCommands(); $testCase = array( 0 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"', diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php index 223716c..af02382 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php @@ -12,8 +12,7 @@ namespace Mage\Tests\Command\BuiltIn\Releases; use Mage\Command\BuiltIn\Releases\RollbackCommand; use Mage\Command\AbstractCommand; -use Mage\Tests\MageTestApplication; -use Mage\Tests\Runtime\RuntimeMockup; +use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit_Framework_TestCase as TestCase; @@ -21,83 +20,17 @@ class RollbackCommandTest extends TestCase { public function testRollbackReleaseCommands() { - $application = new MageTestApplication(); - $application->add(new RollbackCommand()); - - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(array( - 'environments' => - array( - 'test' => - array( - 'user' => 'tester', - 'branch' => 'test', - 'host_path' => '/var/www/test', - 'releases' => 4, - 'exclude' => - array( - 0 => 'vendor', - 1 => 'app/cache', - 2 => 'app/log', - 3 => 'web/app_dev.php', - ), - 'hosts' => - array( - 0 => 'testhost', - ), - 'pre-deploy' => - array( - 0 => 'git/update', - 1 => 'composer/install', - 2 => 'composer/generate-autoload', - ), - 'on-deploy' => - array( - 0 => - array( - 'symfony/cache-clear' => - array( - 'env' => 'dev', - ), - ), - 1 => - array( - 'symfony/cache-warmup' => - array( - 'env' => 'dev', - ), - ), - 2 => - array( - 'symfony/assets-install' => - array( - 'env' => 'dev', - ), - ), - 3 => - array( - 'symfony/assetic-dump' => - array( - 'env' => 'dev', - ), - ), - ), - 'on-release' => null, - 'post-release' => null, - 'post-deploy' => null, - ), - ), - ) - ); + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost.yml'); /** @var AbstractCommand $command */ $command = $application->find('releases:rollback'); - $command->setRuntime($runtime); + $this->assertTrue($command instanceof RollbackCommand); $tester = new CommandTester($command); $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); - $ranCommands = $runtime->getRanCommands(); + $ranCommands = $application->getRuntime()->getRanCommands(); $testCase = array( 0 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"', diff --git a/src/Mage/Tests/Command/BuiltIn/VersionCommandTest.php b/src/Mage/Tests/Command/BuiltIn/VersionCommandTest.php index 80ec95b..2e39690 100644 --- a/src/Mage/Tests/Command/BuiltIn/VersionCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/VersionCommandTest.php @@ -10,10 +10,9 @@ namespace Mage\Tests\Command\BuiltIn; -use Mage\Command\BuiltIn\VersionCommand; use Mage\Command\AbstractCommand; -use Mage\Tests\MageTestApplication; -use Mage\Tests\Runtime\RuntimeMockup; +use Mage\Command\BuiltIn\VersionCommand; +use Mage\Tests\MageApplicationMockup; use Mage\Mage; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit_Framework_TestCase as TestCase; @@ -22,12 +21,12 @@ class VersionCommandTest extends TestCase { public function testVersionOutput() { - $application = new MageTestApplication(); - $application->add(new VersionCommand()); + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/basic.yml'); /** @var AbstractCommand $command */ $command = $application->find('version'); - $command->setRuntime(new RuntimeMockup()); + $this->assertTrue($command instanceof VersionCommand); $tester = new CommandTester($command); $tester->execute(['command' => $command->getName()]); diff --git a/src/Mage/Tests/MageApplicationMockup.php b/src/Mage/Tests/MageApplicationMockup.php new file mode 100644 index 0000000..674c195 --- /dev/null +++ b/src/Mage/Tests/MageApplicationMockup.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests; + +use Mage\Tests\Runtime\RuntimeMockup; +use Mage\MageApplication; + +class MageApplicationMockup extends MageApplication +{ + /** + * Gets the Runtime instance to use + * + * @return RuntimeMockup + */ + protected function instantiateRuntime() + { + return new RuntimeMockup(); + } +} diff --git a/src/Mage/Tests/MageTestApplication.php b/src/Mage/Tests/MageTestApplication.php deleted file mode 100644 index a1d55e1..0000000 --- a/src/Mage/Tests/MageTestApplication.php +++ /dev/null @@ -1,17 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Mage\Tests; - -use Symfony\Component\Console\Application; - -class MageTestApplication extends Application -{ -} diff --git a/src/Mage/Tests/Resources/testhost-custom-task.yml b/src/Mage/Tests/Resources/testhost-custom-task.yml new file mode 100644 index 0000000..6ca959a --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-custom-task.yml @@ -0,0 +1,20 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - composer/install + - composer/generate-autoload + on-deploy: + - Mage\Tests\Task\CustomTask + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-skipping.yml b/src/Mage/Tests/Resources/testhost-skipping.yml new file mode 100644 index 0000000..c32a50b --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-skipping.yml @@ -0,0 +1,24 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: master + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/generate-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/assetic-dump: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-sudo.yml b/src/Mage/Tests/Resources/testhost-sudo.yml new file mode 100644 index 0000000..6d51227 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-sudo.yml @@ -0,0 +1,25 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + sudo: true + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/generate-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/assetic-dump: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-with-error.yml b/src/Mage/Tests/Resources/testhost-with-error.yml new file mode 100644 index 0000000..e12e3ab --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-with-error.yml @@ -0,0 +1,23 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/generate-autoload + on-deploy: + - deploy/rsync + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-with-postdeploy-error.yml b/src/Mage/Tests/Resources/testhost-with-postdeploy-error.yml new file mode 100644 index 0000000..ba827e2 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-with-postdeploy-error.yml @@ -0,0 +1,23 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/generate-autoload + on-deploy: + - deploy/rsync + on-release: + post-release: + post-deploy: + - deploy/targz/cleanup \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-without-releases.yml b/src/Mage/Tests/Resources/testhost-without-releases.yml new file mode 100644 index 0000000..d3f5810 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-without-releases.yml @@ -0,0 +1,24 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/generate-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/assetic-dump: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost.yml b/src/Mage/Tests/Resources/testhost.yml new file mode 100644 index 0000000..ecfe344 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost.yml @@ -0,0 +1,25 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/generate-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/assetic-dump: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/src/Mage/Tests/Runtime/RuntimeMockup.php b/src/Mage/Tests/Runtime/RuntimeMockup.php index 496c7ea..23a05bf 100644 --- a/src/Mage/Tests/Runtime/RuntimeMockup.php +++ b/src/Mage/Tests/Runtime/RuntimeMockup.php @@ -60,4 +60,16 @@ class RuntimeMockup extends Runtime { return '/tmp/mageXYZ'; } + + /** + * Allows to set an invalid environments + * + * @param string $environment + * @return Runtime + */ + public function setInvalidEnvironment($environment) + { + $this->environment = $environment; + return $this; + } } diff --git a/src/Mage/Tests/Runtime/RuntimeTest.php b/src/Mage/Tests/Runtime/RuntimeTest.php index d8c4c84..3dcc997 100644 --- a/src/Mage/Tests/Runtime/RuntimeTest.php +++ b/src/Mage/Tests/Runtime/RuntimeTest.php @@ -49,6 +49,17 @@ class RuntimeTest extends TestCase $this->assertEquals(0, count($config)); } + public function testInvalidEnvironmentConfig() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['valid' => []]]); + $runtime->setInvalidEnvironment('invalid'); + $config = $runtime->getEnvironmentConfig(); + + $this->assertTrue(is_array($config)); + $this->assertEquals(0, count($config)); + } + public function testInvalidEnvironments() { try { diff --git a/src/Mage/Tests/UtilsTest.php b/src/Mage/Tests/UtilsTest.php index ddeb2ba..f2c1207 100644 --- a/src/Mage/Tests/UtilsTest.php +++ b/src/Mage/Tests/UtilsTest.php @@ -24,6 +24,7 @@ class UtilsTest extends TestCase $this->assertEquals('Post Deployment', Utils::getStageName(Runtime::POST_DEPLOY)); $this->assertEquals('On Release', Utils::getStageName(Runtime::ON_RELEASE)); $this->assertEquals('Post Release', Utils::getStageName(Runtime::POST_RELEASE)); + $this->assertEquals('invalid-stage', Utils::getStageName('invalid-stage')); } public function testReleaseDate()