From f4f8bfbf05d1d377a9615e21d6a0d695772f5f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Wed, 4 Jan 2017 22:21:38 -0300 Subject: [PATCH] [Nostromo] Improve tests and coverage --- .../Command/BuiltIn/DeployCommandMiscTest.php | 10 ++--- .../BuiltIn/Releases/ListCommandTest.php | 37 +++++++++++++++++++ .../BuiltIn/Releases/RollbackCommandTest.php | 36 ++++++++++++++++++ src/Mage/Tests/MageApplicationTest.php | 20 +++++++++- src/Mage/Tests/Resources/testhost-sudo.yml | 1 + 5 files changed, 98 insertions(+), 6 deletions(-) diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php index af708d9..6223d3f 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php @@ -39,10 +39,11 @@ class DeployCommandMiscTest extends TestCase 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', + 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 @@ -207,7 +208,6 @@ class DeployCommandMiscTest extends TestCase $this->assertNotEquals(0, $tester->getStatusCode()); } - public function testDeploymentWithSkippingTask() { $application = new MageApplicationMockup(); diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php index 9bfa109..5261fc9 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php @@ -11,9 +11,11 @@ namespace Mage\Tests\Command\BuiltIn\Releases; use Mage\Command\BuiltIn\Releases\ListCommand; +use Mage\Runtime\Exception\DeploymentException; use Mage\Command\AbstractCommand; use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; +use Exception; use PHPUnit_Framework_TestCase as TestCase; class ListCommandTest extends TestCase @@ -45,4 +47,39 @@ class ListCommandTest extends TestCase $this->assertEquals($command, $ranCommands[$index]); } } + + public function testListReleasesWithInvalidEnvironment() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:list'); + $this->assertTrue($command instanceof ListCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'developers']); + + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('The environment "developers" does not exists.', $tester->getDisplay()); + } + + public function testListReleasesWithoutReleases() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-without-releases.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:list'); + $this->assertTrue($command instanceof ListCommand); + + $tester = new CommandTester($command); + + try { + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof DeploymentException); + $this->assertEquals('Releases are not enabled', $exception->getMessage()); + } + } } diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php index af02382..4d9b15b 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php @@ -12,8 +12,10 @@ namespace Mage\Tests\Command\BuiltIn\Releases; use Mage\Command\BuiltIn\Releases\RollbackCommand; use Mage\Command\AbstractCommand; +use Mage\Runtime\Exception\DeploymentException; use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; +use Exception; use PHPUnit_Framework_TestCase as TestCase; class RollbackCommandTest extends TestCase @@ -45,4 +47,38 @@ class RollbackCommandTest extends TestCase $this->assertEquals($command, $ranCommands[$index]); } } + + public function testRollbackReleaseWithInvalidEnvironment() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:rollback'); + $this->assertTrue($command instanceof RollbackCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'developers', 'release' => '20170101015115']); + + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('The environment "developers" does not exists.', $tester->getDisplay()); + } + + public function testRollbackReleaseWithoutReleases() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-without-releases.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:rollback'); + $this->assertTrue($command instanceof RollbackCommand); + + try { + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof DeploymentException); + $this->assertEquals('Releases are not enabled', $exception->getMessage()); + } + } } diff --git a/src/Mage/Tests/MageApplicationTest.php b/src/Mage/Tests/MageApplicationTest.php index a7c0c50..ef08725 100644 --- a/src/Mage/Tests/MageApplicationTest.php +++ b/src/Mage/Tests/MageApplicationTest.php @@ -12,6 +12,7 @@ namespace Mage\Tests; use Mage\MageApplication; use Mage\Runtime\Exception\RuntimeException; +use Symfony\Component\Console\Tester\ApplicationTester; use Exception; use PHPUnit_Framework_TestCase as TestCase; @@ -35,7 +36,7 @@ class MageApplicationTest extends TestCase } } - public function testInValidFile() + public function testInvalidFile() { try { $application = new MageApplication(); @@ -45,4 +46,21 @@ class MageApplicationTest extends TestCase $this->assertEquals(sprintf('The file "%s" does not exists or is not readable.', __DIR__ . '/Resources/this-does-not-exists.yml'), $exception->getMessage()); } } + + public function testAppDispatcher() + { + $application = new MageApplication(); + $application->setAutoExit(false); + $application->configure(__DIR__ . '/Resources/basic.yml'); + $this->assertTrue($application instanceof MageApplication); + + $application->register('foo')->setCode(function() { + throw new \RuntimeException('foo'); + }); + + $tester = new ApplicationTester($application); + $tester->run(['command' => 'foo']); + + $this->assertContains('Oops, exception thrown while running command foo', $tester->getDisplay()); + } } diff --git a/src/Mage/Tests/Resources/testhost-sudo.yml b/src/Mage/Tests/Resources/testhost-sudo.yml index 6d51227..d323f1c 100644 --- a/src/Mage/Tests/Resources/testhost-sudo.yml +++ b/src/Mage/Tests/Resources/testhost-sudo.yml @@ -17,6 +17,7 @@ magephp: - composer/install - composer/generate-autoload on-deploy: + - symfony/cache-clear: { env: 'dev' } - symfony/cache-warmup: { env: 'dev' } - symfony/assets-install: { env: 'dev' } - symfony/assetic-dump: { env: 'dev' }