From 776a92502fdedb2dfce28f7ca9a4e5b311653d7f Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Mon, 16 Dec 2013 13:26:25 +0200 Subject: [PATCH] "yii\authclient" structure refactored. --- extensions/yii/authclient/AuthAction.php | 11 +- extensions/yii/authclient/ClientInterface.php | 52 +++++ extensions/yii/authclient/ClientTrait.php | 145 ++++++++++++++ extensions/yii/authclient/Collection.php | 107 ++++++++++ extensions/yii/authclient/OpenId.php | 11 +- extensions/yii/authclient/provider/Collection.php | 105 ---------- extensions/yii/authclient/provider/OAuth1.php | 51 ----- extensions/yii/authclient/provider/OAuth2.php | 58 ------ extensions/yii/authclient/provider/OpenId.php | 74 ------- .../yii/authclient/provider/ProviderInterface.php | 73 ------- .../yii/authclient/provider/ProviderTrait.php | 221 --------------------- .../yii/authclient/provider/views/redirect.php | 38 ---- extensions/yii/authclient/views/redirect.php | 38 ++++ .../unit/extensions/authclient/ClientTraitTest.php | 58 ++++++ .../unit/extensions/authclient/CollectionTest.php | 90 +++++++++ .../authclient/provider/CollectionTest.php | 90 --------- .../authclient/provider/ProviderTraitTest.php | 90 --------- 17 files changed, 499 insertions(+), 813 deletions(-) create mode 100644 extensions/yii/authclient/ClientInterface.php create mode 100644 extensions/yii/authclient/ClientTrait.php create mode 100644 extensions/yii/authclient/Collection.php delete mode 100644 extensions/yii/authclient/provider/Collection.php delete mode 100644 extensions/yii/authclient/provider/OAuth1.php delete mode 100644 extensions/yii/authclient/provider/OAuth2.php delete mode 100644 extensions/yii/authclient/provider/OpenId.php delete mode 100644 extensions/yii/authclient/provider/ProviderInterface.php delete mode 100644 extensions/yii/authclient/provider/ProviderTrait.php delete mode 100644 extensions/yii/authclient/provider/views/redirect.php create mode 100644 extensions/yii/authclient/views/redirect.php create mode 100644 tests/unit/extensions/authclient/ClientTraitTest.php create mode 100644 tests/unit/extensions/authclient/CollectionTest.php delete mode 100644 tests/unit/extensions/authclient/provider/CollectionTest.php delete mode 100644 tests/unit/extensions/authclient/provider/ProviderTraitTest.php diff --git a/extensions/yii/authclient/AuthAction.php b/extensions/yii/authclient/AuthAction.php index f3dc453..6109bd9 100644 --- a/extensions/yii/authclient/AuthAction.php +++ b/extensions/yii/authclient/AuthAction.php @@ -159,7 +159,7 @@ class AuthAction extends Action 'url' => $url, 'enforceRedirect' => $enforceRedirect, ]; - $viewFile = __DIR__ . DIRECTORY_SEPARATOR . 'provider' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'redirect.php'; + $viewFile = __DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'redirect.php'; $response = Yii::$app->getResponse(); $response->content = Yii::$app->getView()->renderFile($viewFile, $viewData); @@ -208,7 +208,7 @@ class AuthAction extends Action 'id' => $provider->identity ); $rawAttributes = $provider->getAttributes(); - foreach ($provider->getRequiredAttributes() as $openIdAttributeName) { + foreach ($provider->requiredAttributes as $openIdAttributeName) { if (isset($rawAttributes[$openIdAttributeName])) { $attributes[$openIdAttributeName] = $rawAttributes[$openIdAttributeName]; } else { @@ -216,7 +216,6 @@ class AuthAction extends Action } } $provider->setAttributes($attributes); - $provider->isAuthenticated = true; return $this->authenticateSuccess($provider); } else { throw new Exception('Unable to complete the authentication because the required data was not received.'); @@ -231,10 +230,6 @@ class AuthAction extends Action } } else { $provider->identity = $provider->authUrl; // Setting identifier - $provider->required = []; // Try to get info from openid provider - foreach ($provider->getRequiredAttributes() as $openIdAttributeName) { - $this->required[] = $openIdAttributeName; - } $request = Yii::$app->getRequest(); $provider->realm = $request->getHostInfo(); $provider->returnUrl = $provider->realm . $request->getUrl(); // getting return URL @@ -270,7 +265,6 @@ class AuthAction extends Action } else { // Upgrade to access token. $accessToken = $provider->fetchAccessToken(); - $provider->isAuthenticated = true; return $this->authenticateSuccess($provider); } } @@ -304,7 +298,6 @@ class AuthAction extends Action $code = $_GET['code']; $token = $provider->fetchAccessToken($code); if (!empty($token)) { - $provider->isAuthenticated = true; return $this->authenticateSuccess($provider); } else { return $this->redirectCancel(); diff --git a/extensions/yii/authclient/ClientInterface.php b/extensions/yii/authclient/ClientInterface.php new file mode 100644 index 0000000..2b08e1e --- /dev/null +++ b/extensions/yii/authclient/ClientInterface.php @@ -0,0 +1,52 @@ + + * @since 2.0 + */ +interface ClientInterface +{ + /** + * @param string $id service id. + */ + public function setId($id); + + /** + * @return string service id + */ + public function getId(); + + /** + * @return string service name. + */ + public function getName(); + + /** + * @param string $name service name. + */ + public function setName($name); + + /** + * @return string service title. + */ + public function getTitle(); + + /** + * @param string $title service title. + */ + public function setTitle($title); + + /** + * @return array list of user attributes + */ + public function getUserAttributes(); +} \ No newline at end of file diff --git a/extensions/yii/authclient/ClientTrait.php b/extensions/yii/authclient/ClientTrait.php new file mode 100644 index 0000000..cdd3150 --- /dev/null +++ b/extensions/yii/authclient/ClientTrait.php @@ -0,0 +1,145 @@ + + * @since 2.0 + */ +trait ClientTrait +{ + /** + * @var string service id. + * This value mainly used as HTTP request parameter. + */ + private $_id; + /** + * @var string service unique name. + * This value may be used in database records, CSS files and so on. + */ + private $_name; + /** + * @var string service title to display in views. + */ + private $_title; + /** + * @var array authenticated user attributes. + */ + private $_userAttributes; + + /** + * @param string $id service id. + */ + public function setId($id) + { + $this->_id = $id; + } + + /** + * @return string service id + */ + public function getId() + { + if (empty($this->_id)) { + $this->_id = $this->getName(); + } + return $this->_id; + } + + /** + * @return string service name. + */ + public function getName() + { + if ($this->_name === null) { + $this->_name = $this->defaultName(); + } + return $this->_name; + } + + /** + * @param string $name service name. + */ + public function setName($name) + { + $this->_name = $name; + } + + /** + * @return string service title. + */ + public function getTitle() + { + if ($this->_title === null) { + $this->_title = $this->defaultTitle(); + } + return $this->_title; + } + + /** + * @param string $title service title. + */ + public function setTitle($title) + { + $this->_title = $title; + } + + /** + * @return array list of user attributes + */ + public function getUserAttributes() + { + if ($this->_userAttributes === null) { + $this->_userAttributes = $this->initUserAttributes(); + } + return $this->_userAttributes; + } + + /** + * @param array $userAttributes list of user attributes + */ + public function setUserAttributes(array $userAttributes) + { + $this->_userAttributes = $userAttributes; + } + + /** + * Generates service name. + * @return string service name. + */ + protected function defaultName() + { + return StringHelper::basename(get_class($this)); + } + + /** + * Generates service title. + * @return string service title. + */ + protected function defaultTitle() + { + return StringHelper::basename(get_class($this)); + } + + /** + * Initializes authenticated user attributes. + * @return array auth user attributes. + */ + protected function initUserAttributes() + { + throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.'); + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/Collection.php b/extensions/yii/authclient/Collection.php new file mode 100644 index 0000000..d94e619 --- /dev/null +++ b/extensions/yii/authclient/Collection.php @@ -0,0 +1,107 @@ + [ + * 'auth' => [ + * 'class' => 'yii\authclient\Collection', + * 'clients' => [ + * 'google' => [ + * 'class' => 'yii\authclient\clients\GoogleOpenId' + * ], + * 'facebook' => [ + * 'class' => 'yii\authclient\clients\Facebook', + * 'clientId' => 'facebook_client_id', + * 'clientSecret' => 'facebook_client_secret', + * ], + * ], + * ] + * ... + * ] + * ~~~ + * + * @property array $clients list of Auth clients with their configuration in format: 'clientId' => [...] + * + * @author Paul Klimov + * @since 2.0 + */ +class Collection extends Component +{ + /** + * @var array list of Auth clients with their configuration in format: 'clientId' => [...] + */ + private $_clients = []; + + /** + * @param array $clients list of auth clients + */ + public function setClients(array $clients) + { + $this->_clients = $clients; + } + + /** + * @return ClientInterface[] list of auth clients. + */ + public function getClients() + { + $clients = []; + foreach ($this->_clients as $id => $client) { + $clients[$id] = $this->getClient($id); + } + return $clients; + } + + /** + * @param string $id service id. + * @return ClientInterface auth client instance. + * @throws InvalidParamException on non existing client request. + */ + public function getClient($id) + { + if (!array_key_exists($id, $this->_clients)) { + throw new InvalidParamException("Unknown auth client '{$id}'."); + } + if (!is_object($this->_clients[$id])) { + $this->_clients[$id] = $this->createClient($id, $this->_clients[$id]); + } + return $this->_clients[$id]; + } + + /** + * Checks if client exists in the hub. + * @param string $id client id. + * @return boolean whether client exist. + */ + public function hasClient($id) + { + return array_key_exists($id, $this->_clients); + } + + /** + * Creates auth client instance from its array configuration. + * @param string $id auth client id. + * @param array $config auth client instance configuration. + * @return ClientInterface auth client instance. + */ + protected function createClient($id, $config) + { + $config['id'] = $id; + return Yii::createObject($config); + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/OpenId.php b/extensions/yii/authclient/OpenId.php index f421379..fa87204 100644 --- a/extensions/yii/authclient/OpenId.php +++ b/extensions/yii/authclient/OpenId.php @@ -27,7 +27,10 @@ use yii\base\NotSupportedException; */ class OpenId extends Component { - public $required = []; + /** + * @var array list of attributes, which should be requested from server. + */ + public $requiredAttributes = []; public $optional = []; public $verify_peer; public $capath; @@ -501,9 +504,9 @@ class OpenId extends Component # That's because it's fully backwards compatibile with 1.0, and some providers # advertise 1.0 even if they accept only 1.1. One such provider is myopenid.com $params['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1'; - if ($this->required) { + if ($this->requiredAttributes) { $params['openid.sreg.required'] = []; - foreach ($this->required as $required) { + foreach ($this->requiredAttributes as $required) { if (!isset(self::$axToSregMap[$required])) { continue; } @@ -528,7 +531,7 @@ class OpenId extends Component protected function axParams() { $params = []; - if ($this->required || $this->optional) { + if ($this->requiredAttributes || $this->optional) { $params['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0'; $params['openid.ax.mode'] = 'fetch_request'; $this->aliases = []; diff --git a/extensions/yii/authclient/provider/Collection.php b/extensions/yii/authclient/provider/Collection.php deleted file mode 100644 index 309eae5..0000000 --- a/extensions/yii/authclient/provider/Collection.php +++ /dev/null @@ -1,105 +0,0 @@ - [ - * 'auth' => [ - * 'class' => 'yii\authclient\provider\Collection', - * 'providers' => [ - * 'google' => [ - * 'class' => 'yii\authclient\provider\GoogleOpenId' - * ], - * 'facebook' => [ - * 'class' => 'yii\authclient\provider\Facebook', - * 'clientId' => 'facebook_client_id', - * 'clientSecret' => 'facebook_client_secret', - * ], - * ], - * ] - * ... - * ] - * ~~~ - * - * @author Paul Klimov - * @since 2.0 - */ -class Collection extends Component -{ - /** - * @var array list of Auth providers with their configuration in format: 'providerId' => [...] - */ - private $_providers = []; - - /** - * @param array $providers list of auth providers - */ - public function setProviders(array $providers) - { - $this->_providers = $providers; - } - - /** - * @return ProviderInterface[] list of auth providers. - */ - public function getProviders() - { - $providers = []; - foreach ($this->_providers as $id => $provider) { - $providers[$id] = $this->getProvider($id); - } - return $providers; - } - - /** - * @param string $id service id. - * @return ProviderInterface auth service instance. - * @throws InvalidParamException on non existing provider request. - */ - public function getProvider($id) - { - if (!array_key_exists($id, $this->_providers)) { - throw new InvalidParamException("Unknown auth provider '{$id}'."); - } - if (!is_object($this->_providers[$id])) { - $this->_providers[$id] = $this->createProvider($id, $this->_providers[$id]); - } - return $this->_providers[$id]; - } - - /** - * Checks if provider exists in the hub. - * @param string $id provider id. - * @return boolean whether provider exist. - */ - public function hasProvider($id) - { - return array_key_exists($id, $this->_providers); - } - - /** - * Creates auth provider instance from its array configuration. - * @param string $id auth provider id. - * @param array $config auth provider instance configuration. - * @return ProviderInterface auth provider instance. - */ - protected function createProvider($id, $config) - { - $config['id'] = $id; - return Yii::createObject($config); - } -} \ No newline at end of file diff --git a/extensions/yii/authclient/provider/OAuth1.php b/extensions/yii/authclient/provider/OAuth1.php deleted file mode 100644 index 4220c39..0000000 --- a/extensions/yii/authclient/provider/OAuth1.php +++ /dev/null @@ -1,51 +0,0 @@ - - * @since 2.0 - */ -class OAuth1 extends \yii\authclient\OAuth1 implements ProviderInterface -{ - use ProviderTrait; - - /** - * @inheritdoc - */ - public function authenticate() - { - // user denied error - if (isset($_GET['denied'])) { - return $this->redirectCancel(); - } - - if (isset($_REQUEST['oauth_token'])) { - $oauthToken = $_REQUEST['oauth_token']; - } - - if (!isset($oauthToken)) { - // Get request token. - $requestToken = $this->fetchRequestToken(); - // Get authorization URL. - $url = $this->buildAuthUrl($requestToken); - // Redirect to authorization URL. - return Yii::$app->getResponse()->redirect($url); - } else { - // Upgrade to access token. - $accessToken = $this->fetchAccessToken(); - $this->isAuthenticated = true; - } - - return $this->isAuthenticated; - } -} \ No newline at end of file diff --git a/extensions/yii/authclient/provider/OAuth2.php b/extensions/yii/authclient/provider/OAuth2.php deleted file mode 100644 index e55328e..0000000 --- a/extensions/yii/authclient/provider/OAuth2.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @since 2.0 - */ -class OAuth2 extends \yii\authclient\OAuth2 implements ProviderInterface -{ - use ProviderTrait; - - /** - * @inheritdoc - */ - public function authenticate() - { - if (isset($_GET['error'])) { - if ($_GET['error'] == 'access_denied') { - // user denied error - return $this->redirectCancel(); - } else { - // request error - if (isset($_GET['error_description'])) { - $errorMessage = $_GET['error_description']; - } elseif (isset($_GET['error_message'])) { - $errorMessage = $_GET['error_message']; - } else { - $errorMessage = http_build_query($_GET); - } - throw new Exception('Auth error: ' . $errorMessage); - } - } - - // Get the access_token and save them to the session. - if (isset($_GET['code'])) { - $code = $_GET['code']; - $token = $this->fetchAccessToken($code); - if (!empty($token)) { - $this->isAuthenticated = true; - } - } else { - $url = $this->buildAuthUrl(); - return Yii::$app->getResponse()->redirect($url); - } - - return $this->isAuthenticated; - } -} \ No newline at end of file diff --git a/extensions/yii/authclient/provider/OpenId.php b/extensions/yii/authclient/provider/OpenId.php deleted file mode 100644 index ff1985c..0000000 --- a/extensions/yii/authclient/provider/OpenId.php +++ /dev/null @@ -1,74 +0,0 @@ - - * @since 2.0 - */ -class OpenId extends \yii\authclient\OpenId implements ProviderInterface -{ - use ProviderTrait; - - /** - * @inheritdoc - */ - public function authenticate() - { - if (!empty($_REQUEST['openid_mode'])) { - switch ($_REQUEST['openid_mode']) { - case 'id_res': - if ($this->validate()) { - $attributes = array( - 'id' => $this->identity - ); - $rawAttributes = $this->getAttributes(); - foreach ($this->getRequiredAttributes() as $openIdAttributeName) { - if (isset($rawAttributes[$openIdAttributeName])) { - $attributes[$openIdAttributeName] = $rawAttributes[$openIdAttributeName]; - } else { - throw new Exception('Unable to complete the authentication because the required data was not received.'); - } - } - $this->setAttributes($attributes); - $this->isAuthenticated = true; - return true; - } else { - throw new Exception('Unable to complete the authentication because the required data was not received.'); - } - break; - case 'cancel': - $this->redirectCancel(); - break; - default: - throw new HttpException(400); - break; - } - } else { - $this->identity = $this->authUrl; // Setting identifier - $this->required = []; // Try to get info from openid provider - foreach ($this->getRequiredAttributes() as $openIdAttributeName) { - $this->required[] = $openIdAttributeName; - } - $request = Yii::$app->getRequest(); - $this->realm = $request->getHostInfo(); - $this->returnUrl = $this->realm . $request->getUrl(); // getting return URL - - $url = $this->authUrl(); - return Yii::$app->getResponse()->redirect($url); - } - - return false; - } -} \ No newline at end of file diff --git a/extensions/yii/authclient/provider/ProviderInterface.php b/extensions/yii/authclient/provider/ProviderInterface.php deleted file mode 100644 index b3f2b27..0000000 --- a/extensions/yii/authclient/provider/ProviderInterface.php +++ /dev/null @@ -1,73 +0,0 @@ - - * @since 2.0 - */ -interface ProviderInterface -{ - /** - * @param string $id service id. - */ - public function setId($id); - - /** - * @return string service id - */ - public function getId(); - - /** - * @return string service name. - */ - public function getName(); - - /** - * @param string $name service name. - */ - public function setName($name); - - /** - * @return string service title. - */ - public function getTitle(); - - /** - * @param string $title service title. - */ - public function setTitle($title); - - /** - * @param string $url successful URL. - */ - public function setSuccessUrl($url); - - /** - * @return string successful URL. - */ - public function getSuccessUrl(); - - /** - * @param string $url cancel URL. - */ - public function setCancelUrl($url); - - /** - * @return string cancel URL. - */ - public function getCancelUrl(); - - /** - * Authenticate the user. - * @return \yii\web\Response|boolean response instance or whether user was successfully authenticated. - */ - public function authenticate(); -} \ No newline at end of file diff --git a/extensions/yii/authclient/provider/ProviderTrait.php b/extensions/yii/authclient/provider/ProviderTrait.php deleted file mode 100644 index 129fa0d..0000000 --- a/extensions/yii/authclient/provider/ProviderTrait.php +++ /dev/null @@ -1,221 +0,0 @@ - - * @since 2.0 - */ -trait ProviderTrait -{ - /** - * @var string service id. - * This value mainly used as HTTP request parameter. - */ - private $_id; - /** - * @var string service unique name. - * This value may be used in database records, CSS files and so on. - */ - private $_name; - /** - * @var string service title to display in views. - */ - private $_title; - /** - * @var string the redirect url after successful authorization. - */ - private $_successUrl = ''; - /** - * @var string the redirect url after unsuccessful authorization (e.g. user canceled). - */ - private $_cancelUrl = ''; - - /** - * @param string $id service id. - */ - public function setId($id) - { - $this->_id = $id; - } - - /** - * @return string service id - */ - public function getId() - { - if (empty($this->_id)) { - $this->_id = $this->getName(); - } - return $this->_id; - } - - /** - * @return string service name. - */ - public function getName() - { - if ($this->_name === null) { - $this->_name = $this->defaultName(); - } - return $this->_name; - } - - /** - * @param string $name service name. - */ - public function setName($name) - { - $this->_name = $name; - } - - /** - * @return string service title. - */ - public function getTitle() - { - if ($this->_title === null) { - $this->_title = $this->defaultTitle(); - } - return $this->_title; - } - - /** - * @param string $title service title. - */ - public function setTitle($title) - { - $this->_title = $title; - } - - /** - * @param string $url successful URL. - */ - public function setSuccessUrl($url) - { - $this->_successUrl = $url; - } - - /** - * @return string successful URL. - */ - public function getSuccessUrl() - { - if (empty($this->_successUrl)) { - $this->_successUrl = $this->defaultSuccessUrl(); - } - return $this->_successUrl; - } - - /** - * @param string $url cancel URL. - */ - public function setCancelUrl($url) - { - $this->_cancelUrl = $url; - } - - /** - * @return string cancel URL. - */ - public function getCancelUrl() - { - if (empty($this->_cancelUrl)) { - $this->_cancelUrl = $this->defaultCancelUrl(); - } - return $this->_cancelUrl; - } - - /** - * Generates service name. - * @return string service name. - */ - protected function defaultName() - { - return StringHelper::basename(get_class($this)); - } - - /** - * Generates service title. - * @return string service title. - */ - protected function defaultTitle() - { - return StringHelper::basename(get_class($this)); - } - - /** - * Creates default {@link successUrl} value. - * @return string success URL value. - */ - protected function defaultSuccessUrl() - { - return Yii::$app->getUser()->getReturnUrl(); - } - - /** - * Creates default {@link cancelUrl} value. - * @return string cancel URL value. - */ - protected function defaultCancelUrl() - { - return Yii::$app->getRequest()->getAbsoluteUrl(); - } - - /** - * Redirect to the given URL or simply close the popup window. - * @param mixed $url URL to redirect, could be a string or array config to generate a valid URL. - * @param boolean $enforceRedirect indicates if redirect should be performed even in case of popup window. - * @return \yii\web\Response response instance. - */ - public function redirect($url, $enforceRedirect = true) - { - $viewData = [ - 'url' => $url, - 'enforceRedirect' => $enforceRedirect, - ]; - $viewFile = __DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'redirect.php'; - - $response = Yii::$app->getResponse(); - $response->content = Yii::$app->getView()->renderFile($viewFile, $viewData); - return $response; - } - - /** - * Redirect to the URL. If URL is null, {@link successUrl} will be used. - * @param string $url URL to redirect. - * @return \yii\web\Response response instance. - */ - public function redirectSuccess($url = null) - { - if ($url === null) { - $url = $this->getSuccessUrl(); - } - return $this->redirect($url); - } - - /** - * Redirect to the {@link cancelUrl} or simply close the popup window. - * @param string $url URL to redirect. - * @return \yii\web\Response response instance. - */ - public function redirectCancel($url = null) - { - if ($url === null) { - $url = $this->getCancelUrl(); - } - return $this->redirect($url, false); - } -} \ No newline at end of file diff --git a/extensions/yii/authclient/provider/views/redirect.php b/extensions/yii/authclient/provider/views/redirect.php deleted file mode 100644 index e85aa46..0000000 --- a/extensions/yii/authclient/provider/views/redirect.php +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/extensions/yii/authclient/views/redirect.php b/extensions/yii/authclient/views/redirect.php new file mode 100644 index 0000000..e85aa46 --- /dev/null +++ b/extensions/yii/authclient/views/redirect.php @@ -0,0 +1,38 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/unit/extensions/authclient/ClientTraitTest.php b/tests/unit/extensions/authclient/ClientTraitTest.php new file mode 100644 index 0000000..404533f --- /dev/null +++ b/tests/unit/extensions/authclient/ClientTraitTest.php @@ -0,0 +1,58 @@ + [ + 'user' => [ + 'identityClass' => '\yii\web\IdentityInterface' + ], + 'request' => [ + 'hostInfo' => 'http://testdomain.com', + 'scriptUrl' => '/index.php', + ], + ] + ]; + $this->mockApplication($config, '\yii\web\Application'); + } + + public function testSetGet() + { + $provider = new Client(); + + $id = 'test_id'; + $provider->setId($id); + $this->assertEquals($id, $provider->getId(), 'Unable to setup id!'); + + $name = 'test_name'; + $provider->setName($name); + $this->assertEquals($name, $provider->getName(), 'Unable to setup name!'); + + $title = 'test_title'; + $provider->setTitle($title); + $this->assertEquals($title, $provider->getTitle(), 'Unable to setup title!'); + } + + public function testGetDescriptiveData() + { + $provider = new Client(); + + $this->assertNotEmpty($provider->getName(), 'Unable to get name!'); + $this->assertNotEmpty($provider->getTitle(), 'Unable to get title!'); + } +} + +class Client extends Object implements ClientInterface +{ + use ClientTrait; + + public function authenticate() {} +} \ No newline at end of file diff --git a/tests/unit/extensions/authclient/CollectionTest.php b/tests/unit/extensions/authclient/CollectionTest.php new file mode 100644 index 0000000..14f65a0 --- /dev/null +++ b/tests/unit/extensions/authclient/CollectionTest.php @@ -0,0 +1,90 @@ + new TestClient(), + 'testClient2' => new TestClient(), + ]; + $collection->setClients($clients); + $this->assertEquals($clients, $collection->getClients(), 'Unable to setup clients!'); + } + + /** + * @depends testSetGet + */ + public function testGetProviderById() + { + $collection = new Collection(); + + $clientId = 'testClientId'; + $client = new TestClient(); + $clients = [ + $clientId => $client + ]; + $collection->setClients($clients); + + $this->assertEquals($client, $collection->getClient($clientId), 'Unable to get client by id!'); + } + + /** + * @depends testGetProviderById + */ + public function testCreateProvider() + { + $collection = new Collection(); + + $clientId = 'testClientId'; + $clientClassName = TestClient::className(); + $clients = [ + $clientId => [ + 'class' => $clientClassName + ] + ]; + $collection->setClients($clients); + + $provider = $collection->getClient($clientId); + $this->assertTrue(is_object($provider), 'Unable to create client by config!'); + $this->assertTrue(is_a($provider, $clientClassName), 'Client has wrong class name!'); + } + + /** + * @depends testSetGet + */ + public function testHasProvider() + { + $collection = new Collection(); + + $clientName = 'testClientName'; + $clients = [ + $clientName => [ + 'class' => 'TestClient1' + ], + ]; + $collection->setClients($clients); + + $this->assertTrue($collection->hasClient($clientName), 'Existing client check fails!'); + $this->assertFalse($collection->hasClient('unExistingClientName'), 'Not existing client check fails!'); + } +} + +class TestClient extends Object implements ClientInterface +{ + use ClientTrait; + + public function authenticate() {} +} \ No newline at end of file diff --git a/tests/unit/extensions/authclient/provider/CollectionTest.php b/tests/unit/extensions/authclient/provider/CollectionTest.php deleted file mode 100644 index 9c65e59..0000000 --- a/tests/unit/extensions/authclient/provider/CollectionTest.php +++ /dev/null @@ -1,90 +0,0 @@ - new TestProvider(), - 'testProvider2' => new TestProvider(), - ]; - $collection->setProviders($providers); - $this->assertEquals($providers, $collection->getProviders(), 'Unable to setup providers!'); - } - - /** - * @depends testSetGet - */ - public function testGetProviderById() - { - $collection = new Collection(); - - $providerId = 'testProviderId'; - $provider = new TestProvider(); - $providers = [ - $providerId => $provider - ]; - $collection->setProviders($providers); - - $this->assertEquals($provider, $collection->getProvider($providerId), 'Unable to get provider by id!'); - } - - /** - * @depends testGetProviderById - */ - public function testCreateProvider() - { - $collection = new Collection(); - - $providerId = 'testProviderId'; - $providerClassName = TestProvider::className(); - $providers = [ - $providerId => [ - 'class' => $providerClassName - ] - ]; - $collection->setProviders($providers); - - $provider = $collection->getProvider($providerId); - $this->assertTrue(is_object($provider), 'Unable to create provider by config!'); - $this->assertTrue(is_a($provider, $providerClassName), 'Provider has wrong class name!'); - } - - /** - * @depends testSetGet - */ - public function testHasProvider() - { - $collection = new Collection(); - - $providerName = 'testProviderName'; - $providers = [ - $providerName => [ - 'class' => 'TestProvider1' - ], - ]; - $collection->setProviders($providers); - - $this->assertTrue($collection->hasProvider($providerName), 'Existing provider check fails!'); - $this->assertFalse($collection->hasProvider('unExistingProviderName'), 'Not existing provider check fails!'); - } -} - -class TestProvider extends Object implements ProviderInterface -{ - use ProviderTrait; - - public function authenticate() {} -} \ No newline at end of file diff --git a/tests/unit/extensions/authclient/provider/ProviderTraitTest.php b/tests/unit/extensions/authclient/provider/ProviderTraitTest.php deleted file mode 100644 index 8289eb3..0000000 --- a/tests/unit/extensions/authclient/provider/ProviderTraitTest.php +++ /dev/null @@ -1,90 +0,0 @@ - [ - 'user' => [ - 'identityClass' => '\yii\web\IdentityInterface' - ], - 'request' => [ - 'hostInfo' => 'http://testdomain.com', - 'scriptUrl' => '/index.php', - ], - ] - ]; - $this->mockApplication($config, '\yii\web\Application'); - } - - public function testSetGet() - { - $provider = new Provider(); - - $id = 'test_service_id'; - $provider->setId($id); - $this->assertEquals($id, $provider->getId(), 'Unable to setup id!'); - - $successUrl = 'http://test.success.url'; - $provider->setSuccessUrl($successUrl); - $this->assertEquals($successUrl, $provider->getSuccessUrl(), 'Unable to setup success URL!'); - - $cancelUrl = 'http://test.cancel.url'; - $provider->setCancelUrl($cancelUrl); - $this->assertEquals($cancelUrl, $provider->getCancelUrl(), 'Unable to setup cancel URL!'); - } - - public function testGetDescriptiveData() - { - $provider = new Provider(); - - $this->assertNotEmpty($provider->getName(), 'Unable to get name!'); - $this->assertNotEmpty($provider->getTitle(), 'Unable to get title!'); - } - - /** - * @depends testSetGet - */ - public function testGetDefaultSuccessUrl() - { - $provider = new Provider(); - - $this->assertNotEmpty($provider->getSuccessUrl(), 'Unable to get default success URL!'); - } - - /** - * @depends testSetGet - */ - public function testGetDefaultCancelUrl() - { - $provider = new Provider(); - - $this->assertNotEmpty($provider->getSuccessUrl(), 'Unable to get default cancel URL!'); - } - - public function testRedirect() - { - $provider = new Provider(); - - $url = 'http://test.url'; - $response = $provider->redirect($url, true); - - $this->assertContains($url, $response->content); - } -} - -class Provider extends Object implements ProviderInterface -{ - use ProviderTrait; - - public function authenticate() {} -} \ No newline at end of file