Skip to content

Commit

Permalink
Merge pull request #266 from foxworth42/add-specific-error-message-wh…
Browse files Browse the repository at this point in the history
…en-generic-client-provider-class-is-missing

Add specific error message.
  • Loading branch information
weaverryan authored Oct 4, 2020
2 parents 822b1a4 + 52aa2e1 commit 6dea4ca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/DependencyInjection/KnpUOAuth2ClientExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ public function load(array $configs, ContainerBuilder $container)
private function configureProviderAndClient(ContainerBuilder $container, $providerType, $providerKey, $providerClass, $clientClass, $packageName, array $options, $redirectRoute, array $redirectParams, $useState, array $collaborators)
{
if ($this->checkExternalClassExistence && !class_exists($providerClass)) {
if ('generic' === $providerType) {
throw new \LogicException(sprintf('The provider class `%s` must exist in order to use with the "%s" OAuth provider.', $providerClass, $providerType));
}
throw new \LogicException(sprintf('Run `composer require %s` in order to use the "%s" OAuth provider.', $packageName, $providerType));
}

Expand Down
43 changes: 43 additions & 0 deletions tests/DependencyInjection/KnpUOAuth2ClientExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,49 @@ public function provideBadConfiguration()
return $tests;
}

public function testShouldThrowExceptionIfKnownProviderClassNotInstalled()
{
$this->configuration = new ContainerBuilder();
$loader = new KnpUOAuth2ClientExtension(true);
$config = ['clients' => [
"fitbit" => [
'type' => 'fitbit',
'client_id' => 'abc',
'client_secret' => '123',
'redirect_route' => 'foo_bar_route',
'redirect_params' => []
]
]];

try {
$loader->load([$config], $this->configuration);
} catch (\Exception $e) {
$this->assertEquals('Run `composer require djchen/oauth2-fitbit` in order to use the "fitbit" OAuth provider.', $e->getMessage());
}
}

public function testShouldThrowSpecificErrorMessageIfProviderMissingForGenericClient()
{
$this->configuration = new ContainerBuilder();
$loader = new KnpUOAuth2ClientExtension(true);
$config = ['clients' => [
"custom_provider" => [
'type' => 'generic',
'provider_class' => 'Some\Provider\That\Doesnt\Exist',
'client_id' => 'abc',
'client_secret' => '123',
'redirect_route' => 'foo_bar_route',
'redirect_params' => [],
]
]];

try {
$loader->load([$config], $this->configuration);
} catch (\Exception $e) {
$this->assertEquals('The provider class `Some\Provider\That\Doesnt\Exist` must exist in order to use with the "generic" OAuth provider.', $e->getMessage());
}
}

public function testGetAllSupportedTypes()
{
$types = KnpUOAuth2ClientExtension::getAllSupportedTypes();
Expand Down

0 comments on commit 6dea4ca

Please sign in to comment.