Skip to content

Commit

Permalink
Remove old configuration files and use services configuration file an…
Browse files Browse the repository at this point in the history
…d rewrite for Laravel 5
  • Loading branch information
jenssegers committed Feb 14, 2015
1 parent b5e77af commit 325e8c9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 153 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -15,11 +15,11 @@
],
"require": {
"php": ">=5.4",
"illuminate/support": "4.*|5.0.*",
"illuminate/support": "4.*|5.*",
"lusitanian/oauth": "~0.3"
},
"require-dev": {
"orchestra/testbench": "2.2.*",
"orchestra/testbench": "3.0.*",
"mockery/mockery": "*",
"satooshi/php-coveralls": "*"
},
Expand Down
49 changes: 23 additions & 26 deletions src/OAuth.php
Expand Up @@ -20,41 +20,21 @@ class OAuth {
*/
protected $storage;

/**
* The prefix used to get the configuration.
*
* @var string
*/
protected $prefix = 'services';

/**
* Constructor.
*
* @param \OAuth\ServiceFactory $factory
*/
public function __construct(ServiceFactory $factory, TokenStorageInterface $storage)
public function __construct(ServiceFactory $factory, TokenStorageInterface $storage, $httpClient = null)
{
// Dependency injection
$this->factory = $factory;
$this->storage = $storage;

// Here we check what consumer configuration file is used before we start.
// By default, we will use the Laravel services file, but we will check
// other files such as the included package configuration file.
if (Config::get('oauth::consumers'))
{
$this->prefix = 'oauth::consumers';
}
else if (Config::get('oauth.consumers'))
{
$this->prefix = 'oauth.consumers';
}

// Set HTTP client
if ($client = Config::get('oauth.client') ?: Config::get('oauth::client'))
if ($httpClient)
{
$class = '\OAuth\Common\Http\Client\\' . $client;
$this->factory->setHttpClient(new $class);
$this->setHttpClient($httpClient);
}
}

Expand All @@ -70,15 +50,15 @@ public function consumer($service, $url = null, $scope = null)
{
// Create credentials object.
$credentials = new Credentials(
Config::get($this->prefix . ".$service.client_id"),
Config::get($this->prefix . ".$service.client_secret"),
Config::get("services.$service.client_id"),
Config::get("services.$service.client_secret"),
$url ?: URL::current()
);

// Get default scope.
if (is_null($scope))
{
$scope = Config::get($this->prefix . ".$service.scope", array());
$scope = Config::get("services.$service.scope", []);
}

return $this->factory->createService($service, $credentials, $this->storage, $scope);
Expand All @@ -97,6 +77,23 @@ public function service($service, $url = null, $scope = null)
return $this->consumer($service, $url, $scope);
}

/**
* Set the HTTP client.
*
* @param string $client
*/
public function setHttpClient($client)
{
if ( ! in_array($client, ['StreamClient', 'CurlClient']))
{
throw new \InvalidArgumentException('Invalid HTTP client');
}

$class = '\OAuth\Common\Http\Client\\' . $client;

$this->factory->setHttpClient(new $class);
}

/**
* Handle dynamic method calls.
*
Expand Down
3 changes: 1 addition & 2 deletions src/OAuthServiceProvider.php
Expand Up @@ -13,8 +13,7 @@ class OAuthServiceProvider extends ServiceProvider {
*/
public function boot()
{
// Fix for PSR-4
$this->package('jenssegers/oauth', 'oauth', realpath(__DIR__));
//
}

/**
Expand Down
36 changes: 0 additions & 36 deletions src/config/config.php

This file was deleted.

106 changes: 21 additions & 85 deletions tests/OAuthTest.php
Expand Up @@ -8,60 +8,51 @@ class OAuthProviderTest extends Orchestra\Testbench\TestCase {
public function tearDown()
{
Mockery::close();
parent::tearDown();
}

protected function getPackageProviders()
protected function getPackageProviders($app)
{
return array('Jenssegers\OAuth\OAuthServiceProvider');
}

protected function getPackageAliases()
protected function getPackageAliases($app)
{
return array(
'OAuth' => 'Jenssegers\OAuth\Facades\OAuth'
);
}

public function testDefaultConfiguration()
{
$this->assertNotNull(Config::get('oauth::client'));
$this->assertNotNull(Config::get('oauth::consumers'));
}

public function testSetsHttpClient()
{
Config::set('oauth::client', 'CurlClient');

$serviceFactory = Mockery::mock('OAuth\ServiceFactory');
$serviceFactory->shouldReceive('setHttpClient')->times(1);
$serviceFactory->shouldReceive('setHttpClient')->with(Mockery::type('OAuth\\Common\\Http\\Client\\CurlClient'))->times(1);

$oauth = new OAuth($serviceFactory, new Memory);
$oauth->setHttpClient('CurlClient');
}

public function testMagicCalls()
public function testInvalidHttpClient()
{
Config::set('oauth::client', '');
$this->setExpectedException('InvalidArgumentException', 'Invalid HTTP client');

$client = new \OAuth\Common\Http\Client\CurlClient;
$serviceFactory = Mockery::mock('OAuth\ServiceFactory');
$serviceFactory->shouldReceive('setHttpClient')->with($client)->times(1);

$oauth = new OAuth($serviceFactory, new Memory);
$oauth->setHttpClient($client);
$oauth->setHttpClient('FooBarClient');
}

public function testDefaultHttpClient()
{
Config::set('oauth::client', '');

$serviceFactory = Mockery::mock('OAuth\ServiceFactory');
$serviceFactory->shouldReceive('setHttpClient')->times(0);

$oauth = new OAuth($serviceFactory, new Memory);
}

public function testCreatesConsumer()
{
Config::set('oauth::consumers.facebook.client_id', '123');
Config::set('oauth::consumers.facebook.client_secret', 'ABC');
Config::set('services.facebook.client_id', '123');
Config::set('services.facebook.client_secret', 'ABC');

$serviceFactory = Mockery::mock('OAuth\ServiceFactory[createService]');
$serviceFactory->shouldReceive('createService')->passthru();
Expand All @@ -71,26 +62,16 @@ public function testCreatesConsumer()
$this->assertInstanceOf('OAuth\OAuth2\Service\Facebook', $consumer);
}

public function testServiceAlias()
public function testReturnsConsumer()
{
Config::set('oauth::consumers.facebook.client_id', '123');
Config::set('oauth::consumers.facebook.client_secret', 'ABC');
Config::set('services.facebook.client_id', '123');
Config::set('services.facebook.client_secret', 'ABC');
Config::set('services.facebook.scope', array());

$serviceFactory = Mockery::mock('OAuth\ServiceFactory[createService]');
$serviceFactory = Mockery::mock('OAuth\ServiceFactory');
$serviceFactory->shouldReceive('createService')->passthru();

$oauth = new OAuth($serviceFactory, new Memory);
$consumer = $oauth->service('facebook');
$this->assertInstanceOf('OAuth\OAuth2\Service\Facebook', $consumer);
}

public function testReturnsConsumer()
{
Config::set('oauth::consumers.facebook.client_id', '123');
Config::set('oauth::consumers.facebook.client_secret', 'ABC');
Config::set('oauth::consumers.facebook.scope', array());

$oauth = App::make('oauth');
$consumer = $oauth->consumer('facebook', 'foo.bar.com', array('email', 'publish_actions'));
$this->assertInstanceOf('OAuth\OAuth2\Service\Facebook', $consumer);

Expand All @@ -102,43 +83,14 @@ public function testReturnsConsumer()

public function testReturnsDefaultConsumer()
{
Config::set('oauth::consumers.facebook.client_id', '123');
Config::set('oauth::consumers.facebook.client_secret', 'ABC');
Config::set('oauth::consumers.facebook.scope', array('email', 'publish_actions'));

$oauth = App::make('oauth');
$consumer = $oauth->consumer('facebook');
$this->assertInstanceOf('OAuth\OAuth2\Service\Facebook', $consumer);

$uri = (string) $consumer->getAuthorizationUri();
$this->assertContains('client_id=123', $uri);
$this->assertContains('redirect_uri=' . urlencode(URL::current()), $uri);
$this->assertContains('scope=email+publish_actions', $uri);
}

public function testSharesLaravelSession()
{
$oauth = App::make('oauth');
$consumer = $oauth->consumer('facebook');
$storage = $consumer->getStorage();
$session = $storage->getSession();

$session->set('foo', 'bar');
$this->assertEquals('bar', Session::get('foo'));
}

public function testCustomConfigurationFile()
{
Config::set('oauth.client', 'CurlClient');
Config::set('oauth.consumers.facebook.client_id', '123');
Config::set('oauth.consumers.facebook.client_secret', 'ABC');
Config::set('oauth.consumers.facebook.scope', array('email', 'publish_actions'));
Config::set('services.facebook.client_id', '123');
Config::set('services.facebook.client_secret', 'ABC');
Config::set('services.facebook.scope', array('email', 'publish_actions'));

$serviceFactory = Mockery::mock('OAuth\ServiceFactory');
$serviceFactory->shouldReceive('setHttpClient')->times(1);
$serviceFactory->shouldReceive('createService')->passthru();
$oauth = new OAuth($serviceFactory, new Memory);

$oauth = App::make('oauth');
$consumer = $oauth->consumer('facebook');
$this->assertInstanceOf('OAuth\OAuth2\Service\Facebook', $consumer);

Expand All @@ -148,20 +100,4 @@ public function testCustomConfigurationFile()
$this->assertContains('scope=email+publish_actions', $uri);
}

public function testServicesConfiguration()
{
Config::set('services.facebook.client_id', '789');
Config::set('services.facebook.client_secret', 'XYZ');
Config::set('services.facebook.scope', array('email', 'publish_actions'));

$oauth = App::make('oauth');
$consumer = $oauth->consumer('facebook');
$this->assertInstanceOf('OAuth\OAuth2\Service\Facebook', $consumer);

$uri = (string) $consumer->getAuthorizationUri();
$this->assertContains('client_id=789', $uri);
$this->assertContains('redirect_uri=' . urlencode(URL::current()), $uri);
$this->assertContains('scope=email+publish_actions', $uri);
}

}
16 changes: 14 additions & 2 deletions tests/ServiceProviderTest.php
Expand Up @@ -5,14 +5,15 @@ class ServiceProviderTest extends Orchestra\Testbench\TestCase {
public function tearDown()
{
Mockery::close();
parent::tearDown();
}

protected function getPackageProviders()
protected function getPackageProviders($app)
{
return array('Jenssegers\OAuth\OAuthServiceProvider');
}

protected function getPackageAliases()
protected function getPackageAliases($app)
{
return array(
'OAuth' => 'Jenssegers\OAuth\Facades\OAuth'
Expand All @@ -29,4 +30,15 @@ public function testFacade()
$this->assertInstanceOf('Jenssegers\OAuth\OAuth', OAuth::getFacadeRoot());
}

public function testSharesLaravelSession()
{
$oauth = App::make('oauth');
$consumer = $oauth->consumer('facebook');
$storage = $consumer->getStorage();
$session = $storage->getSession();

$session->set('foo', 'bar');
$this->assertEquals('bar', Session::get('foo'));
}

}

0 comments on commit 325e8c9

Please sign in to comment.