Commit 776a9250 by Paul Klimov

"yii\authclient" structure refactored.

parent 1ef606da
...@@ -159,7 +159,7 @@ class AuthAction extends Action ...@@ -159,7 +159,7 @@ class AuthAction extends Action
'url' => $url, 'url' => $url,
'enforceRedirect' => $enforceRedirect, '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 = Yii::$app->getResponse();
$response->content = Yii::$app->getView()->renderFile($viewFile, $viewData); $response->content = Yii::$app->getView()->renderFile($viewFile, $viewData);
...@@ -208,7 +208,7 @@ class AuthAction extends Action ...@@ -208,7 +208,7 @@ class AuthAction extends Action
'id' => $provider->identity 'id' => $provider->identity
); );
$rawAttributes = $provider->getAttributes(); $rawAttributes = $provider->getAttributes();
foreach ($provider->getRequiredAttributes() as $openIdAttributeName) { foreach ($provider->requiredAttributes as $openIdAttributeName) {
if (isset($rawAttributes[$openIdAttributeName])) { if (isset($rawAttributes[$openIdAttributeName])) {
$attributes[$openIdAttributeName] = $rawAttributes[$openIdAttributeName]; $attributes[$openIdAttributeName] = $rawAttributes[$openIdAttributeName];
} else { } else {
...@@ -216,7 +216,6 @@ class AuthAction extends Action ...@@ -216,7 +216,6 @@ class AuthAction extends Action
} }
} }
$provider->setAttributes($attributes); $provider->setAttributes($attributes);
$provider->isAuthenticated = true;
return $this->authenticateSuccess($provider); return $this->authenticateSuccess($provider);
} else { } else {
throw new Exception('Unable to complete the authentication because the required data was not received.'); throw new Exception('Unable to complete the authentication because the required data was not received.');
...@@ -231,10 +230,6 @@ class AuthAction extends Action ...@@ -231,10 +230,6 @@ class AuthAction extends Action
} }
} else { } else {
$provider->identity = $provider->authUrl; // Setting identifier $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(); $request = Yii::$app->getRequest();
$provider->realm = $request->getHostInfo(); $provider->realm = $request->getHostInfo();
$provider->returnUrl = $provider->realm . $request->getUrl(); // getting return URL $provider->returnUrl = $provider->realm . $request->getUrl(); // getting return URL
...@@ -270,7 +265,6 @@ class AuthAction extends Action ...@@ -270,7 +265,6 @@ class AuthAction extends Action
} else { } else {
// Upgrade to access token. // Upgrade to access token.
$accessToken = $provider->fetchAccessToken(); $accessToken = $provider->fetchAccessToken();
$provider->isAuthenticated = true;
return $this->authenticateSuccess($provider); return $this->authenticateSuccess($provider);
} }
} }
...@@ -304,7 +298,6 @@ class AuthAction extends Action ...@@ -304,7 +298,6 @@ class AuthAction extends Action
$code = $_GET['code']; $code = $_GET['code'];
$token = $provider->fetchAccessToken($code); $token = $provider->fetchAccessToken($code);
if (!empty($token)) { if (!empty($token)) {
$provider->isAuthenticated = true;
return $this->authenticateSuccess($provider); return $this->authenticateSuccess($provider);
} else { } else {
return $this->redirectCancel(); return $this->redirectCancel();
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\authclient\provider; namespace yii\authclient;
/** /**
* Class ProviderInterface * Class ProviderInterface
...@@ -13,7 +13,7 @@ namespace yii\authclient\provider; ...@@ -13,7 +13,7 @@ namespace yii\authclient\provider;
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
*/ */
interface ProviderInterface interface ClientInterface
{ {
/** /**
* @param string $id service id. * @param string $id service id.
...@@ -46,28 +46,7 @@ interface ProviderInterface ...@@ -46,28 +46,7 @@ interface ProviderInterface
public function setTitle($title); public function setTitle($title);
/** /**
* @param string $url successful URL. * @return array list of user attributes
*/ */
public function setSuccessUrl($url); public function getUserAttributes();
/**
* @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
...@@ -5,20 +5,21 @@ ...@@ -5,20 +5,21 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\authclient\provider; namespace yii\authclient;
use Yii; use Yii;
use yii\base\NotSupportedException;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
/** /**
* Class ProviderTrait * Class ProviderTrait
* *
* @see ProviderInterface * @see ClientInterface
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
*/ */
trait ProviderTrait trait ClientTrait
{ {
/** /**
* @var string service id. * @var string service id.
...@@ -35,13 +36,9 @@ trait ProviderTrait ...@@ -35,13 +36,9 @@ trait ProviderTrait
*/ */
private $_title; private $_title;
/** /**
* @var string the redirect url after successful authorization. * @var array authenticated user attributes.
*/ */
private $_successUrl = ''; private $_userAttributes;
/**
* @var string the redirect url after unsuccessful authorization (e.g. user canceled).
*/
private $_cancelUrl = '';
/** /**
* @param string $id service id. * @param string $id service id.
...@@ -101,41 +98,22 @@ trait ProviderTrait ...@@ -101,41 +98,22 @@ trait ProviderTrait
} }
/** /**
* @param string $url successful URL. * @return array list of user attributes
*/ */
public function setSuccessUrl($url) public function getUserAttributes()
{ {
$this->_successUrl = $url; if ($this->_userAttributes === null) {
} $this->_userAttributes = $this->initUserAttributes();
/**
* @return string successful URL.
*/
public function getSuccessUrl()
{
if (empty($this->_successUrl)) {
$this->_successUrl = $this->defaultSuccessUrl();
} }
return $this->_successUrl; return $this->_userAttributes;
}
/**
* @param string $url cancel URL.
*/
public function setCancelUrl($url)
{
$this->_cancelUrl = $url;
} }
/** /**
* @return string cancel URL. * @param array $userAttributes list of user attributes
*/ */
public function getCancelUrl() public function setUserAttributes(array $userAttributes)
{ {
if (empty($this->_cancelUrl)) { $this->_userAttributes = $userAttributes;
$this->_cancelUrl = $this->defaultCancelUrl();
}
return $this->_cancelUrl;
} }
/** /**
...@@ -157,65 +135,11 @@ trait ProviderTrait ...@@ -157,65 +135,11 @@ trait ProviderTrait
} }
/** /**
* Creates default {@link successUrl} value. * Initializes authenticated user attributes.
* @return string success URL value. * @return array auth user attributes.
*/
protected function defaultSuccessUrl()
{
return Yii::$app->getUser()->getReturnUrl();
}
/**
* Creates default {@link cancelUrl} value.
* @return string cancel URL value.
*/ */
protected function defaultCancelUrl() protected function initUserAttributes()
{ {
return Yii::$app->getRequest()->getAbsoluteUrl(); throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
}
/**
* 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
...@@ -5,27 +5,27 @@ ...@@ -5,27 +5,27 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\authclient\provider; namespace yii\authclient;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use Yii; use Yii;
/** /**
* Collection is a storage for all auth providers in the application. * Collection is a storage for all auth clients in the application.
* *
* Example application configuration: * Example application configuration:
* *
* ~~~ * ~~~
* 'components' => [ * 'components' => [
* 'auth' => [ * 'auth' => [
* 'class' => 'yii\authclient\provider\Collection', * 'class' => 'yii\authclient\Collection',
* 'providers' => [ * 'clients' => [
* 'google' => [ * 'google' => [
* 'class' => 'yii\authclient\provider\GoogleOpenId' * 'class' => 'yii\authclient\clients\GoogleOpenId'
* ], * ],
* 'facebook' => [ * 'facebook' => [
* 'class' => 'yii\authclient\provider\Facebook', * 'class' => 'yii\authclient\clients\Facebook',
* 'clientId' => 'facebook_client_id', * 'clientId' => 'facebook_client_id',
* 'clientSecret' => 'facebook_client_secret', * 'clientSecret' => 'facebook_client_secret',
* ], * ],
...@@ -35,69 +35,71 @@ use Yii; ...@@ -35,69 +35,71 @@ use Yii;
* ] * ]
* ~~~ * ~~~
* *
* @property array $clients list of Auth clients with their configuration in format: 'clientId' => [...]
*
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Collection extends Component class Collection extends Component
{ {
/** /**
* @var array list of Auth providers with their configuration in format: 'providerId' => [...] * @var array list of Auth clients with their configuration in format: 'clientId' => [...]
*/ */
private $_providers = []; private $_clients = [];
/** /**
* @param array $providers list of auth providers * @param array $clients list of auth clients
*/ */
public function setProviders(array $providers) public function setClients(array $clients)
{ {
$this->_providers = $providers; $this->_clients = $clients;
} }
/** /**
* @return ProviderInterface[] list of auth providers. * @return ClientInterface[] list of auth clients.
*/ */
public function getProviders() public function getClients()
{ {
$providers = []; $clients = [];
foreach ($this->_providers as $id => $provider) { foreach ($this->_clients as $id => $client) {
$providers[$id] = $this->getProvider($id); $clients[$id] = $this->getClient($id);
} }
return $providers; return $clients;
} }
/** /**
* @param string $id service id. * @param string $id service id.
* @return ProviderInterface auth service instance. * @return ClientInterface auth client instance.
* @throws InvalidParamException on non existing provider request. * @throws InvalidParamException on non existing client request.
*/ */
public function getProvider($id) public function getClient($id)
{ {
if (!array_key_exists($id, $this->_providers)) { if (!array_key_exists($id, $this->_clients)) {
throw new InvalidParamException("Unknown auth provider '{$id}'."); throw new InvalidParamException("Unknown auth client '{$id}'.");
} }
if (!is_object($this->_providers[$id])) { if (!is_object($this->_clients[$id])) {
$this->_providers[$id] = $this->createProvider($id, $this->_providers[$id]); $this->_clients[$id] = $this->createClient($id, $this->_clients[$id]);
} }
return $this->_providers[$id]; return $this->_clients[$id];
} }
/** /**
* Checks if provider exists in the hub. * Checks if client exists in the hub.
* @param string $id provider id. * @param string $id client id.
* @return boolean whether provider exist. * @return boolean whether client exist.
*/ */
public function hasProvider($id) public function hasClient($id)
{ {
return array_key_exists($id, $this->_providers); return array_key_exists($id, $this->_clients);
} }
/** /**
* Creates auth provider instance from its array configuration. * Creates auth client instance from its array configuration.
* @param string $id auth provider id. * @param string $id auth client id.
* @param array $config auth provider instance configuration. * @param array $config auth client instance configuration.
* @return ProviderInterface auth provider instance. * @return ClientInterface auth client instance.
*/ */
protected function createProvider($id, $config) protected function createClient($id, $config)
{ {
$config['id'] = $id; $config['id'] = $id;
return Yii::createObject($config); return Yii::createObject($config);
......
...@@ -27,7 +27,10 @@ use yii\base\NotSupportedException; ...@@ -27,7 +27,10 @@ use yii\base\NotSupportedException;
*/ */
class OpenId extends Component class OpenId extends Component
{ {
public $required = []; /**
* @var array list of attributes, which should be requested from server.
*/
public $requiredAttributes = [];
public $optional = []; public $optional = [];
public $verify_peer; public $verify_peer;
public $capath; public $capath;
...@@ -501,9 +504,9 @@ class OpenId extends Component ...@@ -501,9 +504,9 @@ class OpenId extends Component
# That's because it's fully backwards compatibile with 1.0, and some providers # 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 # 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'; $params['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1';
if ($this->required) { if ($this->requiredAttributes) {
$params['openid.sreg.required'] = []; $params['openid.sreg.required'] = [];
foreach ($this->required as $required) { foreach ($this->requiredAttributes as $required) {
if (!isset(self::$axToSregMap[$required])) { if (!isset(self::$axToSregMap[$required])) {
continue; continue;
} }
...@@ -528,7 +531,7 @@ class OpenId extends Component ...@@ -528,7 +531,7 @@ class OpenId extends Component
protected function axParams() protected function axParams()
{ {
$params = []; $params = [];
if ($this->required || $this->optional) { if ($this->requiredAttributes || $this->optional) {
$params['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0'; $params['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0';
$params['openid.ax.mode'] = 'fetch_request'; $params['openid.ax.mode'] = 'fetch_request';
$this->aliases = []; $this->aliases = [];
......
<?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;
}
}
\ No newline at end of file
<?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;
}
}
\ No newline at end of file
<?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;
}
}
\ No newline at end of file
<?php <?php
namespace yiiunit\extensions\authclient\provider; namespace yiiunit\extensions\authclient;
use yii\authclient\ClientInterface;
use yii\authclient\provider\ProviderInterface; use yii\authclient\ClientTrait;
use yii\authclient\provider\ProviderTrait;
use yii\base\Object; use yii\base\Object;
use yiiunit\extensions\authclient\TestCase;
class ProviderTraitTest extends TestCase class ClientTraitTest extends TestCase
{ {
protected function setUp() protected function setUp()
{ {
...@@ -28,63 +26,33 @@ class ProviderTraitTest extends TestCase ...@@ -28,63 +26,33 @@ class ProviderTraitTest extends TestCase
public function testSetGet() public function testSetGet()
{ {
$provider = new Provider(); $provider = new Client();
$id = 'test_service_id'; $id = 'test_id';
$provider->setId($id); $provider->setId($id);
$this->assertEquals($id, $provider->getId(), 'Unable to setup id!'); $this->assertEquals($id, $provider->getId(), 'Unable to setup id!');
$successUrl = 'http://test.success.url'; $name = 'test_name';
$provider->setSuccessUrl($successUrl); $provider->setName($name);
$this->assertEquals($successUrl, $provider->getSuccessUrl(), 'Unable to setup success URL!'); $this->assertEquals($name, $provider->getName(), 'Unable to setup name!');
$cancelUrl = 'http://test.cancel.url'; $title = 'test_title';
$provider->setCancelUrl($cancelUrl); $provider->setTitle($title);
$this->assertEquals($cancelUrl, $provider->getCancelUrl(), 'Unable to setup cancel URL!'); $this->assertEquals($title, $provider->getTitle(), 'Unable to setup title!');
} }
public function testGetDescriptiveData() public function testGetDescriptiveData()
{ {
$provider = new Provider(); $provider = new Client();
$this->assertNotEmpty($provider->getName(), 'Unable to get name!'); $this->assertNotEmpty($provider->getName(), 'Unable to get name!');
$this->assertNotEmpty($provider->getTitle(), 'Unable to get title!'); $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 class Client extends Object implements ClientInterface
{ {
use ProviderTrait; use ClientTrait;
public function authenticate() {} public function authenticate() {}
} }
\ No newline at end of file
<?php <?php
namespace yiiunit\extensions\authclient\provider; namespace yiiunit\extensions\authclient;
use yii\authclient\provider\Collection; use yii\authclient\Collection;
use yii\authclient\provider\ProviderInterface; use yii\authclient\ClientInterface;
use yii\authclient\provider\ProviderTrait; use yii\authclient\ClientTrait;
use yii\base\Object; use yii\base\Object;
use yiiunit\extensions\authclient\TestCase; use yiiunit\extensions\authclient\TestCase;
...@@ -16,12 +16,12 @@ class CollectionTest extends TestCase ...@@ -16,12 +16,12 @@ class CollectionTest extends TestCase
{ {
$collection = new Collection(); $collection = new Collection();
$providers = [ $clients = [
'testProvider1' => new TestProvider(), 'testClient1' => new TestClient(),
'testProvider2' => new TestProvider(), 'testClient2' => new TestClient(),
]; ];
$collection->setProviders($providers); $collection->setClients($clients);
$this->assertEquals($providers, $collection->getProviders(), 'Unable to setup providers!'); $this->assertEquals($clients, $collection->getClients(), 'Unable to setup clients!');
} }
/** /**
...@@ -31,14 +31,14 @@ class CollectionTest extends TestCase ...@@ -31,14 +31,14 @@ class CollectionTest extends TestCase
{ {
$collection = new Collection(); $collection = new Collection();
$providerId = 'testProviderId'; $clientId = 'testClientId';
$provider = new TestProvider(); $client = new TestClient();
$providers = [ $clients = [
$providerId => $provider $clientId => $client
]; ];
$collection->setProviders($providers); $collection->setClients($clients);
$this->assertEquals($provider, $collection->getProvider($providerId), 'Unable to get provider by id!'); $this->assertEquals($client, $collection->getClient($clientId), 'Unable to get client by id!');
} }
/** /**
...@@ -48,18 +48,18 @@ class CollectionTest extends TestCase ...@@ -48,18 +48,18 @@ class CollectionTest extends TestCase
{ {
$collection = new Collection(); $collection = new Collection();
$providerId = 'testProviderId'; $clientId = 'testClientId';
$providerClassName = TestProvider::className(); $clientClassName = TestClient::className();
$providers = [ $clients = [
$providerId => [ $clientId => [
'class' => $providerClassName 'class' => $clientClassName
] ]
]; ];
$collection->setProviders($providers); $collection->setClients($clients);
$provider = $collection->getProvider($providerId); $provider = $collection->getClient($clientId);
$this->assertTrue(is_object($provider), 'Unable to create provider by config!'); $this->assertTrue(is_object($provider), 'Unable to create client by config!');
$this->assertTrue(is_a($provider, $providerClassName), 'Provider has wrong class name!'); $this->assertTrue(is_a($provider, $clientClassName), 'Client has wrong class name!');
} }
/** /**
...@@ -69,22 +69,22 @@ class CollectionTest extends TestCase ...@@ -69,22 +69,22 @@ class CollectionTest extends TestCase
{ {
$collection = new Collection(); $collection = new Collection();
$providerName = 'testProviderName'; $clientName = 'testClientName';
$providers = [ $clients = [
$providerName => [ $clientName => [
'class' => 'TestProvider1' 'class' => 'TestClient1'
], ],
]; ];
$collection->setProviders($providers); $collection->setClients($clients);
$this->assertTrue($collection->hasProvider($providerName), 'Existing provider check fails!'); $this->assertTrue($collection->hasClient($clientName), 'Existing client check fails!');
$this->assertFalse($collection->hasProvider('unExistingProviderName'), 'Not existing provider check fails!'); $this->assertFalse($collection->hasClient('unExistingClientName'), 'Not existing client check fails!');
} }
} }
class TestProvider extends Object implements ProviderInterface class TestClient extends Object implements ClientInterface
{ {
use ProviderTrait; use ClientTrait;
public function authenticate() {} public function authenticate() {}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment