Skip to content

Commit

Permalink
Tests initial refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev authored and m1guelpf committed Jan 16, 2019
1 parent f870574 commit ec23078
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 282 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"authors": [{
"name": "Andy Wendt",
"email": "[email protected]"
}, {
"name": "Anton Komarev",
"email": "[email protected]"
}],
"require": {
"php": "^5.6 || ^7.0",
Expand Down
47 changes: 31 additions & 16 deletions tests/ConfigRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SocialiteProviders\Manager\Test;

use Mockery as m;
use SocialiteProviders\Manager\Exception\MissingConfigException;
use SocialiteProviders\Manager\Helpers\ConfigRetriever;

class ConfigRetrieverTest extends \PHPUnit_Framework_TestCase
Expand All @@ -10,27 +12,37 @@ class ConfigRetrieverTest extends \PHPUnit_Framework_TestCase

/**
* @test
* @expectedException \SocialiteProviders\Manager\Exception\MissingConfigException
*/
public function it_throws_if_there_is_a_problem_with_the_services_config()
{
$providerName = 'test';
$this->expectException(MissingConfigException::class);

self::$functions->shouldReceive('config')->with("services.$providerName")->once()->andReturn(null);
$providerName = 'test';
self::$functions
->shouldReceive('config')
->with("services.{$providerName}")
->once()
->andReturn(null);
$configRetriever = new ConfigRetriever();

$configRetriever->fromServices($providerName)->get();
}

/**
* @test
* @expectedException \SocialiteProviders\Manager\Exception\MissingConfigException
*/
public function it_throws_if_there_are_missing_items_in_the_services_config()
{
$providerName = 'test';
$this->expectException(MissingConfigException::class);

self::$functions->shouldReceive('config')->with("services.$providerName")->once()->andReturn([]);
$providerName = 'test';
self::$functions
->shouldReceive('config')
->with("services.{$providerName}")
->once()
->andReturn([]);
$configRetriever = new ConfigRetriever();

$configRetriever->fromServices($providerName)->get();
}

Expand All @@ -44,22 +56,25 @@ public function it_retrieves_a_config_from_the_services()
$secret = 'secret';
$uri = 'uri';
$additionalConfigItem = 'test';

$config = [
'client_id' => $key,
'client_id' => $key,
'client_secret' => $secret,
'redirect' => $uri,
'additional' => $additionalConfigItem,
'redirect' => $uri,
'additional' => $additionalConfigItem,
];

self::$functions->shouldReceive('config')->with("services.$providerName")->once()->andReturn($config);
self::$functions
->shouldReceive('config')
->with("services.{$providerName}")
->once()
->andReturn($config);
$configRetriever = new ConfigRetriever();

$result = $configRetriever->fromServices($providerName, ['additional'])->get();

$this->assertEquals($key, $result['client_id']);
$this->assertEquals($secret, $result['client_secret']);
$this->assertEquals($uri, $result['redirect']);
$this->assertEquals($additionalConfigItem, $result['additional']);
$this->assertSame($key, $result['client_id']);
$this->assertSame($secret, $result['client_secret']);
$this->assertSame($uri, $result['redirect']);
$this->assertSame($additionalConfigItem, $result['additional']);
}
}

Expand Down
13 changes: 5 additions & 8 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public function it_returns_a_config_array()
$key = 'key';
$secret = 'secret';
$callbackUri = 'uri';

$result = [
'client_id' => $key,
'client_id' => $key,
'client_secret' => $secret,
'redirect' => $callbackUri,
'redirect' => $callbackUri,
];

$config = new Config($key, $secret, $callbackUri);
Expand All @@ -32,14 +31,12 @@ public function it_allows_additional_config_items()
$key = 'key';
$secret = 'secret';
$callbackUri = 'uri';

$result = [
'client_id' => $key,
'client_id' => $key,
'client_secret' => $secret,
'redirect' => $callbackUri,
'additional' => true,
'redirect' => $callbackUri,
'additional' => true,
];

$additional = ['additional' => true];

$config = new Config($key, $secret, $callbackUri, $additional);
Expand Down
109 changes: 44 additions & 65 deletions tests/ManagerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace SocialiteProviders\Manager\Test;

use Illuminate\Contracts\Container\Container as ContainerContract;
use Illuminate\Http\Request as HttpRequest;
use Laravel\Socialite\SocialiteManager;
use Mockery as m;
use SocialiteProviders\Manager\Config;
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
Expand All @@ -21,32 +24,62 @@ public function tearDown()
}

/**
* @return m\MockInterface
* @return \Mockery\MockInterface|\SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface
*/
protected function configRetrieverMock()
{
return m::mock(ConfigRetrieverInterface::class);
}

protected function expectManagerInvalidArgumentException()
/**
* @return \Mockery\MockInterface|\Illuminate\Contracts\Container\Container
*/
protected function appMock()
{
return m::mock(ContainerContract::class);
}

/**
* @return \Mockery\MockInterface|\Laravel\Socialite\SocialiteManager
*/
protected function socialiteMock()
{
return m::mock(SocialiteManager::class);
}

/**
* @return \Mockery\MockInterface|\Illuminate\Http\Request
*/
protected function buildRequest()
{
$this->setExpectedException(\SocialiteProviders\Manager\Exception\InvalidArgumentException::class);
return m::mock(HttpRequest::class);
}

protected function configObject()
{
return new Config('test', 'test', 'test');
}

protected function configRetrieverMockWithDefaultExpectations($providerName, $providerClass)
{
$configRetriever = $this->configRetrieverMock();
$configRetriever
->shouldReceive('fromServices')
->with($providerName, $providerClass::additionalConfigKeys())
->andReturn($this->configObject());

return $configRetriever;
}

/**
* @return array
*/
protected function config()
{
return [
'client_id' => 'test',
'client_id' => 'test',
'client_secret' => 'test',
'redirect' => 'test',
'redirect' => 'test',
];
}

Expand All @@ -58,28 +91,12 @@ protected function config()
protected function oauth1FormattedConfig(array $config)
{
return [
'identifier' => $config['client_id'],
'secret' => $config['client_secret'],
'identifier' => $config['client_id'],
'secret' => $config['client_secret'],
'callback_uri' => $config['redirect'],
];
}

/**
* @return \Mockery\MockInterface
*/
protected function appMock()
{
return m::mock(\Illuminate\Container\Container::class);
}

/**
* @return \Mockery\MockInterface
*/
protected function socialiteMock()
{
return m::mock(\Laravel\Socialite\SocialiteManager::class);
}

protected function oauth2ProviderStub()
{
static $provider = null;
Expand All @@ -102,25 +119,25 @@ protected function oauth1ProviderStub()
return $provider;
}

protected function oauth1ProviderStubName()
protected function oauth1ProviderStubClass()
{
return $this->fullStubClassName('OAuth1ProviderStub');
}

protected function oauth1ServerStubName()
protected function oauth1ServerStubClass()
{
return $this->fullStubClassName('OAuth1ServerStub');
}

protected function oauth2ProviderStubName()
protected function oauth2ProviderStubClass()
{
return $this->fullStubClassName('OAuth2ProviderStub');
}

/**
* @param string $stub
*
* @return m\MockInterface
* @return \Mockery\MockInterface
*/
protected function mockStub($stub)
{
Expand All @@ -137,44 +154,6 @@ protected function fullStubClassName($stub)
return __NAMESPACE__.'\Stubs\\'.$stub;
}

/**
* @param string $class
*
* @return string
*/
protected function fullClassName($class)
{
return __NAMESPACE__.'\\'.$class;
}

/**
* @param string $providerName
*
* @return array
*/
protected function servicesArray($providerName)
{
return [$this->providerConfigKey($providerName) => $this->config()];
}

/**
* @param string $providerName
*
* @return string
*/
protected function providerConfigKey($providerName)
{
return 'services.'.$providerName;
}

/**
* @return m\MockInterface
*/
protected function buildRequest()
{
return m::mock(\Illuminate\Http\Request::class);
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit ec23078

Please sign in to comment.