Commit 6100facd authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Tests unitaires et améliorations/refactorisations.

parent 9b578467
...@@ -53,6 +53,20 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel ...@@ -53,6 +53,20 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel
return null; return null;
} }
return $this->getAuthService()->getIdentity(); $identity = $this->getAuthService()->getIdentity();
if (is_array($identity)) {
if (isset($identity['db'])) {
$identity = $identity['db'];
}
elseif (isset($identity['ldap'])) {
$identity = $identity['ldap'];
}
else {
throw new \InvalidArgumentException("Données d'identité invalides.");
}
}
return $identity;
} }
} }
...@@ -60,18 +60,6 @@ class UserStatus extends UserAbstract ...@@ -60,18 +60,6 @@ class UserStatus extends UserAbstract
protected function createStatusContainer() protected function createStatusContainer()
{ {
if (($identity = $this->getIdentity())) { if (($identity = $this->getIdentity())) {
if (is_array($identity)) {
if (isset($identity['db'])) {
$identity = $identity['db'];
}
elseif (isset($identity['ldap'])) {
$identity = $identity['ldap'];
}
else {
throw new \InvalidArgumentException("Données d'identité invalides.");
}
}
if (method_exists($identity, '__toString')) { if (method_exists($identity, '__toString')) {
$name = (string) $identity; $name = (string) $identity;
} }
......
<?php
namespace UnicaenAuthTest\Provider\Role;
use PHPUnit_Framework_TestCase;
use UnicaenAuth\Provider\Role\Db;
use BjyAuthorize\Provider\Role\ObjectRepositoryProvider;
/**
* Description of DbTest
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class DbTest extends PHPUnit_Framework_TestCase
{
protected $provider;
protected $objectRepository;
protected function setUp()
{
$this->objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository', array());
$this->provider = new Db($this->objectRepository);
}
public function testGettingRolesReturnsParentClassRoles()
{
ObjectRepositoryProvider::$throwException = false;
$this->assertEquals(array('Role 1', 'Role 2'), $this->provider->getRoles());
}
public function testException()
{
ObjectRepositoryProvider::$throwException = true;
$this->assertEquals(array(), $this->provider->getRoles());
}
}
namespace BjyAuthorize\Provider\Role;
use Doctrine\Common\Persistence\ObjectRepository;
class ObjectRepositoryProvider implements ProviderInterface
{
public static $throwException = false;
public function __construct(ObjectRepository $objectRepository)
{
}
public function getRoles()
{
if (self::$throwException) {
throw new \PDOException("No db connection!");
}
else {
return array('Role 1', 'Role 2');
}
}
}
\ No newline at end of file
...@@ -57,4 +57,53 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase ...@@ -57,4 +57,53 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase
->will($this->returnValue($identity = 'Auth Service Identity')); ->will($this->returnValue($identity = 'Auth Service Identity'));
$this->assertEquals($identity, $this->helper->getIdentity()); $this->assertEquals($identity, $this->helper->getIdentity());
} }
public function provideValidArrayIdentity()
{
return array(
array(array('db' => 'Db Identity'), 'Db Identity'),
array(array('ldap' => 'Ldap Identity'), 'Ldap Identity'),
array(array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), 'Db Identity'),
);
}
/**
* @dataProvider provideValidArrayIdentity
* @param array $identity
* @param string $expected
*/
public function testGettingIdentityReturnsAuthServiceIdentityFromValidArrayIdentity($identity, $expected)
{
$this->authService->expects($this->once())
->method('hasIdentity')
->will($this->returnValue(true));
$this->authService->expects($this->once())
->method('getIdentity')
->will($this->returnValue($identity));
$this->assertEquals($expected, $this->helper->getIdentity());
}
public function provideInvalidArrayIdentity()
{
return array(
array(array()),
array(array('other' => 'Other Identity')),
);
}
/**
* @dataProvider provideInvalidArrayIdentity
* @expectedException \InvalidArgumentException
* @param array $identity
*/
public function testGettingIdentityThrowsExceptionFromInvalidArrayIdentity($identity)
{
$this->authService->expects($this->once())
->method('hasIdentity')
->will($this->returnValue(true));
$this->authService->expects($this->once())
->method('getIdentity')
->will($this->returnValue($identity));
$this->helper->getIdentity();
}
} }
\ No newline at end of file
...@@ -169,7 +169,7 @@ class UserStatusTest extends AbstractTest ...@@ -169,7 +169,7 @@ class UserStatusTest extends AbstractTest
{ {
$loader = new ArrayTranslatorLoader(); $loader = new ArrayTranslatorLoader();
$loader->translations = array( $loader->translations = array(
"Aucun" => "None", "Vous n'êtes pas connecté(e)" => "None",
); );
$translator = new Translator(); $translator = new Translator();
......
<strong>Aucun</strong> | UserConnection Helper Markup <strong>Vous n'êtes pas connecté(e)</strong> | UserConnection Helper Markup
\ No newline at end of file \ No newline at end of file
<strong>Aucun</strong> <strong>Vous n'êtes pas connecté(e)</strong>
\ No newline at end of file \ 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