Commit 966d6b76 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Classe abstraite des aides de vue User* : tests unitaires + légères modifs.

Aide de vue UserConnection : tests unitaires + légères modifs.
parent ba77d6c2
...@@ -6,7 +6,7 @@ namespace UnicaenAuth\View\Helper; ...@@ -6,7 +6,7 @@ namespace UnicaenAuth\View\Helper;
* *
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/ */
class UserAbstract extends \Zend\View\Helper\AbstractHelper abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHelper
{ {
protected $authService; protected $authService;
...@@ -47,7 +47,7 @@ class UserAbstract extends \Zend\View\Helper\AbstractHelper ...@@ -47,7 +47,7 @@ class UserAbstract extends \Zend\View\Helper\AbstractHelper
* *
* @return mixed * @return mixed
*/ */
protected function getIdentity() public function getIdentity()
{ {
if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) { if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) {
return null; return null;
......
...@@ -38,25 +38,27 @@ class UserConnection extends UserAbstract ...@@ -38,25 +38,27 @@ class UserConnection extends UserAbstract
*/ */
protected function createConnectionLink() protected function createConnectionLink()
{ {
$urlLogin = $this->getView()->url('zfcuser/login');
$urlLogout = $this->getView()->url('zfcuser/logout');
$identity = $this->getIdentity(); $identity = $this->getIdentity();
$urlHelper = $this->getView()->plugin('url');
$template = '<a class="navbar-link user-connection" href="%s" title="%s">%s</a>'; $template = '<a class="navbar-link user-connection" href="%s" title="%s">%s</a>';
if (!$identity) { if (!$identity) {
$href = $urlLogin; $href = $urlHelper('zfcuser/login');
$lib = $this->getView()->translate("Connexion"); $lib = "Connexion";
$title = $this->getView()->translate("Affiche le formulaire d'authentification"); $title = "Affiche le formulaire d'authentification";
} }
else { else {
$href = $urlLogout; $href = $urlHelper('zfcuser/logout');
$lib = $this->getView()->translate("Déconnexion"); $lib = "Déconnexion";
$title = $this->getView()->translate("Supprime les informations de connexion"); $title = "Supprime les informations de connexion";
}
if ($this->getTranslator()) {
$lib = $this->getTranslator()->translate($lib, $this->getTranslatorTextDomain());
$title = $this->getTranslator()->translate($title, $this->getTranslatorTextDomain());
} }
$link = sprintf($template, $href, $title, $lib); $link = sprintf($template, $href, $title, $lib);
return $link; return $link;
} }
}
} \ No newline at end of file
<?php
namespace UnicaenAuthTest\View\Helper;
use PHPUnit_Framework_TestCase;
use UnicaenAuth\View\Helper\UserAbstract;
/**
* Description of AppConnectionTest
*
* @property UserAbstract $helper Description
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class UserAbstractTest extends PHPUnit_Framework_TestCase
{
protected $helper;
protected $authService;
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->authService = $this->getMock('Zend\Authentication\AuthenticationService', array('hasIdentity', 'getIdentity'));
$this->helper = $this->getMockForAbstractClass('UnicaenAuth\View\Helper\UserAbstract');
$this->helper->setAuthService($this->authService);
}
public function testCanSetAuthService()
{
$this->assertSame($this->authService, $this->helper->getAuthService());
}
public function testGettingIdentityReturnsNullIfNoAuthServiceAvailable()
{
$this->helper->setAuthService(null);
$this->assertNull($this->helper->getAuthService());
$this->assertNull($this->helper->getIdentity());
}
public function testGettingIdentityReturnsNullIfAuthServiceHasNoIdentity()
{
$this->authService->expects($this->once())
->method('hasIdentity')
->will($this->returnValue(false));
$this->assertNull($this->helper->getIdentity());
}
public function testGettingIdentityReturnsAuthServiceIdentity()
{
$this->authService->expects($this->once())
->method('hasIdentity')
->will($this->returnValue(true));
$this->authService->expects($this->once())
->method('getIdentity')
->will($this->returnValue($identity = 'Auth Service Identity'));
$this->assertEquals($identity, $this->helper->getIdentity());
}
}
\ No newline at end of file
<?php
namespace UnicaenAuthTest\View\Helper;
use UnicaenAppTest\View\Helper\AbstractTest;
use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
use UnicaenAuth\View\Helper\UserConnection;
use Zend\I18n\Translator\Translator;
/**
* Description of AppConnectionTest
*
* @property UserConnection $helper Description
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class UserConnectionTest extends AbstractTest
{
protected $helperClass = 'UnicaenAuth\View\Helper\UserConnection';
protected $renderer;
protected $authService;
protected $urlHelper;
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
parent::setUp();
$this->files = __DIR__ . '/_files';
$this->urlHelper = $this->getMock('Zend\View\Helper\Url', array('__invoke'));
$this->renderer = $this->getMock('Zend\View\Renderer\PhpRenderer', array('plugin'));
$this->renderer->expects($this->any())
->method('plugin')
->with('url')
->will($this->returnValue($this->urlHelper));
$this->authService = $this->getMock('Zend\Authentication\AuthenticationService', array('hasIdentity', 'getIdentity'));
$this->helper->setAuthService($this->authService)
->setView($this->renderer)
->setTranslator(new Translator());
}
public function testCanConstructWithAuthService()
{
$helper = new UserConnection($this->authService);
$this->assertSame($this->authService, $helper->getAuthService());
}
public function testEntryPointReturnsSelfInstance()
{
$this->assertSame($this->helper, $this->helper->__invoke());
}
public function testCanRenderLoginLinkIfNoIdentityAvailable()
{
$this->authService->expects($this->any())
->method('hasIdentity')
->will($this->returnValue(false));
$this->urlHelper
->expects($this->any())
->method('__invoke')
->with('zfcuser/login')
->will($this->returnValue('/appli/connexion'));
$this->assertEquals($this->getExpected('user_connection/login.phtml'), (string) $this->helper);
// traduction
$this->helper->setTranslator($this->_getTranslator());
$this->assertEquals($this->getExpected('user_connection/login-translated.phtml'), (string) $this->helper);
}
public function testCanRenderLogoutLinkIfIdentityAvailable()
{
$this->authService->expects($this->any())
->method('hasIdentity')
->will($this->returnValue(true));
$this->authService->expects($this->any())
->method('getIdentity')
->will($this->returnValue($identity = 'Auth Service Identity'));
$this->urlHelper
->expects($this->any())
->method('__invoke')
->with('zfcuser/logout')
->will($this->returnValue('/appli/deconnexion'));
$this->assertEquals($this->getExpected('user_connection/logout.phtml'), (string) $this->helper);
// traduction
$this->helper->setTranslator($this->_getTranslator());
$this->assertEquals($this->getExpected('user_connection/logout-translated.phtml'), (string) $this->helper);
}
/**
* Returns translator
*
* @return Translator
*/
protected function _getTranslator()
{
$loader = new ArrayTranslatorLoader();
$loader->translations = array(
"Connexion" => 'Login',
"Déconnexion" => 'Logout',
"Affiche le formulaire d'authentification" => 'Display auth form',
"Supprime les informations de connexion" => 'Reset auth info',
);
$translator = new Translator();
$translator->getPluginManager()->setService('default', $loader);
$translator->addTranslationFile('default', null);
return $translator;
}
}
\ No newline at end of file
<a class="navbar-link user-connection" href="/appli/connexion" title="Display auth form">Login</a>
\ No newline at end of file
<a class="navbar-link user-connection" href="/appli/connexion" title="Affiche le formulaire d'authentification">Connexion</a>
\ No newline at end of file
<a class="navbar-link user-connection" href="/appli/deconnexion" title="Reset auth info">Logout</a>
\ No newline at end of file
<a class="navbar-link user-connection" href="/appli/deconnexion" title="Supprime les informations de connexion">Déconnexion</a>
\ 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