Skip to content

Commit

Permalink
Added upstream:updates:status command (#1654)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaDethray authored Mar 9, 2017
1 parent 731bcac commit 34547e8
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. This projec
### Added
- Added an `--element=` option to `backup:list`. (#1563)
- Added the label column to `org:list`'s output. (#1612)
- Added the `upstream:updates:status` command to report whether any site environment is outdated or current. (#1654)

### Changed
- `self:cc` now acts to delete all files in the command cache directory. (#1569)
Expand Down
3 changes: 0 additions & 3 deletions src/Commands/Upstream/Updates/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Pantheon\Terminus\Commands\Upstream\Updates;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Pantheon\Terminus\Exceptions\TerminusException;

/**
* Class ListCommand
Expand All @@ -27,8 +26,6 @@ class ListCommand extends UpdatesCommand
*
* @param string $site_env Site & development environment
*
* @throws TerminusException
*
* @usage <site>.<env> Displays a list of new code commits available from the upstream for <site>'s <env> environment.
*/
public function listUpstreamUpdates($site_env)
Expand Down
28 changes: 28 additions & 0 deletions src/Commands/Upstream/Updates/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Pantheon\Terminus\Commands\Upstream\Updates;

/**
* Class StatusCommand
* @package Pantheon\Terminus\Commands\Upstream\Updates
*/
class StatusCommand extends UpdatesCommand
{
/**
* Displays a whether there are updates available from the upstream for a site's environment.
*
* @authorize
*
* @command upstream:updates:status
*
* @param string $site_env Site & environment in the format `site-name.env`
* @return string
*
* @usage <site>.<env> Displays the status of updates available from the upstream for <site>'s <env> environment.
*/
public function status($site_env)
{
list(, $env) = $this->getSiteEnv($site_env);
return $env->getUpstreamStatus()->getStatus();
}
}
12 changes: 11 additions & 1 deletion src/Models/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,16 @@ public function initializeBindings()
return $this->getWorkflows()->create('create_environment', compact('params'));
}

/**
* Is this branch a development environment?
*
* @return bool True if ths environment is a development environment
*/
public function isDevelopment()
{
return !in_array($this->id, ['test', 'live',]);
}

/**
* Have the environment's bindings have been initialized?
*
Expand All @@ -665,7 +675,7 @@ public function isInitialized()
*/
public function isMultidev()
{
return !in_array($this->id, ['dev', 'test', 'live']);
return !in_array($this->id, ['dev', 'test', 'live',]);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Models/UpstreamStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function getUpdates()
*/
public function hasUpdates()
{
return ($this->getUpdates()->behind > 0);
$updates = $this->getUpdates();
$env = $this->getEnvironment();
if ($env->isDevelopment()) {
return ($updates->behind > 0);
}
$parent_env_id = ($env->id === 'test') ? 'dev' : 'test';
return !($updates->{$env->id}->is_up_to_date_with_upstream && $updates->$parent_env_id->is_up_to_date_with_upstream);
}
}
19 changes: 19 additions & 0 deletions tests/features/upstream-updates-status.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: Check an environment's upstream update status
In order to easily maintain my site
As a user
I need to be able to check my environments' upstream update status.

Background: I am authenticated and I have a site named [[test_site_name]] using Git mode
Given I am authenticated
And a site named "[[test_site_name]]"
And the connection mode of "[[test_site_name]]" is "git"

@vcr upstream-updates.yml
Scenario: Check for upstream update status when it's current
When I run "terminus upstream:updates:status [[test_site_name]].dev"
Then I should get: "current"

@vcr upstream-update-list.yml
Scenario: Check for upstream updates status when it's outdated
When I run "terminus upstream:updates:status [[test_site_name]].dev"
Then I should get: "outdated"
40 changes: 40 additions & 0 deletions tests/unit_tests/Commands/Upstream/Updates/StatusCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Pantheon\Terminus\UnitTests\Commands\Upstream\Updates;

use Pantheon\Terminus\Commands\Upstream\Updates\StatusCommand;

/**
* Class StatusCommandTest
* Testing class for Pantheon\Terminus\Commands\Upstream\Updates\StatusCommand
* @package Pantheon\Terminus\UnitTests\Commands\Upstream\Updates
*/
class StatusCommandTest extends UpdatesCommandTest
{
/**
* @inheritdoc
*/
public function setUp()
{
parent::setUp();

$this->command = new StatusCommand($this->getConfig());
$this->command->setSites($this->sites);
}

/**
* Tests the StatusCommand::status($site_env) function
*/
public function testStatus()
{
$status = 'some status';

$this->upstream_status->expects($this->once())
->method('getStatus')
->with()
->willReturn($status);

$out = $this->command->status('site.env');
$this->assertEquals($status, $out);
}
}
15 changes: 15 additions & 0 deletions tests/unit_tests/Models/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,21 @@ public function testIsInitialized()
$this->assertFalse($model->isInitialized());
}

public function testIsDevelopment()
{
$model = $this->createModel(['id' => 'test',]);
$this->assertFalse($model->isDevelopment());

$model = $this->createModel(['id' => 'live',]);
$this->assertFalse($model->isDevelopment());

$model = $this->createModel(['id' => 'mymulti',]);
$this->assertTrue($model->isDevelopment());

$model = $this->createModel(['id' => 'dev',]);
$this->assertTrue($model->isDevelopment());
}

public function testIsMultidev()
{
$this->assertFalse($this->model->isMultidev());
Expand Down
117 changes: 91 additions & 26 deletions tests/unit_tests/Models/UpstreamStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ public function setUp()
*/
public function testGetStatusCurrent()
{
$this->request->expects($this->once())
->method('request')
->with($this->equalTo($this->request_url))
->willReturn(['data' => (object)['behind' => 0,],]);

$this->expectIsCurrent();
$this->assertEquals('current', $this->model->getStatus());
}

Expand All @@ -72,11 +68,7 @@ public function testGetStatusCurrent()
*/
public function testGetStatusOutdated()
{
$this->request->expects($this->once())
->method('request')
->with($this->equalTo($this->request_url))
->willReturn(['data' => (object)['behind' => 1,],]);

$this->expectIsBehind();
$this->assertEquals('outdated', $this->model->getStatus());
}

Expand All @@ -86,39 +78,112 @@ public function testGetStatusOutdated()
public function testGetUpdates()
{
$expected = 'return me';

$this->request->expects($this->once())
->method('request')
->with($this->equalTo($this->request_url))
->willReturn(['data' => $expected,]);

$this->expectRequest($expected);
$out = $this->model->getUpdates();
$this->assertEquals($expected, $out);
}

/**
* Tests UpstreamStatus::hasUpdates() when there are no updates
* Tests UpstreamStatus::hasUpdates() when there are no updates and the environment is test or live
*/
public function testHasNoUpdates()
{
$this->request->expects($this->once())
->method('request')
->with($this->equalTo($this->request_url))
->willReturn(['data' => (object)['behind' => 0,],]);

$return_data = (object)[
$this->environment->id => (object)['is_up_to_date_with_upstream' => true,],
'test' => (object)['is_up_to_date_with_upstream' => true,],
];
$this->expectIsDevelopment(false);
$this->expectRequest($return_data);
$this->assertFalse($this->model->hasUpdates());
}

/**
* Tests UpstreamStatus::hasUpdates() when there are updates
* Tests UpstreamStatus::hasUpdates() when there are updates and the environment is test or live and the named env is outdated
*/
public function testHasUpdates()
{
$return_data = (object)[
$this->environment->id => (object)['is_up_to_date_with_upstream' => false,],
'test' => (object)['is_up_to_date_with_upstream' => true,],
];
$this->expectIsDevelopment(false);
$this->expectRequest($return_data);
$this->assertTrue($this->model->hasUpdates());
}

/**
* Tests UpstreamStatus::hasUpdates() when there are updates and the environment is test or live and the named env's parent is outdated
*/
public function testHasUpdatesFromParent()
{
$return_data = (object)[
$this->environment->id => (object)['is_up_to_date_with_upstream' => true,],
'test' => (object)['is_up_to_date_with_upstream' => false,],
];
$this->expectIsDevelopment(false);
$this->expectRequest($return_data);
$this->assertTrue($this->model->hasUpdates());
}

/**
* Tests UpstreamStatus::hasUpdates() when there are no updates and the environment is a dev environment
*/
public function testHasNoUpdatesDev()
{
$this->expectIsCurrent();
$this->assertFalse($this->model->hasUpdates());
}

/**
* Tests UpstreamStatus::hasUpdates() when there are updates and the environment is a dev environment
*/
public function testHasUpdatesDev()
{
$this->expectIsBehind();
$this->assertTrue($this->model->hasUpdates());
}

/**
* Sets the test to expect a result saying that the dev environment is behind on updates
*/
private function expectIsBehind()
{
$this->expectIsDevelopment();
$this->expectRequest((object)['behind' => 1,]);
}

/**
* Sets the test to expect a result saying that the dev environment is current on updates
*/
private function expectIsCurrent()
{
$this->expectIsDevelopment();
$this->expectRequest((object)['behind' => 0,]);
}

/**
* Sets the test to expect a call to isDevelopment with the set return value
*
* @param boolean
*/
private function expectIsDevelopment($is_dev = true)
{
$this->environment->expects($this->once())
->method('isDevelopment')
->with()
->willReturn($is_dev);
}

/**
* Sets the test to expect a request with specific returned data
*
* @param mixed
*/
private function expectRequest($return_data = null)
{
$this->request->expects($this->once())
->method('request')
->with($this->equalTo($this->request_url))
->willReturn(['data' => (object)['behind' => 1,],]);

$this->assertTrue($this->model->hasUpdates());
->willReturn(['data' => $return_data,]);
}
}

0 comments on commit 34547e8

Please sign in to comment.