-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from foxworth42/improve-test-coverage
Improve test coverage
- Loading branch information
Showing
12 changed files
with
524 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Client; | ||
|
||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | ||
use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class ClientRegistryTest extends TestCase | ||
{ | ||
public function testShouldKnowWhatServicesAreConfigured() | ||
{ | ||
$mockServiceMap = [ | ||
'facebook' => 'knpu.oauth2.client.facebook', | ||
'google' => 'knpu.oauth2.client.google', | ||
'okta' => 'knpu.oauth2.client.okta' | ||
]; | ||
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock(); | ||
|
||
$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap); | ||
|
||
$results = $testClientRegistry->getEnabledClientKeys(); | ||
$this->assertEquals([ | ||
"facebook", | ||
"google", | ||
"okta" | ||
], $results); | ||
} | ||
|
||
public function testShouldThrowExceptionIfClientDoesNotExist() | ||
{ | ||
$mockServiceMap = [ | ||
'facebook' => 'knpu.oauth2.client.facebook', | ||
'google' => 'knpu.oauth2.client.google', | ||
'okta' => 'knpu.oauth2.client.okta' | ||
]; | ||
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock(); | ||
|
||
$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
|
||
$testClientRegistry->getClient("unknownClient"); | ||
} | ||
|
||
public function testShouldThrowExceptionIfClientExistsButNotOAuth2Client() | ||
{ | ||
$mockServiceMap = [ | ||
'facebook' => 'knpu.oauth2.client.facebook', | ||
'google' => 'knpu.oauth2.client.google', | ||
'okta' => 'knpu.oauth2.client.okta', | ||
'invalid' => 'knpu.oauth2.client.invalid' | ||
]; | ||
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock(); | ||
$mockContainer->method("get")->willReturn(\stdClass::class); | ||
|
||
$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
|
||
$testClientRegistry->getClient("invalid"); | ||
} | ||
|
||
public function testShouldReturnValidClient() | ||
{ | ||
$mockServiceMap = [ | ||
'facebook' => 'knpu.oauth2.client.facebook', | ||
'google' => 'knpu.oauth2.client.google', | ||
'okta' => 'knpu.oauth2.client.okta' | ||
]; | ||
$mockClient = $this->getMockBuilder(OAuth2ClientInterface::class)->getMock(); | ||
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock(); | ||
$mockContainer->method("get")->willReturn($mockClient); | ||
|
||
$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap); | ||
|
||
$result = $testClientRegistry->getClient("facebook"); | ||
|
||
$this->assertInstanceOf(OAuth2ClientInterface::class, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Client\Provider; | ||
|
||
use KnpU\OAuth2ClientBundle\Client\OAuth2Client; | ||
use League\OAuth2\Client\Provider\AbstractProvider; | ||
use League\OAuth2\Client\Provider\ResourceOwnerInterface; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class BatchProviderTest extends TestCase | ||
{ | ||
public function testProviders() | ||
{ | ||
// This is basically just validating that the clients are sane/implementing OAuth2Client | ||
|
||
$mockAccessToken = $this->getMockBuilder(AccessToken::class)->disableOriginalConstructor()->getMock(); | ||
$mockProvider = $this->getMockProvider($mockAccessToken); | ||
$mockRequestStack = $this->getMockRequestStack($this->getMockRequest()); | ||
|
||
$clients = scandir(__DIR__ . "/../../../src/Client/Provider"); | ||
foreach($clients as $client) { | ||
if(substr($client, -4, 4) !== ".php") { continue; } | ||
|
||
$client = sprintf("KnpU\OAuth2ClientBundle\Client\Provider\%s", explode(".", $client)[0]); | ||
$testClient = new $client($mockProvider, $mockRequestStack); | ||
$testClient->setAsStateless(); | ||
$this->assertTrue(is_subclass_of($testClient, OAuth2Client::class)); | ||
|
||
$this->assertInstanceOf(ResourceOwnerInterface::class, $testClient->fetchUserFromToken($mockAccessToken)); | ||
$this->assertInstanceOf(ResourceOwnerInterface::class, $testClient->fetchUser()); | ||
} | ||
} | ||
|
||
private function getMockProvider($mockAccessToken) | ||
{ | ||
$mockProvider = $this->getMockBuilder(AbstractProvider::class)->getMock(); | ||
$mockProvider->method("getResourceOwner")->willReturn($this->getMockBuilder(ResourceOwnerInterface::class)->getMock()); | ||
$mockProvider->method("getAccessToken")->willReturn($mockAccessToken); | ||
return $mockProvider; | ||
} | ||
|
||
private function getMockRequest() | ||
{ | ||
$mockRequest = $this->getMockBuilder(Request::class)->getMock(); | ||
$mockRequest->method("get")->willReturn(true); | ||
return $mockRequest; | ||
} | ||
|
||
private function getMockRequestStack($mockRequest) | ||
{ | ||
$mockRequestStack = $this->getMockBuilder(RequestStack::class)->getMock(); | ||
$mockRequestStack->method("getCurrentRequest")->willReturn($mockRequest); | ||
return $mockRequestStack; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\DependencyInjection; | ||
|
||
use KnpU\OAuth2ClientBundle\DependencyInjection\ProviderFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
class ProviderFactoryTest extends TestCase | ||
{ | ||
public function testShouldCreateProvider() | ||
{ | ||
$redirectUri = "http://redirect.url"; | ||
$mockGenerator = $this->getMockGenerator($redirectUri); | ||
$mockGenerator->expects($this->once())->method("generate"); | ||
|
||
$testProviderFactory = new ProviderFactory($mockGenerator); | ||
|
||
$httpsRedirectUri = "symfony_route_name"; | ||
$result = $testProviderFactory->createProvider(MockProvider::class, [], $httpsRedirectUri); | ||
|
||
$this->assertInstanceOf(MockProvider::class, $result); | ||
$this->assertEquals(["redirectUri" => $redirectUri], $result->getOptions()); | ||
$this->assertEquals([], $result->getCollaborators()); | ||
} | ||
|
||
private function getMockGenerator($generateReturn) | ||
{ | ||
$mockGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->disableOriginalConstructor()->getMock(); | ||
$mockGenerator->method("generate")->willReturn($generateReturn); | ||
return $mockGenerator; | ||
} | ||
} | ||
|
||
class MockProvider | ||
{ | ||
private $options; | ||
private $collaborators; | ||
public function __construct($options, $collaborators) | ||
{ | ||
$this->options = $options; | ||
$this->collaborators = $collaborators; | ||
} | ||
|
||
public function getOptions() | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function getCollaborators() | ||
{ | ||
return $this->collaborators; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests; | ||
|
||
use KnpU\OAuth2ClientBundle\DependencyInjection\KnpUOAuth2ClientExtension; | ||
use KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class KnpUOAuth2ClientBundleTest extends TestCase | ||
{ | ||
public function testShouldReturnNewContainerExtension() | ||
{ | ||
$testBundle = new KnpUOAuth2ClientBundle(); | ||
|
||
$result = $testBundle->getContainerExtension(); | ||
$this->assertInstanceOf(KnpUOAuth2ClientExtension::class, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/Security/Exception/IdentityProviderAuthenticationExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Security\Exception; | ||
|
||
use KnpU\OAuth2ClientBundle\Security\Exception\IdentityProviderAuthenticationException; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IdentityProviderAuthenticationExceptionTest extends TestCase | ||
{ | ||
public function testException() | ||
{ | ||
$mockProviderException = new IdentityProviderException("Message", 0, "Response"); | ||
|
||
$testException = new IdentityProviderAuthenticationException($mockProviderException); | ||
$this->assertEquals('Error fetching OAuth credentials: "%error%".', $testException->getMessageKey()); | ||
$this->assertEquals(["%error%" => "Message"], $testException->getMessageData()); | ||
} | ||
} |
Oops, something went wrong.