Skip to content

Commit

Permalink
Now able to dynamically pass a config
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyWendt committed Jan 13, 2016
1 parent 626e023 commit f238687
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 27 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ class ProviderNameExtendSocialite
## Overriding a Built-in Provider

You can easily override a built-in `laravel/socialite` provider by creating a new one with exactly the same name (i.e. 'facebook').


## Dynamically Passing a Config

You can dynamically pass a config by using:
```
$key = 'SocialiteProviders.config.<provider_name>';
$config = new \SocialiteProviders\Manager\Config('key', 'secret', 'callbackUri');
$this->app->instance($key, $config)
```

**You must call this before you run any Socialite methods.**
12 changes: 6 additions & 6 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Config implements Contracts\Config

public function __construct($key, $secret, $callbackUri)
{
$this->config = [
'client_id' => $key,
'client_secret' => $secret,
'redirect' => $callbackUri
];
$this->config = [
'client_id' => $key,
'client_secret' => $secret,
'redirect' => $callbackUri,
];
}

/**
* @return array
*/
Expand Down
31 changes: 22 additions & 9 deletions src/SocialiteWasCalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Foundation\Application as LaravelApp;
use Laravel\Socialite\SocialiteManager;
use SocialiteProviders\Manager\Contracts;

class SocialiteWasCalled
{
Expand All @@ -23,15 +24,16 @@ public function __construct(LaravelApp $app)
/**
* @param string $providerName 'meetup'
* @param string $providerClass 'Your\Name\Space\ClassNameProvider' must extend
* either Laravel\Socialite\Two\AbstractProvider or Laravel\Socialite\One\AbstractProvider
* either Laravel\Socialite\Two\AbstractProvider or
* Laravel\Socialite\One\AbstractProvider
* @param string $oauth1Server 'Your\Name\Space\ClassNameServer' must extend League\OAuth1\Client\Server\Server
*
* @throws InvalidArgumentException
*/
public function extendSocialite($providerName, $providerClass, $oauth1Server = null)
{
/** @var SocialiteManager $socialite */
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class);
$provider = $this->buildProvider($socialite, $providerName, $providerClass, $oauth1Server);
$socialite->extend(
$providerName,
Expand All @@ -43,9 +45,9 @@ function () use ($provider) {

/**
* @param SocialiteManager $socialite
* @param $providerName
* @param string $providerClass
* @param null|string $oauth1Server
* @param $providerName
* @param string $providerClass
* @param null|string $oauth1Server
*
* @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider
*/
Expand All @@ -70,8 +72,8 @@ protected function buildProvider(SocialiteManager $socialite, $providerName, $pr
*/
protected function buildOAuth1Provider($providerClass, $oauth1Server, array $config)
{
$this->classExtends($providerClass, 'Laravel\Socialite\One\AbstractProvider');
$this->classExtends($oauth1Server, 'League\OAuth1\Client\Server\Server');
$this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class);
$this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class);

return new $providerClass(
$this->app->offsetGet('request'), new $oauth1Server($config)
Expand All @@ -89,7 +91,7 @@ protected function buildOAuth1Provider($providerClass, $oauth1Server, array $con
*/
protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, array $config)
{
$this->classExtends($providerClass, 'Laravel\Socialite\Two\AbstractProvider');
$this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class);

return $socialite->buildProvider($providerClass, $config);
}
Expand All @@ -101,7 +103,18 @@ protected function buildOAuth2Provider(SocialiteManager $socialite, $providerCla
*/
protected function getConfig($providerName)
{
return $this->app->offsetGet('config')['services.'.$providerName];
try {
/** @var Contracts\Config $config */
$config = $this->app->make('SocialiteProviders.config.'.$providerName);

if (!($config instanceof Contracts\Config)) {
throw new InvalidArgumentException('Config class does not implement config contract');
}

return $config->get();
} catch (\ReflectionException $e) {
return $this->app->offsetGet('config')['services.'.$providerName];
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function it_returns_a_config_array()
$secret = 'secret';
$callbackUri = 'uri';

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

$config = new Config($key, $secret, $callbackUri);
Expand Down
14 changes: 7 additions & 7 deletions tests/ManagerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ protected function expectManagerInvalidArgumentException()
protected function config()
{
return [
'client_id' => 'test',
'client_id' => 'test',
'client_secret' => 'test',
'redirect' => 'test',
'redirect' => 'test',
];
}

Expand All @@ -31,8 +31,8 @@ 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'],
];
}
Expand All @@ -42,15 +42,15 @@ protected function oauth1FormattedConfig(array $config)
*/
protected function appMock()
{
return m::mock('Illuminate\Contracts\Foundation\Application');
return m::mock(\Illuminate\Contracts\Foundation\Application::class);
}

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

protected function oauth2ProviderStub()
Expand Down Expand Up @@ -145,7 +145,7 @@ protected function providerConfigKey($providerName)
*/
protected function buildRequest()
{
return m::mock('\Illuminate\Http\Request');
return m::mock(\Illuminate\Http\Request::class);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/OAuth1ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ function ($closure) {
]
);

$config = new Config(
$this->config()['client_id'],
$this->config()['client_secret'],
$this->config()['redirect']
);

$app = $this->appMock();
$app->shouldReceive('make')->andReturn($socialite);
$app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite);
$app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn($config);
$app->shouldReceive('offsetGet')->with('request')->andReturn($this->buildRequest());
$app->shouldReceive('offsetGet')->with('config')->andReturn($this->servicesArray($providerName));

Expand Down
41 changes: 40 additions & 1 deletion tests/OAuth2ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ public function it_passes()
$this->assertTrue(true);
}

/**
* @test
* @expectedException \SocialiteProviders\Manager\InvalidArgumentException
*/
public function it_should_blow_up_if_the_config_passed_does_not_implement_config_contract()
{
$providerName = 'bar';

$socialite = $this->socialiteMock();
$socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()])
->andReturn($this->oauth2ProviderStub());
$socialite->shouldReceive('extend')->withArgs(
[
$providerName,
m::on(
function ($closure) {
$this->assertInstanceOf($this->oauth2ProviderStubName(), $closure());

return is_callable($closure);
}
),
]
);

$app = $this->appMock();
$app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite);
$app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn('foobar');

$s = new SocialiteWasCalled($app);
$s->extendSocialite($providerName, $this->oauth2ProviderStubName());
}

/**
* @test
*/
Expand All @@ -44,8 +76,15 @@ function ($closure) {
]
);

$config = new Config(
$this->config()['client_id'],
$this->config()['client_secret'],
$this->config()['redirect']
);

$app = $this->appMock();
$app->shouldReceive('make')->andReturn($socialite);
$app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite);
$app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn($config);
$app->shouldReceive('offsetGet')->andReturn($this->servicesArray($providerName));

$s = new SocialiteWasCalled($app);
Expand Down

0 comments on commit f238687

Please sign in to comment.