Skip to content

Commit

Permalink
Merge pull request #150 from ericduran/certify-command-test
Browse files Browse the repository at this point in the history
UGH THIS WAS NOT EASY
  • Loading branch information
ericduran committed Mar 26, 2015
2 parents b3221c1 + 7baea90 commit 9c9d683
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/flo/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Command extends \Symfony\Component\Console\Command\Command {
const GITHUB_PULL_REQUEST_TARGET_BRANCH = 'ghprbTargetBranch';
const JENKINS_BUILD_URL = 'BUILD_URL';

private $github;
public $github;
private $repository;

/**
Expand All @@ -40,7 +40,7 @@ public function getConfigParameter($key) {
/**
* @return Github\Client
*/
public function getGithub($cache = TRUE) {
public function getGithub($cache = TRUE, $api = NULL) {
if (null === $this->github) {
if ($cache) {
$this->github = new Github\Client(
Expand All @@ -51,7 +51,12 @@ public function getGithub($cache = TRUE) {
$this->github = new Github\Client();
}
$this->github->authenticate($this->getConfigParameter('github_oauth_token'), NULL, Github\Client::AUTH_URL_TOKEN);

if ($api !== NULL) {
return $this->github->api($api);
}
}

return $this->github;
}

Expand Down Expand Up @@ -82,8 +87,9 @@ public function addGithubLabel($pr_number, $label) {
if (!is_numeric($pr_number)) {
throw new \Exception("PR must be a number.");
}
$github = $this->getGithub(FALSE);
$github->api('issue')->labels()->add(

$github = $this->getGithub(FALSE, 'issue');
$github->labels()->add(
$this->getConfigParameter('organization'),
$this->getConfigParameter('repository'),
$pr_number,
Expand Down
97 changes: 97 additions & 0 deletions tests/flo/Test/Command/PullRequest/CertifyCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace flo\Test\Command\PullRequest;

use flo\Test;
use flo\Console\Application;
use Github;
use Symfony\Component\Console\Tester\CommandTester;

class CertifyCommandTest extends Test\FunctionalFramework {

/**
* The main flo application.
*
* @var string
*/
private $application;

/**
* set up test environment filesystem.
*/
public function setUp() {
$this->application = new Application();
parent::setUp();
}

/**
* Test Running pr-deploy with an string instead of PR Number.
*
* @expectedException Exception
* @expectedExceptionMessageRegExp #PR must be a number.#
*/
public function testNANPRCertifyException() {
$command_run_script = $this->application->find('pr-certify');
$command_tester = new CommandTester($command_run_script);
$command_tester->execute(array(
'command' => $command_run_script->getName(),
'pull-request' => "Not-A-Valid-PR",
));
}

/**
* Test Running pr-deploy with an string instead of PR Number.
*/
public function testAddingCertifyLabel() {
$this->writeConfig();

//TODO: CLEAN THIS UP AND UNDERSTAND THIS!!!!!
//I FINALLY UNDERSTAND HOW TO MOCK OBJECTS!!!!!!!!!!!

$httpClientMock = $this->getMock('Guzzle\Http\Client', array('send'));
$httpClientMock
->expects($this->any())
->method('send');

$mock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $httpClientMock));
$client = new Github\Client($mock);
$client->setHttpClient($mock);

// Mock the Issue API.
$IssueMock = $this->getMockBuilder('Github\Api\Issue')
->setMethods(array('labels'))
->setConstructorArgs(array($client))
->getMock();

// Mock the label API.
$labelsMock = $this->getMockBuilder('Github\Api\Issue\Labels')
->setMethods(array('get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'))
->setConstructorArgs(array($client))
->getMock();

// This actually runs an assert and makes sure our API call actually returns that :-O.
$labelsMock->expects($this->once())
->method('post')
->with('repos/NBCUOTS/Publisher7_nbcuflo/issues/1/labels')
->will($this->returnValue('Test'));

// Set up the Issue API to return the Label api.
$IssueMock->expects($this->once())
->method('labels')
->willReturn($labelsMock);


// Now after ALLLLL that set up, lets call our command
$command_run_script = $this->application->find('pr-certify');
$command_run_script->github = $IssueMock;

$command_tester = new CommandTester($command_run_script);
$command_tester->execute(array(
'command' => $command_run_script->getName(),
'pull-request' => "1",
));

$this->assertEquals("PR #1 has been certified.\n", $command_tester->getDisplay());
}

}
23 changes: 23 additions & 0 deletions tests/flo/Test/FunctionalFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ public function setUp() {
parent::setUp();
}

/**
* Helper function to write configuration file.
*/
protected function writeConfig() {
// Create a sample flo.yml file.
$project_config = <<<EOT
---
organization: NBCUOTS
repository: Publisher7_nbcuflo
shortname: Publisher7_nbcuflo
github_git_uri: [email protected]:NBCUOTS/Publisher7_nbcuflo.git
pull_request:
domain: pr.publisher7.com
prefix: flo-test
scripts:
pre_deploy_cmd:
- scripts/pre-deploy.sh
post_deploy_cmd:
- scripts/post-deploy.sh
EOT;
$this->fs->dumpFile($this->root . "/flo.yml", $project_config);
}

/**
* Remove the files and directories created for this test.
*/
Expand Down

0 comments on commit 9c9d683

Please sign in to comment.