From 325e8c91c7eb3516229da236f17890c91da88b50 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sat, 14 Feb 2015 10:41:09 +0100 Subject: [PATCH] Remove old configuration files and use services configuration file and rewrite for Laravel 5 --- composer.json | 4 +- src/OAuth.php | 49 ++++++++-------- src/OAuthServiceProvider.php | 3 +- src/config/config.php | 36 ------------ tests/OAuthTest.php | 106 +++++++--------------------------- tests/ServiceProviderTest.php | 16 ++++- 6 files changed, 61 insertions(+), 153 deletions(-) delete mode 100644 src/config/config.php diff --git a/composer.json b/composer.json index 3c072dd..b7831df 100644 --- a/composer.json +++ b/composer.json @@ -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": "*" }, diff --git a/src/OAuth.php b/src/OAuth.php index 4c177be..090bc1c 100644 --- a/src/OAuth.php +++ b/src/OAuth.php @@ -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); } } @@ -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); @@ -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. * diff --git a/src/OAuthServiceProvider.php b/src/OAuthServiceProvider.php index 88b45f8..231cbbe 100644 --- a/src/OAuthServiceProvider.php +++ b/src/OAuthServiceProvider.php @@ -13,8 +13,7 @@ class OAuthServiceProvider extends ServiceProvider { */ public function boot() { - // Fix for PSR-4 - $this->package('jenssegers/oauth', 'oauth', realpath(__DIR__)); + // } /** diff --git a/src/config/config.php b/src/config/config.php deleted file mode 100644 index 537ad6b..0000000 --- a/src/config/config.php +++ /dev/null @@ -1,36 +0,0 @@ - 'StreamClient', - - - /* - |-------------------------------------------------------------------------- - | OAuth Consumers - |-------------------------------------------------------------------------- - | - | Example: - | - | 'facebook' => array( - | 'client_id' => 'your-client-id', - | 'client_secret' => 'your-client-secret', - | 'scope' => array(), - | ) - | - */ - - 'consumers' => array( - - ) - -); diff --git a/tests/OAuthTest.php b/tests/OAuthTest.php index 4cc4b29..54888f3 100644 --- a/tests/OAuthTest.php +++ b/tests/OAuthTest.php @@ -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(); @@ -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); @@ -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); @@ -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); - } - } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 9e57ba8..ac5ee34 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -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' @@ -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')); + } + }