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
9b715b05
Commit
9b715b05
authored
Jul 05, 2013
by
Bertrand Gauthier
Browse files
Storage d'authentification Db : tests unitaires ; légères améliorations.
parent
2d8dc9b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/UnicaenAuth/Authentication/Storage/Db.php
View file @
9b715b05
...
...
@@ -23,50 +23,59 @@ class Db extends \ZfcUser\Authentication\Storage\Db
*/
public
function
read
()
{
$id
=
$this
->
getStorage
()
->
read
();
/**
* 1ere tentative (mécanisme standard du module ZfcUser) :
*
* Recherche dans la base de données de l'utilisateur dont l'id correspond à ce qui
* est stoqué en session.
*
* NB: En cas de problème de connexion ou de service 'zfcuser_user_mapper' introuvable,
* cela signifie sans doute que l'application n'utilise pas de table des utilisateurs.
*/
try
{
$identity
=
parent
::
read
();
}
catch
(
PDOException
$pdoe
)
{
// throw new \UnicaenApp\Exception(
// "Erreur lors de la recherche de l'utilisateur $id dans la base de données : " . $pdoe->getMessage(),
// null,
// $pdoe);
$identity
=
null
;
}
catch
(
ServiceNotFoundException
$e
)
{
$identity
=
null
;
}
// si on obtient autre chose qu'un scalaire, l'utilisateur a déjà été
// recherché/trouvé dans la base de données
if
(
$identity
&&
!
is_scalar
(
$identity
))
{
return
$identity
;
}
/**
* 2e tentative :
*
* Recherche de l'utilisateur dont le supannAliasLogin correspond à ce qui
* est stoqué en session.
*
* NB: En cas de problème de connexion ou de service 'zfcuser_user_mapper' introuvable,
* cela signifie sans doute que l'application n'utilise pas de table des utilisateurs.
*/
$username
=
$this
->
getStorage
()
->
read
();
if
(
is_string
(
$username
))
{
try
{
$identity
=
$this
->
getMapper
()
->
findByUsername
(
$username
);
}
catch
(
\
PDOException
$pdoe
)
{
// throw new \UnicaenApp\Exception(
// "Erreur lors de la recherche de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(),
// null,
// $pdoe);
catch
(
PDOException
$pdoe
)
{
$identity
=
null
;
}
catch
(
ServiceNotFoundException
$e
)
{
$identity
=
null
;
}
}
if
(
$identity
)
{
$this
->
resolvedIdentity
=
$identity
;
}
else
{
}
else
{
$this
->
resolvedIdentity
=
null
;
}
return
$this
->
resolvedIdentity
;
}
}
}
\ No newline at end of file
tests/UnicaenAuthTest/Authentication/Storage/DbTest.php
0 → 100644
View file @
9b715b05
<?php
namespace
UnicaenAuthTest\Authentication\Storage
;
use
PDOException
;
use
PHPUnit_Framework_TestCase
;
use
UnicaenAuth\Authentication\Storage\Db
;
use
Zend\ServiceManager\Exception\ServiceNotFoundException
;
use
ZfcUser\Entity\User
;
/**
* Description of DbTest
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class
DbTest
extends
PHPUnit_Framework_TestCase
{
protected
$storage
;
protected
$mapper
;
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected
function
setUp
()
{
$this
->
mapper
=
$this
->
getMock
(
'ZfcUser\Mapper\User'
,
array
(
'findById'
,
'findByUsername'
));
$this
->
storage
=
new
Db
();
$this
->
storage
->
setMapper
(
$this
->
mapper
);
}
public
function
getException
()
{
return
array
(
array
(
new
PDOException
()),
array
(
new
ServiceNotFoundException
()),
);
}
/**
* @dataProvider getException
*/
public
function
testReadingReturnsNullIfFindByIdThrowsException
(
$exception
)
{
$this
->
mapper
->
expects
(
$this
->
once
())
->
method
(
'findById'
)
->
will
(
$this
->
throwException
(
$exception
));
$this
->
storage
->
getStorage
()
->
write
(
12
);
// id utilisateur
$result
=
$this
->
storage
->
read
();
$this
->
assertNull
(
$result
);
}
public
function
testReadingReturnsEntityIfUserFoundById
()
{
$entity
=
new
User
();
$this
->
mapper
->
expects
(
$this
->
once
())
->
method
(
'findById'
)
->
will
(
$this
->
returnValue
(
$entity
));
$this
->
storage
->
getStorage
()
->
write
(
12
);
// id utilisateur
$result
=
$this
->
storage
->
read
();
$this
->
assertSame
(
$entity
,
$result
);
}
/**
* @dataProvider getException
*/
public
function
testReadingReturnsNullIfFindByUsernameThrowsException
(
$exception
)
{
$this
->
mapper
->
expects
(
$this
->
once
())
->
method
(
'findById'
)
->
will
(
$this
->
returnValue
(
null
));
$this
->
mapper
->
expects
(
$this
->
once
())
->
method
(
'findByUsername'
)
->
will
(
$this
->
throwException
(
$exception
));
$this
->
storage
->
getStorage
()
->
write
(
'username'
);
// login utilisateur
$result
=
$this
->
storage
->
read
();
$this
->
assertNull
(
$result
);
}
public
function
testReadingReturnsEntityIfUserFoundByUsername
()
{
$entity
=
new
User
();
$this
->
mapper
->
expects
(
$this
->
once
())
->
method
(
'findById'
)
->
will
(
$this
->
returnValue
(
null
));
$this
->
mapper
->
expects
(
$this
->
once
())
->
method
(
'findByUsername'
)
->
will
(
$this
->
returnValue
(
$entity
));
$this
->
storage
->
getStorage
()
->
write
(
'bob'
);
// login utilisateur
$result
=
$this
->
storage
->
read
();
$this
->
assertSame
(
$entity
,
$result
);
}
}
\ 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