Skip to content

Commit

Permalink
Merge pull request #1242 from pantheon-systems/env_deploy_unit_tests
Browse files Browse the repository at this point in the history
Env:deploy unit tests
  • Loading branch information
marktheunissen authored Sep 26, 2016
2 parents 03e10ab + f0ed165 commit cf0b87d
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/Commands/Env/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.');
Expand Down
6 changes: 3 additions & 3 deletions tests/active_features/env-deploy.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
122 changes: 122 additions & 0 deletions tests/new_unit_tests/Commands/Env/DeployCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
namespace Pantheon\Terminus\UnitTests\Commands\Env;

use Pantheon\Terminus\Commands\Env\DeployCommand;
use Pantheon\Terminus\Config;
use Terminus\Exceptions\TerminusException;

/**
* Testing class for Pantheon\Terminus\Commands\Env\DeployCommand
*/
class DeployCommandTest extends EnvCommandTest
{

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
parent::setUp();
$this->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,
]);
}
}
61 changes: 61 additions & 0 deletions tests/new_unit_tests/Commands/Env/EnvCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Pantheon\Terminus\UnitTests\Commands\Env;

use Pantheon\Terminus\UnitTests\Commands\CommandTestCase;

use Pantheon\Terminus\Session\Session;
use Psr\Log\NullLogger;
use Terminus\Collections\Sites;
use Terminus\Models\Site;
use Terminus\Collections\Environments;
use Terminus\Models\Environment;
use Terminus\Models\Workflow;

/**
* @property \PHPUnit_Framework_MockObject_MockObject sites
*/
abstract class EnvCommandTest extends CommandTestCase
{
protected $session;
protected $sites;
protected $user;
protected $logger;
protected $command;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->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();
}
}

0 comments on commit cf0b87d

Please sign in to comment.