Browse Source

"yii\authclient" structure refactored.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
776a92502f
  1. 11
      extensions/yii/authclient/AuthAction.php
  2. 29
      extensions/yii/authclient/ClientInterface.php
  3. 145
      extensions/yii/authclient/ClientTrait.php
  4. 107
      extensions/yii/authclient/Collection.php
  5. 11
      extensions/yii/authclient/OpenId.php
  6. 105
      extensions/yii/authclient/provider/Collection.php
  7. 51
      extensions/yii/authclient/provider/OAuth1.php
  8. 58
      extensions/yii/authclient/provider/OAuth2.php
  9. 74
      extensions/yii/authclient/provider/OpenId.php
  10. 221
      extensions/yii/authclient/provider/ProviderTrait.php
  11. 0
      extensions/yii/authclient/views/redirect.php
  12. 58
      tests/unit/extensions/authclient/ClientTraitTest.php
  13. 90
      tests/unit/extensions/authclient/CollectionTest.php
  14. 90
      tests/unit/extensions/authclient/provider/CollectionTest.php
  15. 90
      tests/unit/extensions/authclient/provider/ProviderTraitTest.php

11
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();

29
extensions/yii/authclient/provider/ProviderInterface.php → extensions/yii/authclient/ClientInterface.php

@ -5,7 +5,7 @@
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\provider;
namespace yii\authclient;
/**
* Class ProviderInterface
@ -13,7 +13,7 @@ namespace yii\authclient\provider;
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
interface ProviderInterface
interface ClientInterface
{
/**
* @param string $id service id.
@ -46,28 +46,7 @@ interface ProviderInterface
public function setTitle($title);
/**
* @param string $url successful URL.
* @return array list of user attributes
*/
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();
public function getUserAttributes();
}

145
extensions/yii/authclient/ClientTrait.php

@ -0,0 +1,145 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient;
use Yii;
use yii\base\NotSupportedException;
use yii\helpers\StringHelper;
/**
* Class ProviderTrait
*
* @see ClientInterface
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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.');
}
}

107
extensions/yii/authclient/Collection.php

@ -0,0 +1,107 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient;
use yii\base\Component;
use yii\base\InvalidParamException;
use Yii;
/**
* Collection is a storage for all auth clients in the application.
*
* Example application configuration:
*
* ~~~
* 'components' => [
* '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 <klimov.paul@gmail.com>
* @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);
}
}

11
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 = [];

105
extensions/yii/authclient/provider/Collection.php

@ -1,105 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\provider;
use yii\base\Component;
use yii\base\InvalidParamException;
use Yii;
/**
* Collection is a storage for all auth providers in the application.
*
* Example application configuration:
*
* ~~~
* 'components' => [
* '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 <klimov.paul@gmail.com>
* @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);
}
}

51
extensions/yii/authclient/provider/OAuth1.php

@ -1,51 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\provider;
use Yii;
/**
* Class OAuth1
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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;
}
}

58
extensions/yii/authclient/provider/OAuth2.php

@ -1,58 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\provider;
use Yii;
use yii\base\Exception;
/**
* Class OAuth2
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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;
}
}

74
extensions/yii/authclient/provider/OpenId.php

@ -1,74 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\provider;
use Yii;
use yii\base\Exception;
use yii\web\HttpException;
/**
* Class OpenId
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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;
}
}

221
extensions/yii/authclient/provider/ProviderTrait.php

@ -1,221 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient\provider;
use Yii;
use yii\helpers\StringHelper;
/**
* Class ProviderTrait
*
* @see ProviderInterface
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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);
}
}

0
extensions/yii/authclient/provider/views/redirect.php → extensions/yii/authclient/views/redirect.php

58
tests/unit/extensions/authclient/ClientTraitTest.php

@ -0,0 +1,58 @@
<?php
namespace yiiunit\extensions\authclient;
use yii\authclient\ClientInterface;
use yii\authclient\ClientTrait;
use yii\base\Object;
class ClientTraitTest extends TestCase
{
protected function setUp()
{
$config = [
'components' => [
'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() {}
}

90
tests/unit/extensions/authclient/CollectionTest.php

@ -0,0 +1,90 @@
<?php
namespace yiiunit\extensions\authclient;
use yii\authclient\Collection;
use yii\authclient\ClientInterface;
use yii\authclient\ClientTrait;
use yii\base\Object;
use yiiunit\extensions\authclient\TestCase;
class CollectionTest extends TestCase
{
// Tests :
public function testSetGet()
{
$collection = new Collection();
$clients = [
'testClient1' => 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() {}
}

90
tests/unit/extensions/authclient/provider/CollectionTest.php

@ -1,90 +0,0 @@
<?php
namespace yiiunit\extensions\authclient\provider;
use yii\authclient\provider\Collection;
use yii\authclient\provider\ProviderInterface;
use yii\authclient\provider\ProviderTrait;
use yii\base\Object;
use yiiunit\extensions\authclient\TestCase;
class CollectionTest extends TestCase
{
// Tests :
public function testSetGet()
{
$collection = new Collection();
$providers = [
'testProvider1' => 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() {}
}

90
tests/unit/extensions/authclient/provider/ProviderTraitTest.php

@ -1,90 +0,0 @@
<?php
namespace yiiunit\extensions\authclient\provider;
use yii\authclient\provider\ProviderInterface;
use yii\authclient\provider\ProviderTrait;
use yii\base\Object;
use yiiunit\extensions\authclient\TestCase;
class ProviderTraitTest extends TestCase
{
protected function setUp()
{
$config = [
'components' => [
'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() {}
}
Loading…
Cancel
Save