Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
lib
unicaen
auth
Commits
966d6b76
Commit
966d6b76
authored
Jul 09, 2013
by
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
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/UnicaenAuth/View/Helper/UserAbstract.php
View file @
966d6b76
...
...
@@ -6,7 +6,7 @@ namespace UnicaenAuth\View\Helper;
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class
UserAbstract
extends
\
Zend\View\Helper\AbstractHelper
abstract
class
UserAbstract
extends
\
Zend\
I18n\
View\Helper\Abstract
Translator
Helper
{
protected
$authService
;
...
...
@@ -47,7 +47,7 @@ class UserAbstract extends \Zend\View\Helper\AbstractHelper
*
* @return mixed
*/
p
rotected
function
getIdentity
()
p
ublic
function
getIdentity
()
{
if
(
!
$this
->
getAuthService
()
||
!
$this
->
getAuthService
()
->
hasIdentity
())
{
return
null
;
...
...
src/UnicaenAuth/View/Helper/UserConnection.php
View file @
966d6b76
...
...
@@ -38,25 +38,27 @@ class UserConnection extends UserAbstract
*/
protected
function
createConnectionLink
()
{
$urlLogin
=
$this
->
getView
()
->
url
(
'zfcuser/login'
);
$urlLogout
=
$this
->
getView
()
->
url
(
'zfcuser/logout'
);
$identity
=
$this
->
getIdentity
();
$urlHelper
=
$this
->
getView
()
->
plugin
(
'url'
);
$template
=
'<a class="navbar-link user-connection" href="%s" title="%s">%s</a>'
;
if
(
!
$identity
)
{
$href
=
$url
L
ogin
;
$lib
=
$this
->
getView
()
->
translate
(
"Connexion"
)
;
$title
=
$this
->
getView
()
->
translate
(
"Affiche le formulaire d'authentification"
)
;
$href
=
$url
Helper
(
'zfcuser/l
ogin
'
)
;
$lib
=
"Connexion"
;
$title
=
"Affiche le formulaire d'authentification"
;
}
else
{
$href
=
$urlLogout
;
$lib
=
$this
->
getView
()
->
translate
(
"Déconnexion"
);
$title
=
$this
->
getView
()
->
translate
(
"Supprime les informations de connexion"
);
$href
=
$urlHelper
(
'zfcuser/logout'
);
$lib
=
"Dé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
);
return
$link
;
}
}
}
\ No newline at end of file
tests/UnicaenAuthTest/View/Helper/UserAbstractTest.php
0 → 100644
View file @
966d6b76
<?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
tests/UnicaenAuthTest/View/Helper/UserConnectionTest.php
0 → 100644
View file @
966d6b76
<?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
tests/UnicaenAuthTest/View/Helper/_files/expected/user_connection/login-translated.phtml
0 → 100644
View file @
966d6b76
<a
class=
"navbar-link user-connection"
href=
"/appli/connexion"
title=
"Display auth form"
>
Login
</a>
\ No newline at end of file
tests/UnicaenAuthTest/View/Helper/_files/expected/user_connection/login.phtml
0 → 100644
View file @
966d6b76
<a
class=
"navbar-link user-connection"
href=
"/appli/connexion"
title=
"Affiche le formulaire d'authentification"
>
Connexion
</a>
\ No newline at end of file
tests/UnicaenAuthTest/View/Helper/_files/expected/user_connection/logout-translated.phtml
0 → 100644
View file @
966d6b76
<a
class=
"navbar-link user-connection"
href=
"/appli/deconnexion"
title=
"Reset auth info"
>
Logout
</a>
\ No newline at end of file
tests/UnicaenAuthTest/View/Helper/_files/expected/user_connection/logout.phtml
0 → 100644
View file @
966d6b76
<a
class=
"navbar-link user-connection"
href=
"/appli/deconnexion"
title=
"Supprime les informations de connexion"
>
Déconnexion
</a>
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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