Skip to content
Snippets Groups Projects
Commit 431bbc99 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Quelques corrections de tests unitaires

parent acd80024
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ class Bootstrap ...@@ -29,7 +29,7 @@ class Bootstrap
static::initAutoloader(); static::initAutoloader();
static::$serviceManager = new ServiceManager(new ServiceManagerConfig()); static::$serviceManager = new ServiceManager([]);
} }
public static function getServiceManager() public static function getServiceManager()
......
...@@ -2,11 +2,14 @@ ...@@ -2,11 +2,14 @@
namespace UnicaenAuthTest\Authentication\Adapter; namespace UnicaenAuthTest\Authentication\Adapter;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use UnicaenApp\Mapper\Ldap\People;
use UnicaenAuth\Authentication\Adapter\AbstractFactory; use UnicaenAuth\Authentication\Adapter\AbstractFactory;
use UnicaenAuth\Service\User; use UnicaenAuth\Service\User;
use Zend\EventManager\EventManager; use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface; use Zend\EventManager\EventManagerAwareInterface;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
use UnicaenApp\Exception\LogicException;
use ZfcUser\Options\ModuleOptions;
/** /**
* Description of AbstractFactoryTest * Description of AbstractFactoryTest
...@@ -62,7 +65,7 @@ class AbstractFactoryTest extends PHPUnit_Framework_TestCase ...@@ -62,7 +65,7 @@ class AbstractFactoryTest extends PHPUnit_Framework_TestCase
/** /**
* @dataProvider getInvalidServiceClassName * @dataProvider getInvalidServiceClassName
* @expectedException \UnicaenApp\Exception\RuntimeException * @expectedException \UnicaenApp\Exception\LogicException
* @param string $serviceClassName * @param string $serviceClassName
*/ */
public function testCreateServiceWithNameThrowsExceptionIfInvalidServiceSpecified($serviceClassName) public function testCreateServiceWithNameThrowsExceptionIfInvalidServiceSpecified($serviceClassName)
...@@ -85,9 +88,21 @@ class AbstractFactoryTest extends PHPUnit_Framework_TestCase ...@@ -85,9 +88,21 @@ class AbstractFactoryTest extends PHPUnit_Framework_TestCase
if ('unicaen-auth_user_service' === $serviceName) { if ('unicaen-auth_user_service' === $serviceName) {
return new User(); return new User();
} }
if ('event_manager' === $serviceName) { if ('EventManager' === $serviceName) {
return $eventManager; return $eventManager;
} }
if ('zfcuser_module_options' === $serviceName) {
return new ModuleOptions();
}
if ('unicaen-app_module_options' === $serviceName) {
return new \UnicaenApp\Options\ModuleOptions();
}
if ('unicaen-auth_module_options' === $serviceName) {
return new \UnicaenAuth\Options\ModuleOptions();
}
if ('ldap_people_mapper' === $serviceName) {
return new People();
}
return null; return null;
})); }));
......
...@@ -5,9 +5,10 @@ namespace UnicaenAuthTest\Authentication\Adapter; ...@@ -5,9 +5,10 @@ namespace UnicaenAuthTest\Authentication\Adapter;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use UnicaenApp\Exception\RuntimeException; use UnicaenApp\Exception\RuntimeException;
use UnicaenAuth\Authentication\Adapter\Cas; use UnicaenAuth\Authentication\Adapter\Cas;
use UnicaenAuth\Authentication\Adapter\phpCASWrapper;
use UnicaenAuth\Options\ModuleOptions; use UnicaenAuth\Options\ModuleOptions;
use Zend\EventManager\EventManager; use Zend\EventManager\EventManager;
use Zend\Router\Http\TreeRouteStack;
use Zend\Uri\Uri;
use ZfcUser\Authentication\Adapter\AdapterChainEvent; use ZfcUser\Authentication\Adapter\AdapterChainEvent;
use Zend\Authentication\Result; use Zend\Authentication\Result;
...@@ -69,7 +70,7 @@ class CasTest extends PHPUnit_Framework_TestCase ...@@ -69,7 +70,7 @@ class CasTest extends PHPUnit_Framework_TestCase
})); }));
$this->adapter = new Cas(); $this->adapter = new Cas();
$this->adapter->setServiceManager($serviceManager) $this->adapter//->setServiceManager($serviceManager)
->setEventManager(new EventManager()); ->setEventManager(new EventManager());
} }
...@@ -85,25 +86,28 @@ class CasTest extends PHPUnit_Framework_TestCase ...@@ -85,25 +86,28 @@ class CasTest extends PHPUnit_Framework_TestCase
/** /**
* @dataProvider getInvalidCasOptions * @dataProvider getInvalidCasOptions
* @expectedException RuntimeException * @expectedException \Exception
* @param array $config
*/ */
public function testThrowsExceptionIfNoCasParamSpecified($config) public function testThrowsExceptionIfNoCasParamSpecified(array $config)
{ {
$this->moduleOptions->setCas($config); $this->moduleOptions->setCas($config);
$this->adapter->setOptions($this->moduleOptions);
$this->adapter->authenticate(new AdapterChainEvent()); $this->adapter->authenticate(new AdapterChainEvent());
} }
public function testAuthenticateReturnsNullIfNoCasConfigSpecified() public function testAuthenticateReturnsNullIfNoCasConfigSpecified()
{ {
$this->moduleOptions->setCas([]); $this->moduleOptions->setCas([]);
$this->adapter->setOptions($this->moduleOptions);
$result = $this->adapter->authenticate(new AdapterChainEvent()); $result = $this->adapter->authenticate(new AdapterChainEvent());
$this->assertNull($result); $this->assertNull($result);
} }
public function testAuthenticatePopulatesEventWhenAuthenticationSucceeds() public function testAuthenticatePopulatesEventWhenAuthenticationSucceeds()
{ {
/** @var phpCASWrapper|\PHPUnit_Framework_MockObject_MockObject $casClient */ /** @var \phpCAS|\PHPUnit_Framework_MockObject_MockObject $casClient */
$casClient = $this->createMock(phpCASWrapper::class); $casClient = $this->createMock(\phpCAS::class);
$casClient->expects($this->once()) $casClient->expects($this->once())
->method('getUser') ->method('getUser')
->will($this->returnValue($username = 'username')); ->will($this->returnValue($username = 'username'));
...@@ -112,6 +116,7 @@ class CasTest extends PHPUnit_Framework_TestCase ...@@ -112,6 +116,7 @@ class CasTest extends PHPUnit_Framework_TestCase
$event = new AdapterChainEvent(); $event = new AdapterChainEvent();
$this->adapter->setOptions($this->moduleOptions);
$this->adapter->authenticate($event); $this->adapter->authenticate($event);
$this->assertTrue($this->adapter->isSatisfied()); $this->assertTrue($this->adapter->isSatisfied());
...@@ -125,22 +130,36 @@ class CasTest extends PHPUnit_Framework_TestCase ...@@ -125,22 +130,36 @@ class CasTest extends PHPUnit_Framework_TestCase
public function testLogoutReturnsNullIfNoCasConfigSpecified() public function testLogoutReturnsNullIfNoCasConfigSpecified()
{ {
$this->moduleOptions->setCas([]); $this->moduleOptions->setCas([]);
$this->adapter->setOptions($this->moduleOptions);
$result = $this->adapter->logout(new AdapterChainEvent()); $result = $this->adapter->logout(new AdapterChainEvent());
$this->assertNull($result); $this->assertNull($result);
} }
public function testCanLogoutFromCasWithRedirectService() public function testCanLogoutFromCasWithRedirectService()
{ {
/** @var phpCASWrapper|\PHPUnit_Framework_MockObject_MockObject $casClient */ /** @var \phpCAS|\PHPUnit_Framework_MockObject_MockObject $casClient */
$casClient = $this->createMock(phpCASWrapper::class); $casClient = $this->createMock(\phpCAS::class);
$casClient->expects($this->once()) $casClient->expects($this->once())
->method('isAuthenticated') ->method('isAuthenticated')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$casClient->expects($this->once()) $casClient->expects($this->once())
->method('logoutWithRedirectService'); ->method('logoutWithRedirectService');
/** @var TreeRouteStack|\PHPUnit_Framework_MockObject_MockObject $router */
$router = $this->createMock(TreeRouteStack::class);
$router
->expects($this->once())
->method('getRequestUri')
->willReturn(new Uri());
$router
->expects($this->once())
->method('getBaseUrl')
->willReturn('path');
$this->adapter->setCasClient($casClient); $this->adapter->setCasClient($casClient);
$this->adapter->setOptions($this->moduleOptions);
$this->adapter->setRouter($router);
$this->adapter->logout(new AdapterChainEvent()); $this->adapter->logout(new AdapterChainEvent());
} }
} }
\ No newline at end of file
...@@ -5,10 +5,13 @@ use PDOException; ...@@ -5,10 +5,13 @@ use PDOException;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use UnicaenAuth\Authentication\Adapter\Db; use UnicaenAuth\Authentication\Adapter\Db;
use UnicaenAuth\Options\ModuleOptions; use UnicaenAuth\Options\ModuleOptions;
use Zend\EventManager\EventInterface;
use Zend\Http\PhpEnvironment\Request; use Zend\Http\PhpEnvironment\Request;
use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\Parameters; use Zend\Stdlib\Parameters;
use ZfcUser\Authentication\Adapter\AdapterChainEvent; use ZfcUser\Authentication\Adapter\AdapterChainEvent;
use ZfcUser\Mapper\User;
/** /**
* Description of DbTest * Description of DbTest
...@@ -17,8 +20,16 @@ use ZfcUser\Authentication\Adapter\AdapterChainEvent; ...@@ -17,8 +20,16 @@ use ZfcUser\Authentication\Adapter\AdapterChainEvent;
*/ */
class DbTest extends PHPUnit_Framework_TestCase class DbTest extends PHPUnit_Framework_TestCase
{ {
/**
* @var Db|\PHPUnit_Framework_MockObject_MockObject
*/
protected $adapter; protected $adapter;
protected $moduleOptions; protected $moduleOptions;
/**
* @var User|\PHPUnit_Framework_MockObject_MockObject
*/
protected $mapper; protected $mapper;
/** /**
...@@ -45,6 +56,7 @@ class DbTest extends PHPUnit_Framework_TestCase ...@@ -45,6 +56,7 @@ class DbTest extends PHPUnit_Framework_TestCase
$this->mapper = $mapper = $this->createMock('ZfcUser\Mapper\User'/*, ['findByUsername', 'findByEmail']*/); $this->mapper = $mapper = $this->createMock('ZfcUser\Mapper\User'/*, ['findByUsername', 'findByEmail']*/);
/** @var ServiceManager|\PHPUnit_Framework_MockObject_MockObject $serviceManager */
$serviceManager = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/); $serviceManager = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
$serviceManager->expects($this->any()) $serviceManager->expects($this->any())
->method('get') ->method('get')
...@@ -68,13 +80,14 @@ class DbTest extends PHPUnit_Framework_TestCase ...@@ -68,13 +80,14 @@ class DbTest extends PHPUnit_Framework_TestCase
public function getException() public function getException()
{ {
return [ return [
[new PDOException()], //[new PDOException()],
[new ServiceNotFoundException()], [new ServiceNotFoundException()],
]; ];
} }
/** /**
* @dataProvider getException * @dataProvider getException
* @param \Exception $exception
*/ */
public function testAuthenticateReturnsFalseIfExceptionThrown($exception) public function testAuthenticateReturnsFalseIfExceptionThrown($exception)
{ {
...@@ -85,8 +98,12 @@ class DbTest extends PHPUnit_Framework_TestCase ...@@ -85,8 +98,12 @@ class DbTest extends PHPUnit_Framework_TestCase
$request = new Request(); $request = new Request();
$request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"])); $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"]));
$event = new AdapterChainEvent(); /** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */
$event->setRequest($request); $event = $this->createMock(EventInterface::class);
$event
->expects($this->exactly(2))
->method('getTarget')
->willReturn((new AdapterChainEvent())->setRequest($request));
$result = $this->adapter->authenticate($event); $result = $this->adapter->authenticate($event);
$this->assertFalse($result); $this->assertFalse($result);
...@@ -97,8 +114,12 @@ class DbTest extends PHPUnit_Framework_TestCase ...@@ -97,8 +114,12 @@ class DbTest extends PHPUnit_Framework_TestCase
$request = new Request(); $request = new Request();
$request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"])); $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"]));
$event = new AdapterChainEvent(); /** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */
$event->setRequest($request); $event = $this->createMock(EventInterface::class);
$event
->expects($this->exactly(2))
->method('getTarget')
->willReturn((new AdapterChainEvent())->setRequest($request));
$result = $this->adapter->authenticate($event); $result = $this->adapter->authenticate($event);
$this->assertFalse($result); $this->assertFalse($result);
......
...@@ -3,8 +3,11 @@ ...@@ -3,8 +3,11 @@
namespace UnicaenAuthTest\Authentication\Adapter; namespace UnicaenAuthTest\Authentication\Adapter;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use UnicaenApp\Mapper\Ldap\People;
use UnicaenAuth\Authentication\Adapter\Ldap; use UnicaenAuth\Authentication\Adapter\Ldap;
use Zend\Authentication\Result; use Zend\Authentication\Result;
use Zend\Authentication\Storage\StorageInterface;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManager; use Zend\EventManager\EventManager;
use Zend\Http\Request; use Zend\Http\Request;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
...@@ -19,7 +22,7 @@ use ZfcUser\Authentication\Adapter\AdapterChainEvent; ...@@ -19,7 +22,7 @@ use ZfcUser\Authentication\Adapter\AdapterChainEvent;
class LdapTest extends PHPUnit_Framework_TestCase class LdapTest extends PHPUnit_Framework_TestCase
{ {
/** /**
* @var Ldap * @var Ldap|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected $adapter; protected $adapter;
...@@ -82,12 +85,14 @@ class LdapTest extends PHPUnit_Framework_TestCase ...@@ -82,12 +85,14 @@ class LdapTest extends PHPUnit_Framework_TestCase
})); }));
$this->adapter = new Ldap(); $this->adapter = new Ldap();
$this->adapter->setServiceManager($serviceManager) $this->adapter//->setServiceManager($serviceManager)
->setEventManager(new EventManager()); ->setEventManager(new EventManager());
} }
public function testCanProvideDefaultLdapAuthAdapter() public function testCanProvideDefaultLdapAuthAdapter()
{ {
$this->adapter->setAppModuleOptions($this->appModuleOptions);
$adapter = $this->adapter->getLdapAuthAdapter(); $adapter = $this->adapter->getLdapAuthAdapter();
$this->assertInstanceOf('Zend\Authentication\Adapter\Ldap', $adapter); $this->assertInstanceOf('Zend\Authentication\Adapter\Ldap', $adapter);
...@@ -99,10 +104,35 @@ class LdapTest extends PHPUnit_Framework_TestCase ...@@ -99,10 +104,35 @@ class LdapTest extends PHPUnit_Framework_TestCase
public function testAuthenticatingReturnsNullIfAlreadyStatisfied() public function testAuthenticatingReturnsNullIfAlreadyStatisfied()
{ {
$event = new AdapterChainEvent(); /** @var AdapterChainEvent|\PHPUnit_Framework_MockObject_MockObject $adapterChainEvent */
$this->adapter->setSatisfied(); $adapterChainEvent = $this->createMock(AdapterChainEvent::class);
$adapterChainEvent
->expects($this->once())
->method('setIdentity')
->with('IDENTITY')
->willReturnSelf();
$adapterChainEvent
->expects($this->once())
->method('setCode')
->with(Result::SUCCESS)
->willReturnSelf();
/** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->createMock(EventInterface::class);
$event
->expects($this->once())
->method('getTarget')
->willReturn($adapterChainEvent);
/** @var StorageInterface|\PHPUnit_Framework_MockObject_MockObject $storage */
$storage = $this->createMock(StorageInterface::class);
$storage
->expects($this->exactly(2))
->method('read')
->willReturn(['is_satisfied' => true, 'identity' => 'IDENTITY']);
$this->adapter->setStorage($storage);
$this->assertNull($this->adapter->authenticate($event)); $this->assertNull($this->adapter->authenticate($event));
$this->assertEquals($event->getCode(), Result::SUCCESS);
} }
public function testUsurpationWithAllowedUsernameAndSuccessfulAuthentication() public function testUsurpationWithAllowedUsernameAndSuccessfulAuthentication()
...@@ -228,7 +258,7 @@ class LdapTest extends PHPUnit_Framework_TestCase ...@@ -228,7 +258,7 @@ class LdapTest extends PHPUnit_Framework_TestCase
$this->assertFalse($event->propagationIsStopped()); $this->assertFalse($event->propagationIsStopped());
} }
protected function _authenticateWithUsurpation($authenticationResultCode, AdapterChainEvent &$event) protected function _authenticateWithUsurpation($authenticationResultCode, AdapterChainEvent &$adapterChainEvent)
{ {
$usernameUsurpateur = 'usurpateur'; $usernameUsurpateur = 'usurpateur';
$usernameUsurpe = 'usurpe'; $usernameUsurpe = 'usurpe';
...@@ -246,10 +276,39 @@ class LdapTest extends PHPUnit_Framework_TestCase ...@@ -246,10 +276,39 @@ class LdapTest extends PHPUnit_Framework_TestCase
->will($this->returnValue(new Result($authenticationResultCode, $usernameUsurpateur))); ->will($this->returnValue(new Result($authenticationResultCode, $usernameUsurpateur)));
$this->adapter->setLdapAuthAdapter($this->zendAuthLdapAdapter); $this->adapter->setLdapAuthAdapter($this->zendAuthLdapAdapter);
$ldapPeopleMapper = $this->createMock(People::class);
$ldapPeopleMapper
->expects($this->once())
->method('findOneByUsername')
->willReturn('not empty');
$this->adapter->setLdapPeopleMapper($ldapPeopleMapper);
$request = new Request(); $request = new Request();
$request->setPost(new Parameters(['identity' => $username, 'credential' => "xxxxx"])); $request->setPost(new Parameters(['identity' => $username, 'credential' => "xxxxx"]));
$event->setRequest($request); $adapterChainEvent->setRequest($request);
// /** @var AdapterChainEvent|\PHPUnit_Framework_MockObject_MockObject $adapterChainEvent */
// $adapterChainEvent = $this->createMock(AdapterChainEvent::class);
// $adapterChainEvent
// ->expects($this->once())
// ->method('setIdentity')
// ->with('IDENTITY')
// ->willReturnSelf();
// $adapterChainEvent
// ->expects($this->once())
// ->method('setCode')
// ->with(Result::SUCCESS)
// ->willReturnSelf();
/** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->createMock(EventInterface::class);
$event
->expects($this->once())
->method('getTarget')
->willReturn($adapterChainEvent);
$this->adapter->setOptions($this->authModuleOptions);
$this->adapter->authenticate($event); $this->adapter->authenticate($event);
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment