From b4f2b9b2243e426debb18ae566fd0e3ca5f9d5f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Wed, 4 Jan 2017 01:14:36 -0300 Subject: [PATCH] [Nostromo] Tests --- .../Command/BuiltIn/DeployCommandTest.php | 173 ++++++++++++++++++ src/Mage/Tests/MageApplicationTest.php | 49 +++++ src/Mage/Tests/Resources/basic.yml | 27 +++ src/Mage/Tests/Resources/invalid.yml | 1 + src/Mage/Tests/Task/CustomTask.php | 36 ++++ 5 files changed, 286 insertions(+) create mode 100644 src/Mage/Tests/MageApplicationTest.php create mode 100644 src/Mage/Tests/Resources/basic.yml create mode 100644 src/Mage/Tests/Resources/invalid.yml create mode 100644 src/Mage/Tests/Task/CustomTask.php diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php index c024fcd..9051b0a 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php @@ -505,6 +505,110 @@ class DeployCommandTest extends TestCase $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(); @@ -609,4 +713,73 @@ class DeployCommandTest extends TestCase $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/MageApplicationTest.php b/src/Mage/Tests/MageApplicationTest.php new file mode 100644 index 0000000..d871887 --- /dev/null +++ b/src/Mage/Tests/MageApplicationTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests; + +use Mage\MageApplication; +use Mage\Runtime\Exception\RuntimeException; +use Exception; +use PHPUnit_Framework_TestCase as TestCase; + +class MageApplicationTest extends TestCase +{ + public function testValidConfiguration() + { + $application = new MageApplication(); + $application->configure(__DIR__ . '/Resources/basic.yml'); + $this->assertTrue($application instanceof MageApplication); + } + + public function testInValidConfiguration() + { + try { + $application = new MageApplication(); + $application->configure(__DIR__ . '/Resources/invalid.yml'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof RuntimeException); + $this->assertEquals(sprintf('The file "%s" does not have a valid Magallanes configuration.', __DIR__ . '/Resources/invalid.yml'), $exception->getMessage()); + } + } + + public function testInValidFile() + { + try { + $application = new MageApplication(); + $application->configure(__DIR__ . '/Resources/this-does-not-exists.yml'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof RuntimeException); + $this->assertEquals(sprintf('The file "%s" does not exists or is not readable.', __DIR__ . '/Resources/this-does-not-exists.yml'), $exception->getMessage()); + } + } + +} \ No newline at end of file diff --git a/src/Mage/Tests/Resources/basic.yml b/src/Mage/Tests/Resources/basic.yml new file mode 100644 index 0000000..dd4cd8e --- /dev/null +++ b/src/Mage/Tests/Resources/basic.yml @@ -0,0 +1,27 @@ +magephp: + log_dir: /tmp + environments: + production: + user: app + branch: master + host_path: /var/www/myapp + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - webserver1 + - webserver2 + - webserver3 + 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/invalid.yml b/src/Mage/Tests/Resources/invalid.yml new file mode 100644 index 0000000..cd7e8d6 --- /dev/null +++ b/src/Mage/Tests/Resources/invalid.yml @@ -0,0 +1 @@ +mage: diff --git a/src/Mage/Tests/Task/CustomTask.php b/src/Mage/Tests/Task/CustomTask.php new file mode 100644 index 0000000..75c7acf --- /dev/null +++ b/src/Mage/Tests/Task/CustomTask.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task; + +use Mage\Task\AbstractTask; + +/** + * Custom Task for Testing + * + * @author Andrés Montañez + */ +class CustomTask extends AbstractTask +{ + public function getName() + { + return 'custom'; + } + + public function getDescription() + { + return '[Custom] Dummy Task'; + } + + public function execute() + { + return true; + } +}