From ec23078b10f487b2451cfeaea843ee085fee9ed4 Mon Sep 17 00:00:00 2001 From: a-komarev Date: Tue, 15 Jan 2019 18:31:19 +0300 Subject: [PATCH] Tests initial refactoring --- composer.json | 3 + tests/ConfigRetrieverTest.php | 47 ++++-- tests/ConfigTest.php | 13 +- tests/ManagerTestTrait.php | 109 +++++------- tests/OAuth1ProviderTest.php | 95 +++++++---- tests/OAuth2ProviderTest.php | 278 +++++++++++++++++-------------- tests/OAuthTwoTest.php | 153 +++++++++++++---- tests/ServiceProviderTest.php | 19 ++- tests/Stubs/OAuth1ServerStub.php | 2 +- 9 files changed, 437 insertions(+), 282 deletions(-) diff --git a/composer.json b/composer.json index ac91abd..1705e52 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,9 @@ "authors": [{ "name": "Andy Wendt", "email": "andy@awendt.com" + }, { + "name": "Anton Komarev", + "email": "a.komarev@cybercog.su" }], "require": { "php": "^5.6 || ^7.0", diff --git a/tests/ConfigRetrieverTest.php b/tests/ConfigRetrieverTest.php index 465d0a8..9c34b4d 100644 --- a/tests/ConfigRetrieverTest.php +++ b/tests/ConfigRetrieverTest.php @@ -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 @@ -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(); } @@ -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']); } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index a665a9e..c6facb8 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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); @@ -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); diff --git a/tests/ManagerTestTrait.php b/tests/ManagerTestTrait.php index 6c65604..38763e7 100644 --- a/tests/ManagerTestTrait.php +++ b/tests/ManagerTestTrait.php @@ -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; @@ -21,16 +24,35 @@ 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() @@ -38,15 +60,26 @@ 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', ]; } @@ -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; @@ -102,17 +119,17 @@ 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'); } @@ -120,7 +137,7 @@ protected function oauth2ProviderStubName() /** * @param string $stub * - * @return m\MockInterface + * @return \Mockery\MockInterface */ protected function mockStub($stub) { @@ -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 */ diff --git a/tests/OAuth1ProviderTest.php b/tests/OAuth1ProviderTest.php index 0a202ae..8aeea6f 100644 --- a/tests/OAuth1ProviderTest.php +++ b/tests/OAuth1ProviderTest.php @@ -2,7 +2,9 @@ namespace SocialiteProviders\Manager\Test; +use Laravel\Socialite\Contracts\Factory as SocialiteFactoryContract; use Mockery as m; +use SocialiteProviders\Manager\Exception\InvalidArgumentException; use SocialiteProviders\Manager\SocialiteWasCalled; class OAuth1ProviderTest extends \PHPUnit_Framework_TestCase @@ -15,29 +17,42 @@ class OAuth1ProviderTest extends \PHPUnit_Framework_TestCase public function it_should_build_a_provider_and_extend_socialite() { $providerName = 'bar'; - + $providerClass = $this->oauth1ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('formatConfig')->with($this->config()) + $socialite + ->shouldReceive('formatConfig') + ->with($this->config()) ->andReturn($this->oauth1FormattedConfig($this->config())); - $socialite->shouldReceive('extend')->withArgs( - [ + $socialite + ->shouldReceive('extend') + ->withArgs([ $providerName, - m::on( - function ($closure) { - $this->assertInstanceOf($this->oauth1ProviderStubName(), $closure()); - - return is_callable($closure); - } - ), - ] - ); + m::on(function ($closure) use ($providerClass) { + $this->assertInstanceOf($providerClass, $closure()); + return is_callable($closure); + }), + ]); $app = $this->appMock(); - $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); - $app->shouldReceive('offsetGet')->with('request')->andReturn($this->buildRequest()); + $app + ->shouldReceive('make') + ->with(SocialiteFactoryContract::class) + ->andReturn($socialite); + $app + ->shouldReceive('offsetGet') + ->with('request') + ->andReturn($this->buildRequest()); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth1ProviderStubName())); - $s->extendSocialite($providerName, $this->oauth1ProviderStubName(), $this->oauth1ServerStubName()); + $event->extendSocialite( + $providerName, + $providerClass, + $this->oauth1ServerStubClass() + ); } /** @@ -45,19 +60,30 @@ function ($closure) { */ public function it_throws_if_given_an_invalid_oauth1_provider() { - $this->expectManagerInvalidArgumentException(); + $this->expectException(InvalidArgumentException::class); $providerName = 'foo'; - + $providerClass = $this->oauth1ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('formatConfig')->with($this->config()) + $socialite + ->shouldReceive('formatConfig') + ->with($this->config()) ->andReturn($this->oauth1FormattedConfig($this->config())); - $app = $this->appMock(); - $app->shouldReceive('make')->andReturn($socialite); + $app + ->shouldReceive('make') + ->andReturn($socialite); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth1ProviderStubName(), $providerName)); - $s->extendSocialite($providerName, $this->invalidClass(), $this->oauth1ServerStubName()); + $event->extendSocialite( + $providerName, + $this->invalidClass(), + $this->oauth1ServerStubClass() + ); } /** @@ -65,16 +91,25 @@ public function it_throws_if_given_an_invalid_oauth1_provider() */ public function it_throws_if_given_an_invalid_oauth1_server() { - $this->expectManagerInvalidArgumentException(); + $this->expectException(InvalidArgumentException::class); $providerName = 'bar'; - + $providerClass = $this->oauth1ProviderStubClass(); $socialite = $this->socialiteMock(); - $app = $this->appMock(); - $app->shouldReceive('make')->andReturn($socialite); + $app + ->shouldReceive('make') + ->andReturn($socialite); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth1ProviderStubName(), $providerName)); - $s->extendSocialite($providerName, $this->oauth1ProviderStubName(), $this->invalidClass()); + $event->extendSocialite( + $providerName, + $providerClass, + $this->invalidClass() + ); } } diff --git a/tests/OAuth2ProviderTest.php b/tests/OAuth2ProviderTest.php index 085d0dd..58f2884 100644 --- a/tests/OAuth2ProviderTest.php +++ b/tests/OAuth2ProviderTest.php @@ -2,8 +2,10 @@ namespace SocialiteProviders\Manager\Test; +use Laravel\Socialite\Contracts\Factory as SocialiteFactoryContract; use Mockery as m; use SocialiteProviders\Manager\Config; +use SocialiteProviders\Manager\Exception\InvalidArgumentException; use SocialiteProviders\Manager\Exception\MissingConfigException; use SocialiteProviders\Manager\SocialiteWasCalled; use SocialiteProviders\Manager\Test\Stubs\OAuth2ProviderStub; @@ -15,44 +17,40 @@ class OAuth2ProviderTest extends \PHPUnit_Framework_TestCase /** * @test */ - public function it_passes() - { - $this->assertTrue(true); - } - - /** - * @expectedException \SocialiteProviders\Manager\Exception\MissingConfigException - */ public function it_throws_if_there_is_no_config_in_services_or_env() { - $providerName = 'bar'; + $this->expectException(MissingConfigException::class); + $providerName = 'bar'; + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()]) + $socialite + ->shouldReceive('buildProvider') + ->withArgs([$providerClass, $this->config()]) ->andReturn($this->oauth2ProviderStub()); - $socialite->shouldReceive('extend')->withArgs( - [ + $socialite + ->shouldReceive('extend') + ->withArgs([ $providerName, - m::on( - function ($closure) { - $this->assertInstanceOf($this->oauth2ProviderStubName(), $closure()); - - return is_callable($closure); - } - ), - ] - ); + m::on(function ($closure) use ($providerClass) { + $this->assertInstanceOf($providerClass, $closure()); - $app = $this->appMock(); - $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); - - $providerClass = $this->oauth2ProviderStubName(); + return is_callable($closure); + }), + ]); + $app = $this->appMock(); + $app + ->shouldReceive('make') + ->with(SocialiteFactoryContract::class) + ->andReturn($socialite); $configRetriever = $this->configRetrieverMock(); - $configRetriever->shouldReceive('fromServices')->andThrow(MissingConfigException::class); + $configRetriever + ->shouldReceive('fromServices') + ->andThrow(MissingConfigException::class); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $configRetriever); - $s->extendSocialite($providerName, $this->oauth2ProviderStubName()); + $event->extendSocialite($providerName, $providerClass); } /** @@ -61,33 +59,35 @@ function ($closure) { public function it_allows_the_config_to_be_retrieved_from_the_services_array() { $providerName = 'bar'; - + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()]) + $socialite + ->shouldReceive('buildProvider') + ->withArgs([$providerClass, $this->config()]) ->andReturn($this->oauth2ProviderStub()); - $socialite->shouldReceive('extend')->withArgs( - [ + $socialite + ->shouldReceive('extend') + ->withArgs([ $providerName, - m::on( - function ($closure) { - $this->assertInstanceOf($this->oauth2ProviderStubName(), $closure()); - - return is_callable($closure); - } - ), - ] - ); + m::on(function ($closure) use ($providerClass) { + $this->assertInstanceOf($providerClass, $closure()); + return is_callable($closure); + }), + ]); + $config = $this->configObject(); $app = $this->appMock(); - $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); - - $providerClass = $this->oauth2ProviderStubName(); - + $app + ->shouldReceive('make') + ->with(SocialiteFactoryContract::class) + ->andReturn($socialite); $configRetriever = $this->configRetrieverMock(); - $configRetriever->shouldReceive('fromServices')->andThrow(MissingConfigException::class); + $configRetriever + ->shouldReceive('fromServices') + ->andReturn($config); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $configRetriever); - $s->extendSocialite($providerName, $this->oauth2ProviderStubName()); + $event->extendSocialite($providerName, $providerClass); } /** @@ -95,7 +95,12 @@ function ($closure) { */ public function it_allows_a_custom_config_to_be_passed_dynamically() { - $provider = new OAuth2ProviderStub(\Mockery::mock(\Illuminate\Http\Request::class), 'client id', 'client secret', 'redirect url'); + $provider = new OAuth2ProviderStub( + $this->buildRequest(), + 'client id', + 'client secret', + 'redirect url' + ); $result = $provider->setConfig(new Config('key', 'secret', 'callback uri')); @@ -108,28 +113,34 @@ public function it_allows_a_custom_config_to_be_passed_dynamically() public function it_retrieves_from_the_config_if_no_config_is_provided() { $providerName = 'bar'; - + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()]) + $socialite + ->shouldReceive('buildProvider') + ->withArgs([$providerClass, $this->config()]) ->andReturn($this->oauth2ProviderStub()); - $socialite->shouldReceive('extend')->withArgs( - [ + $socialite + ->shouldReceive('extend') + ->withArgs([ $providerName, - m::on( - function ($closure) { - $this->assertInstanceOf($this->oauth2ProviderStubName(), $closure()); - - return is_callable($closure); - } - ), - ] - ); + m::on(function ($closure) use ($providerClass) { + $this->assertInstanceOf($providerClass, $closure()); + return is_callable($closure); + }), + ]); $app = $this->appMock(); - $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); + $app + ->shouldReceive('make') + ->with(SocialiteFactoryContract::class) + ->andReturn($socialite); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth2ProviderStubName())); - $s->extendSocialite($providerName, $this->oauth2ProviderStubName()); + $event->extendSocialite($providerName, $providerClass); } /** @@ -138,35 +149,39 @@ function ($closure) { public function it_should_build_a_provider_and_extend_socialite() { $providerName = 'bar'; - + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()]) + $socialite + ->shouldReceive('buildProvider') + ->withArgs([$providerClass, $this->config()]) ->andReturn($this->oauth2ProviderStub()); - $socialite->shouldReceive('extend')->withArgs( - [ + $socialite + ->shouldReceive('extend') + ->withArgs([ $providerName, - m::on( - function ($closure) { - $this->assertInstanceOf($this->oauth2ProviderStubName(), $closure()); - - return is_callable($closure); - } - ), - ] - ); - - $config = new Config( - $this->config()['client_id'], - $this->config()['client_secret'], - $this->config()['redirect'] - ); + m::on(function ($closure) use ($providerClass) { + $this->assertInstanceOf($providerClass, $closure()); + return is_callable($closure); + }), + ]); + $config = $this->configObject(); $app = $this->appMock(); - $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); - $app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn($config); + $app + ->shouldReceive('make') + ->with(SocialiteFactoryContract::class) + ->andReturn($socialite); + $app + ->shouldReceive('make') + ->with("SocialiteProviders.config.{$providerName}") + ->andReturn($config); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth2ProviderStubName())); - $s->extendSocialite($providerName, $this->oauth2ProviderStubName()); + $event->extendSocialite($providerName, $providerClass); } /** @@ -176,35 +191,39 @@ function ($closure) { public function it_throws_if_given_a_bad_provider_class_name() { $providerName = 'bar'; - + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()]) + $socialite + ->shouldReceive('buildProvider') + ->withArgs([$providerClass, $this->config()]) ->andReturn($this->oauth2ProviderStub()); - $socialite->shouldReceive('extend')->withArgs( - [ + $socialite + ->shouldReceive('extend') + ->withArgs([ $providerName, - m::on( - function ($closure) { - $this->assertInstanceOf($this->oauth2ProviderStubName(), $closure()); - - return is_callable($closure); - } - ), - ] - ); - - $config = new Config( - $this->config()['client_id'], - $this->config()['client_secret'], - $this->config()['redirect'] - ); + m::on(function ($closure) use ($providerClass) { + $this->assertInstanceOf($providerClass, $closure()); + return is_callable($closure); + }), + ]); + $config = $this->configObject(); $app = $this->appMock(); - $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); - $app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn($config); + $app + ->shouldReceive('make') + ->with(SocialiteFactoryContract::class) + ->andReturn($socialite); + $app + ->shouldReceive('make') + ->with("SocialiteProviders.config.{$providerName}") + ->andReturn($config); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth2ProviderStubName())); - $s->extendSocialite($providerName, 'foobar'); + $event->extendSocialite($providerName, $this->invalidClass()); } /** @@ -212,17 +231,22 @@ function ($closure) { */ public function it_throws_if_given_an_invalid_oauth2_provider() { - $this->expectManagerInvalidArgumentException(); + $this->expectException(InvalidArgumentException::class); $providerName = 'foo'; - + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $app = $this->appMock(); - $app->shouldReceive('make')->andReturn($socialite); + $app + ->shouldReceive('make') + ->andReturn($socialite); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth2ProviderStubName())); - $s->extendSocialite($providerName, $this->invalidClass()); + $event->extendSocialite($providerName, $this->invalidClass()); } /** @@ -230,18 +254,26 @@ public function it_throws_if_given_an_invalid_oauth2_provider() */ public function it_throws_if_oauth1_server_is_passed_for_oauth2() { - $this->expectManagerInvalidArgumentException(); + $this->expectException(InvalidArgumentException::class); $providerName = 'baz'; - + $providerClass = $this->oauth2ProviderStubClass(); $socialite = $this->socialiteMock(); - $socialite->shouldReceive('formatConfig')->with($this->config()) + $socialite + ->shouldReceive('formatConfig') + ->with($this->config()) ->andReturn($this->oauth1FormattedConfig($this->config())); $app = $this->appMock(); - $app->shouldReceive('make')->andReturn($socialite); + $app + ->shouldReceive('make') + ->andReturn($socialite); + $configRetriever = $this->configRetrieverMockWithDefaultExpectations( + $providerName, + $providerClass + ); + $event = new SocialiteWasCalled($app, $configRetriever); - $s = new SocialiteWasCalled($app, $this->configRetrieverMockWithDefaultExpectations($this->oauth2ProviderStubName())); - $s->extendSocialite($providerName, $this->oauth2ProviderStubName(), $this->oauth1ServerStubName()); + $event->extendSocialite($providerName, $providerClass, $this->oauth1ServerStubClass()); } } diff --git a/tests/OAuthTwoTest.php b/tests/OAuthTwoTest.php index 4577644..4c34789 100644 --- a/tests/OAuthTwoTest.php +++ b/tests/OAuthTwoTest.php @@ -2,11 +2,15 @@ namespace SocialiteProviders\Manager\Test; +use Illuminate\Contracts\Session\Session as SessionContract; use Illuminate\Http\Request; +use Laravel\Socialite\Two\User as SocialiteOAuth2User; use Mockery as m; use PHPUnit_Framework_TestCase; use SocialiteProviders\Manager\OAuth2\AbstractProvider; use SocialiteProviders\Manager\OAuth2\User; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Session\SessionInterface; class OAuthTwoTest extends PHPUnit_Framework_TestCase { @@ -15,13 +19,16 @@ class OAuthTwoTest extends PHPUnit_Framework_TestCase */ public function redirectGeneratesTheProperSymfonyRedirectResponse() { + $session = m::mock(SessionContract::class); $request = Request::create('foo'); - $request->setLaravelSession($session = m::mock(\Illuminate\Contracts\Session\Session::class)); - $session->shouldReceive('put')->once(); + $request->setLaravelSession($session); + $session + ->shouldReceive('put') + ->once(); $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect'); $response = $provider->redirect(); - $this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $response); + $this->assertInstanceOf(RedirectResponse::class, $response); $this->assertEquals('http://auth.url', $response->getTargetUrl()); } @@ -40,15 +47,37 @@ public function it_can_return_the_service_container_key() */ public function userReturnsAUserInstanceForTheAuthenticatedRequest() { - $request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']); - $request->setSession($session = m::mock(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)); - $session->shouldReceive('pull')->once()->with('state')->andReturn(str_repeat('A', 40)); + $session = m::mock(SessionInterface::class); + $request = Request::create('foo', 'GET', [ + 'state' => str_repeat('A', 40), + 'code' => 'code', + ]); + $request->setSession($session); + $session + ->shouldReceive('pull') + ->once() + ->with('state') + ->andReturn(str_repeat('A', 40)); $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri'); $provider->http = m::mock('StdClass'); - $provider->http->shouldReceive('post')->once()->with('http://token.url', [ - 'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'], - ])->andReturn($response = m::mock('StdClass')); - $response->shouldReceive('getBody')->andReturn('{"access_token": "access_token", "test": "test"}'); + $provider->http + ->shouldReceive('post') + ->once() + ->with('http://token.url', [ + 'headers' => [ + 'Accept' => 'application/json', + ], + 'form_params' => [ + 'client_id' => 'client_id', + 'client_secret' => 'client_secret', + 'code' => 'code', + 'redirect_uri' => 'redirect_uri', + ], + ]) + ->andReturn($response = m::mock('StdClass')); + $response + ->shouldReceive('getBody') + ->andReturn('{"access_token": "access_token", "test": "test"}'); $user = $provider->user(); $this->assertInstanceOf(User::class, $user); @@ -60,16 +89,37 @@ public function userReturnsAUserInstanceForTheAuthenticatedRequest() */ public function access_token_response_body_is_accessible_from_user() { + $session = m::mock(SessionInterface::class); $accessTokenResponseBody = '{"access_token": "access_token", "test": "test"}'; - $request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']); - $request->setSession($session = m::mock(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)); - $session->shouldReceive('pull')->once()->with('state')->andReturn(str_repeat('A', 40)); + $request = Request::create('foo', 'GET', [ + 'state' => str_repeat('A', 40), + 'code' => 'code', + ]); + $request->setSession($session); + $session + ->shouldReceive('pull') + ->once() + ->with('state') + ->andReturn(str_repeat('A', 40)); $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri'); $provider->http = m::mock('StdClass'); - $provider->http->shouldReceive('post')->once()->with('http://token.url', [ - 'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'], - ])->andReturn($response = m::mock('StdClass')); - $response->shouldReceive('getBody')->andReturn($accessTokenResponseBody); + $provider->http + ->shouldReceive('post') + ->once() + ->with('http://token.url', [ + 'headers' => [ + 'Accept' => 'application/json', + ], 'form_params' => [ + 'client_id' => 'client_id', + 'client_secret' => 'client_secret', + 'code' => 'code', + 'redirect_uri' => 'redirect_uri', + ], + ]) + ->andReturn($response = m::mock('StdClass')); + $response + ->shouldReceive('getBody') + ->andReturn($accessTokenResponseBody); $user = $provider->user(); $this->assertInstanceOf(User::class, $user); @@ -82,19 +132,41 @@ public function access_token_response_body_is_accessible_from_user() */ public function regular_laravel_socialite_class_works_as_well() { + $session = m::mock(SessionInterface::class); $accessTokenResponseBody = '{"access_token": "access_token", "test": "test"}'; - $request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']); - $request->setSession($session = m::mock(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)); - $session->shouldReceive('pull')->once()->with('state')->andReturn(str_repeat('A', 40)); + $request = Request::create('foo', 'GET', [ + 'state' => str_repeat('A', 40), + 'code' => 'code', + ]); + $request->setSession($session); + $session + ->shouldReceive('pull') + ->once() + ->with('state') + ->andReturn(str_repeat('A', 40)); $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri'); $provider->http = m::mock('StdClass'); - $provider->http->shouldReceive('post')->once()->with('http://token.url', [ - 'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'], - ])->andReturn($response = m::mock('StdClass')); - $response->shouldReceive('getBody')->andReturn($accessTokenResponseBody); + $provider->http + ->shouldReceive('post') + ->once() + ->with('http://token.url', [ + 'headers' => [ + 'Accept' => 'application/json', + ], + 'form_params' => [ + 'client_id' => 'client_id', + 'client_secret' => 'client_secret', + 'code' => 'code', + 'redirect_uri' => 'redirect_uri', + ], + ]) + ->andReturn($response = m::mock('StdClass')); + $response + ->shouldReceive('getBody') + ->andReturn($accessTokenResponseBody); $user = $provider->user(); - $this->assertInstanceOf(\Laravel\Socialite\Two\User::class, $user); + $this->assertInstanceOf(SocialiteOAuth2User::class, $user); $this->assertEquals('foo', $user->id); } @@ -104,11 +176,19 @@ public function regular_laravel_socialite_class_works_as_well() */ public function exceptionIsThrownIfStateIsInvalid() { - $request = Request::create('foo', 'GET', ['state' => str_repeat('B', 40), 'code' => 'code']); - $request->setSession($session = m::mock(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)); - $session->shouldReceive('pull')->once()->with('state')->andReturn(str_repeat('A', 40)); + $session = m::mock(SessionInterface::class); + $request = Request::create('foo', 'GET', [ + 'state' => str_repeat('B', 40), + 'code' => 'code', + ]); + $request->setSession($session); + $session + ->shouldReceive('pull') + ->once() + ->with('state') + ->andReturn(str_repeat('A', 40)); $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect'); - $user = $provider->user(); + $provider->user(); } /** @@ -117,11 +197,18 @@ public function exceptionIsThrownIfStateIsInvalid() */ public function exceptionIsThrownIfStateIsNotSet() { - $request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']); - $request->setSession($session = m::mock(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)); - $session->shouldReceive('pull')->once()->with('state'); + $session = m::mock(SessionInterface::class); + $request = Request::create('foo', 'GET', [ + 'state' => 'state', + 'code' => 'code', + ]); + $request->setSession($session); + $session + ->shouldReceive('pull') + ->once() + ->with('state'); $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect'); - $user = $provider->user(); + $provider->user(); } } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 7a4df87..e02b7f6 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -16,12 +16,19 @@ class ServiceProviderTest extends \PHPUnit_Framework_TestCase public function it_fires_an_event() { $socialiteWasCalledMock = m::mock(SocialiteWasCalled::class); - self::$functions->shouldReceive('app')->with(SocialiteWasCalled::class)->once()->andReturn($socialiteWasCalledMock); - - self::$functions->shouldReceive('event')->with($socialiteWasCalledMock)->once(); - - $sp = new ServiceProvider($this->appMock()); - $sp->boot(); + self::$functions + ->shouldReceive('app') + ->with(SocialiteWasCalled::class) + ->once() + ->andReturn($socialiteWasCalledMock); + + self::$functions + ->shouldReceive('event') + ->with($socialiteWasCalledMock) + ->once(); + + $serviceProvider = new ServiceProvider($this->appMock()); + $serviceProvider->boot(); $this->assertTrue(true); } diff --git a/tests/Stubs/OAuth1ServerStub.php b/tests/Stubs/OAuth1ServerStub.php index 6b100fe..10e6b1d 100644 --- a/tests/Stubs/OAuth1ServerStub.php +++ b/tests/Stubs/OAuth1ServerStub.php @@ -55,7 +55,7 @@ public function urlUserDetails() * @param mixed $data * @param TokenCredentials $tokenCredentials * - * @return User + * @return \SocialiteProviders\Manager\OAuth1\User */ public function userDetails($data, TokenCredentials $tokenCredentials) {