Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
691cc14c
Commit
691cc14c
authored
Dec 12, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"\yii\authclient\provider\Collection" added.
parent
6171287f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
197 additions
and
0 deletions
+197
-0
Collection.php
extensions/yii/authclient/provider/Collection.php
+106
-0
CollectionTest.php
tests/unit/extensions/authclient/provider/CollectionTest.php
+91
-0
No files found.
extensions/yii/authclient/provider/Collection.php
0 → 100644
View file @
691cc14c
<?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
,
array
$config
)
{
$config
[
'id'
]
=
$id
;
return
Yii
::
createObject
(
$config
);
}
}
\ No newline at end of file
tests/unit/extensions/authclient/provider/CollectionTest.php
0 → 100644
View file @
691cc14c
<?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
()
{}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment