From c303a9bc241ddd9b60210b10751eb2169dbbae03 Mon Sep 17 00:00:00 2001 From: Mark Theunissen Date: Fri, 23 Sep 2016 12:52:08 +0200 Subject: [PATCH] Adding unit tests for env:deploy command --- src/Commands/Env/DeployCommand.php | 19 ++- tests/active_features/env-deploy.feature | 6 +- .../Commands/Env/DeployCommandTest.php | 122 ++++++++++++++++++ .../Commands/Env/EnvCommandTest.php | 61 +++++++++ 4 files changed, 200 insertions(+), 8 deletions(-) create mode 100644 tests/new_unit_tests/Commands/Env/DeployCommandTest.php create mode 100644 tests/new_unit_tests/Commands/Env/EnvCommandTest.php diff --git a/src/Commands/Env/DeployCommand.php b/src/Commands/Env/DeployCommand.php index bbdc4d4b5..7e4f4606e 100644 --- a/src/Commands/Env/DeployCommand.php +++ b/src/Commands/Env/DeployCommand.php @@ -8,12 +8,21 @@ class DeployCommand extends TerminusCommand { + /** + * Contructor that supports injection for unit testing. + */ + public function __construct($sites = null) + { + parent::__construct(); + $this->sites = $sites ? $sites : new Sites(); + } + /** * Deploy the dev environment to test or live. * * @command env:deploy * - * @param string $env Environment to deploy to + * @param string $site_env Site & environment to deploy to, in the form `site-name.env`. * * @option string $site Site to deploy from * @option string $sync-content If deploying test, copy database and files from Live @@ -24,16 +33,16 @@ class DeployCommand extends TerminusCommand * @usage terminus env:deploy test --site=my-site-1 * Deploy from dev to test environment */ - public function deploy($env, $options = [ + public function deploy($site_env, $options = [ 'site' => '', 'sync-content' => false, 'note' => 'Deploy from Terminus', 'cc' => false, 'updatedb' => false]) { - $sites = new Sites(); - $site = $sites->get($options['site']); - $env = $site->environments->get($env); + list($site_name, $env_name) = explode('.', $site_env); + $site = $this->sites->get($site_name); + $env = $site->environments->get($env_name); if (!$env->hasDeployableCode()) { $this->log()->info('There is nothing to deploy.'); diff --git a/tests/active_features/env-deploy.feature b/tests/active_features/env-deploy.feature index 7ef5ed305..49efa089a 100644 --- a/tests/active_features/env-deploy.feature +++ b/tests/active_features/env-deploy.feature @@ -9,7 +9,7 @@ Feature: Site Deployment @vcr site_deploy Scenario: Deploy dev to test - When I run "terminus env:deploy test --site=[[test_site_name]] --sync-content --note='Deploy test'" + When I run "terminus env:deploy [[test_site_name]].test --note='Deploy test' --sync-content" Then I should get "." And I should get "." Then I should get: @@ -19,12 +19,12 @@ Feature: Site Deployment @vcr site_deploy_no_changes Scenario: Failing to deploy dev to test because there are no changes to deploy - When I run "terminus env:deploy test --site=[[test_site_name]] --sync-content --note='Deploy test'" + When I run "terminus env:deploy [[test_site_name]].test --note='Deploy test' --sync-content" Then I should get: "There is nothing to deploy." @vcr env-deploy Scenario: Initializing test when it has not been previously initialized - When I run "terminus env:deploy test --site=[[test_site_name]] --sync-content --note='First deploy to live'" + When I run "terminus env:deploy [[test_site_name]].test --note='First deploy to live' --sync-content" Then I should get "." And I should get "." Then I should get: diff --git a/tests/new_unit_tests/Commands/Env/DeployCommandTest.php b/tests/new_unit_tests/Commands/Env/DeployCommandTest.php new file mode 100644 index 000000000..b23e379db --- /dev/null +++ b/tests/new_unit_tests/Commands/Env/DeployCommandTest.php @@ -0,0 +1,122 @@ +command = new DeployCommand($this->sites); + $this->command->setLogger($this->logger); + } + + + /** + * Tests the env:deploy command success with all parameters. + * + * @return void + */ + public function testDeploy() + { + $this->env->id = 'test'; + + $this->env->expects($this->once()) + ->method('hasDeployableCode') + ->willReturn(true); + + $this->env->expects($this->once()) + ->method('deploy') + ->willReturn($this->workflow) + ->with([ + 'updatedb' => 0, + 'clear_cache' => 0, + 'annotation' => 'Deploy from Terminus', + 'clone_database' => [ + 'from_environment' => 'live' + ], + 'clone_files' => [ + 'from_environment' => 'live' + ] + ]); + + $this->workflow->expects($this->once()) + ->method('wait'); + + // Run the deploy. + $this->command->deploy('mysite.test', [ + 'sync-content' => true, + 'note' => 'Deploy from Terminus', + 'cc' => false, + 'updatedb' => false, + ]); + } + + /** + * Tests the env:deploy command where no code is deployable. + * + * @return void + */ + public function testDeployNoCode() + { + $this->env->id = 'test'; + + $this->env->expects($this->once()) + ->method('hasDeployableCode') + ->willReturn(false); + + $this->env->expects($this->never()) + ->method('deploy'); + + $this->logger->expects($this->once()) + ->method('log'); + + // Run the deploy. + $this->command->deploy('mysite.test'); + } + + /** + * Tests the env:deploy command to live. + * + * @return void + */ + public function testDeployLive() + { + $this->env->id = 'live'; + + $this->env->expects($this->once()) + ->method('hasDeployableCode') + ->willReturn(true); + + $this->env->expects($this->once()) + ->method('deploy') + ->willReturn($this->workflow) + ->with([ + 'updatedb' => 1, + 'clear_cache' => 1, + 'annotation' => 'Deploy from Terminus', + ]); + + $this->workflow->expects($this->once()) + ->method('wait'); + + // Run the deploy. + $this->command->deploy('mysite.live', [ + 'sync-content' => true, + 'note' => 'Deploy from Terminus', + 'cc' => true, + 'updatedb' => true, + ]); + } +} diff --git a/tests/new_unit_tests/Commands/Env/EnvCommandTest.php b/tests/new_unit_tests/Commands/Env/EnvCommandTest.php new file mode 100644 index 000000000..b7eb70e0c --- /dev/null +++ b/tests/new_unit_tests/Commands/Env/EnvCommandTest.php @@ -0,0 +1,61 @@ +sites = $this->getMockBuilder(Sites::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->site = $this->getMockBuilder(Site::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->sites->method('get') + ->willReturn($this->site); + + $this->site->environments = $this->getMockBuilder(Environments::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->env = $this->getMockBuilder(Environment::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->site->environments->method('get') + ->willReturn($this->env); + + $this->logger = $this->getMockBuilder(NullLogger::class) + ->setMethods(array('log')) + ->getMock(); + + $this->workflow = $this->getMockBuilder(Workflow::class) + ->disableOriginalConstructor() + ->getMock(); + } +}