Skip to content

Commit

Permalink
Merge 7428fd9 into e245044
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Nov 3, 2018
2 parents e245044 + 7428fd9 commit 157ecce
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 83 deletions.
2 changes: 0 additions & 2 deletions src/CosyComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,7 @@ public function run()
$default_base = null;
try {
$branches_flattened = $this->getPrClient()->getBranchesFlattened($branch_user, $user_repo);
$this->deleteTempToken();
$default_base = $this->getPrClient()->getDefaultBase($branch_user, $user_repo, $default_branch);
$this->deleteTempToken();
if ($default_base_upstream = $this->privateClient->getDefaultBase($user_name, $user_repo, $default_branch)) {
$default_base = $default_base_upstream;
}
Expand Down
6 changes: 5 additions & 1 deletion test/GetCosyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

trait GetCosyTrait
{
protected function getMockCosy()
protected function getMockCosy($dir = null)
{
$app = $this->createMock(Application::class);
$output = $this->createMock(ArrayOutput::class);
Expand All @@ -20,6 +20,10 @@ protected function getMockCosy()
$p->setNid(123);
$c->setProject($p);
$c->setTokenUrl('http://localhost:9988');
if ($dir) {
mkdir($dir);
$c->setTmpDir($dir);
}
return $c;
}
}
22 changes: 1 addition & 21 deletions test/integration/UpdatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,8 @@ function ($cmd) use (&$called, $dir) {

public function testEndToEndNotPrivate()
{
$c = $this->getMockCosy();
$dir = '/tmp/' . uniqid();
mkdir($dir);
$c->setTmpDir($dir);
$c = $this->getMockCosy($dir);
// Create a mock app, that can respond to things.
$mock_definition = $this->createMock(InputDefinition::class);
$mock_definition->method('getOptions')
Expand Down Expand Up @@ -708,24 +706,6 @@ function ($cmd) use (&$called, $dir) {
$this->assertEquals(false, $called);
$composer_lock_contents = file_get_contents(__DIR__ . '/../fixtures/composer-psr-log.lock');
file_put_contents("$dir/composer.lock", $composer_lock_contents);
// Create a fake http client.
$mock_200_response = new Response(200, [], '{"token":123}');
$mock_204_response = new Response(204);
$mock_client = $this->createMock(Client::class);
$mock_client->method('sendRequest')
->willReturnOnConsecutiveCalls(
$mock_200_response,
$mock_204_response,
$mock_200_response,
$mock_204_response,
$mock_200_response,
$mock_204_response,
$mock_200_response,
$mock_204_response,
$mock_200_response,
$mock_204_response
);
$c->setHttpClient($mock_client);
$c->setGithubAuth('test', 'pass');
$c->run();
$output = $c->getOutput();
Expand Down
30 changes: 0 additions & 30 deletions test/unit/CosyComposerUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,6 @@ public function testLastStdOut()
$this->assertEquals('output', $c->getLastStdOut());
}

public function testCreateTempTokenNoProject()
{
$c = $this->getMockCosy();
$c->setProject(null);
$this->expectExceptionMessage('No project data was found, so no temp token can be generated.');
$c->createTempToken();
}

public function testCreateTempTokenNoTokenUrl()
{
$c = $this->getMockCosy();
$c->setTokenUrl(null);
$this->expectExceptionMessage('No token URL specified for project');
$c->createTempToken();
}

public function testCreateTempTokenBadResponse()
{
$c = $this->getMockCosy();
$mock_418_response = new Response(418, [], '{"token":123}');
$mock_client = $this->createMock(Client::class);
$mock_client->method('sendRequest')
->willReturnOnConsecutiveCalls(
$mock_418_response
);
$c->setHttpClient($mock_client);
$this->expectExceptionMessage('Wrong status code on temp token request (418).');
$c->createTempToken();
}

/**
* @dataProvider getComposerJsonVariations
*/
Expand Down
62 changes: 33 additions & 29 deletions test/unit/Providers/GithubProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,15 @@
use Github\Client;
use Psr\Http\Message\ResponseInterface;

class GithubProviderTest extends \PHPUnit_Framework_TestCase
class GithubProviderTest extends ProvidersTestBase
{
public function testAuthenticate()
{
$mock_client = $this->getMockClient();
$user = 'testUser';
$password = 'testPassword';
$mock_client->expects($this->once())
->method('authenticate')
->with($user, null, Client::AUTH_URL_TOKEN);
$g = new Github($mock_client);
$g->authenticate($user, $password);
}
protected $authenticateArguments = [
'testUser', null, Client::AUTH_URL_TOKEN,
];

public function testAuthenticatePrivate()
{
$mock_client = $this->getMockClient();
$user = 'testUser';
$password = 'testPassword';
$mock_client->expects($this->once())
->method('authenticate')
->with($user, null, Client::AUTH_HTTP_TOKEN);
$g = new Github($mock_client);
$g->authenticatePrivate($user, $password);
}
protected $authenticatePrivateArguments = [
'testUser', null, Client::AUTH_HTTP_TOKEN
];

public function testRepoIsPrivate()
{
Expand Down Expand Up @@ -218,11 +202,7 @@ public function testCreateFork()

public function testCreatePR()
{
$user = 'testUser';
$repo = 'testRepo';
$params = [
'param1' => true,
];
list($user, $repo, $params) = $this->getPrData();
$testresponse = 'testresponse';
$mock_pr_api = $this->createMock(PullRequest::class);
$mock_pr_api->expects($this->once())
Expand All @@ -238,8 +218,32 @@ public function testCreatePR()
$this->assertEquals($testresponse, $g->createPullRequest($user, $repo, $params));
}

public function testUpdatePR()
{
list($user, $repo, $params) = $this->getPrData();
$id = 42;
$testresponse = 'testresponse';
$mock_pr_api = $this->createMock(PullRequest::class);
$mock_pr_api->expects($this->once())
->method('update')
->with($user, $repo, $id, $params)
->willReturn($testresponse);
$mock_client = $this->getMockClient();
$mock_client->expects($this->once())
->method('api')
->with('pull_request')
->willReturn($mock_pr_api);
$g = new Github($mock_client);
$this->assertEquals($testresponse, $g->updatePullRequest($user, $repo, $id, $params));
}

protected function getProvider(Client $client)
{
return new Github($client);
}


private function getMockClient()
protected function getMockClient()
{
return $this->createMock(Client::class);
}
Expand Down
36 changes: 36 additions & 0 deletions test/unit/Providers/GitlabProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace eiriksm\CosyComposerTest\unit\Providers;

use eiriksm\CosyComposer\Providers\Gitlab;
use Gitlab\Client;

class GitlabProviderTest extends ProvidersTestBase
{
protected $authenticateArguments = [
'testUser',
Client::AUTH_OAUTH_TOKEN
];

protected $authenticatePrivateArguments = [
'testUser',
Client::AUTH_OAUTH_TOKEN
];

public function testRepoIsPrivate()
{
$client = $this->getMockClient();
$provider = $this->getProvider($client);
$this->assertEquals(true, $provider->repoIsPrivate('testUser', 'testRepo'));
}

protected function getProvider(Client $client)
{
return new Gitlab($client);
}

protected function getMockClient()
{
return $this->createMock(Client::class);
}
}
68 changes: 68 additions & 0 deletions test/unit/Providers/ProvidersTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace eiriksm\CosyComposerTest\unit\Providers;

use eiriksm\CosyComposer\ProviderInterface;

abstract class ProvidersTestBase extends \PHPUnit_Framework_TestCase
{

protected $authenticateArguments = [];

public function testAuthenticate()
{
$client = $this->getMockClient();
$expect = $client->expects($this->once())
->method('authenticate');
$this->configureArguments('authenticateArguments', $expect);
$provider = $this->getProvider($client);
$this->runAuthenticate($provider);
}

public function testAuthenticatePrivate()
{
$mock_client = $this->getMockClient();
$expect = $mock_client->expects($this->once())
->method('authenticate');
$this->configureArguments('authenticatePrivateArguments', $expect);
$provider = $this->getProvider($mock_client);
$this->runAuthenticate($provider, 'authenticatePrivate');
}

protected function configureArguments($key, \PHPUnit_Framework_MockObject_Builder_InvocationMocker $object)
{
$arguments = $this->{$key};
switch (count($arguments)) {
case 2:
list($one, $two) = $arguments;
$object->with($one, $two);
break;

case 3:
list($one, $two, $three) = $arguments;
$object->with($one, $two, $three);
break;

default:
throw new \Exception('Auth arguments not configured');
}
}

protected function runAuthenticate(ProviderInterface $provider, $method = 'authenticate')
{
$user = 'testUser';
$password = 'testPassword';
$provider->{$method}($user, $password);
}

protected function getPrData()
{
return [
'testUser',
'testRepo',
[
'param1' => true,
],
];
}
}

0 comments on commit 157ecce

Please sign in to comment.