diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 2c872f405ff83f11dee28677eab9bfa8afc69fa7..b657ec30526255b8263910eff36108197566e145 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -29,7 +29,7 @@ class Bootstrap static::initAutoloader(); - static::$serviceManager = new ServiceManager(new ServiceManagerConfig()); + static::$serviceManager = new ServiceManager([]); } public static function getServiceManager() diff --git a/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php b/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php index 6e4db97b8184012a9c87948da63db69259cb74db..6c0654ababe493e88a58985162670745297c9438 100644 --- a/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php +++ b/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php @@ -2,11 +2,14 @@ namespace UnicaenAuthTest\Authentication\Adapter; use PHPUnit_Framework_TestCase; +use UnicaenApp\Mapper\Ldap\People; use UnicaenAuth\Authentication\Adapter\AbstractFactory; use UnicaenAuth\Service\User; use Zend\EventManager\EventManager; use Zend\EventManager\EventManagerAwareInterface; use Zend\ServiceManager\ServiceManager; +use UnicaenApp\Exception\LogicException; +use ZfcUser\Options\ModuleOptions; /** * Description of AbstractFactoryTest @@ -62,7 +65,7 @@ class AbstractFactoryTest extends PHPUnit_Framework_TestCase /** * @dataProvider getInvalidServiceClassName - * @expectedException \UnicaenApp\Exception\RuntimeException + * @expectedException \UnicaenApp\Exception\LogicException * @param string $serviceClassName */ public function testCreateServiceWithNameThrowsExceptionIfInvalidServiceSpecified($serviceClassName) @@ -85,9 +88,21 @@ class AbstractFactoryTest extends PHPUnit_Framework_TestCase if ('unicaen-auth_user_service' === $serviceName) { return new User(); } - if ('event_manager' === $serviceName) { + if ('EventManager' === $serviceName) { 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; })); diff --git a/tests/UnicaenAuthTest/Authentication/Adapter/CasTest.php b/tests/UnicaenAuthTest/Authentication/Adapter/CasTest.php index 82969c0304238c44a89bfbcd1b6cb1d067f2032e..66d9bf2169e5ba50a68cb313a609db07333c5678 100644 --- a/tests/UnicaenAuthTest/Authentication/Adapter/CasTest.php +++ b/tests/UnicaenAuthTest/Authentication/Adapter/CasTest.php @@ -5,9 +5,10 @@ namespace UnicaenAuthTest\Authentication\Adapter; use PHPUnit_Framework_TestCase; use UnicaenApp\Exception\RuntimeException; use UnicaenAuth\Authentication\Adapter\Cas; -use UnicaenAuth\Authentication\Adapter\phpCASWrapper; use UnicaenAuth\Options\ModuleOptions; use Zend\EventManager\EventManager; +use Zend\Router\Http\TreeRouteStack; +use Zend\Uri\Uri; use ZfcUser\Authentication\Adapter\AdapterChainEvent; use Zend\Authentication\Result; @@ -69,7 +70,7 @@ class CasTest extends PHPUnit_Framework_TestCase })); $this->adapter = new Cas(); - $this->adapter->setServiceManager($serviceManager) + $this->adapter//->setServiceManager($serviceManager) ->setEventManager(new EventManager()); } @@ -85,25 +86,28 @@ class CasTest extends PHPUnit_Framework_TestCase /** * @dataProvider getInvalidCasOptions - * @expectedException RuntimeException + * @expectedException \Exception + * @param array $config */ - public function testThrowsExceptionIfNoCasParamSpecified($config) + public function testThrowsExceptionIfNoCasParamSpecified(array $config) { $this->moduleOptions->setCas($config); + $this->adapter->setOptions($this->moduleOptions); $this->adapter->authenticate(new AdapterChainEvent()); } public function testAuthenticateReturnsNullIfNoCasConfigSpecified() { $this->moduleOptions->setCas([]); + $this->adapter->setOptions($this->moduleOptions); $result = $this->adapter->authenticate(new AdapterChainEvent()); $this->assertNull($result); } public function testAuthenticatePopulatesEventWhenAuthenticationSucceeds() { - /** @var phpCASWrapper|\PHPUnit_Framework_MockObject_MockObject $casClient */ - $casClient = $this->createMock(phpCASWrapper::class); + /** @var \phpCAS|\PHPUnit_Framework_MockObject_MockObject $casClient */ + $casClient = $this->createMock(\phpCAS::class); $casClient->expects($this->once()) ->method('getUser') ->will($this->returnValue($username = 'username')); @@ -112,6 +116,7 @@ class CasTest extends PHPUnit_Framework_TestCase $event = new AdapterChainEvent(); + $this->adapter->setOptions($this->moduleOptions); $this->adapter->authenticate($event); $this->assertTrue($this->adapter->isSatisfied()); @@ -125,22 +130,36 @@ class CasTest extends PHPUnit_Framework_TestCase public function testLogoutReturnsNullIfNoCasConfigSpecified() { $this->moduleOptions->setCas([]); + $this->adapter->setOptions($this->moduleOptions); $result = $this->adapter->logout(new AdapterChainEvent()); $this->assertNull($result); } public function testCanLogoutFromCasWithRedirectService() { - /** @var phpCASWrapper|\PHPUnit_Framework_MockObject_MockObject $casClient */ - $casClient = $this->createMock(phpCASWrapper::class); + /** @var \phpCAS|\PHPUnit_Framework_MockObject_MockObject $casClient */ + $casClient = $this->createMock(\phpCAS::class); $casClient->expects($this->once()) ->method('isAuthenticated') ->will($this->returnValue(true)); $casClient->expects($this->once()) ->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->setOptions($this->moduleOptions); + $this->adapter->setRouter($router); $this->adapter->logout(new AdapterChainEvent()); } } \ No newline at end of file diff --git a/tests/UnicaenAuthTest/Authentication/Adapter/DbTest.php b/tests/UnicaenAuthTest/Authentication/Adapter/DbTest.php index e5edf6c3e0bb8f595d8ac8456941d4f8ef213f3d..e9501903f64ecb95de73b6505983b78db7eee2e9 100644 --- a/tests/UnicaenAuthTest/Authentication/Adapter/DbTest.php +++ b/tests/UnicaenAuthTest/Authentication/Adapter/DbTest.php @@ -5,10 +5,13 @@ use PDOException; use PHPUnit_Framework_TestCase; use UnicaenAuth\Authentication\Adapter\Db; use UnicaenAuth\Options\ModuleOptions; +use Zend\EventManager\EventInterface; use Zend\Http\PhpEnvironment\Request; use Zend\ServiceManager\Exception\ServiceNotFoundException; +use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\Parameters; use ZfcUser\Authentication\Adapter\AdapterChainEvent; +use ZfcUser\Mapper\User; /** * Description of DbTest @@ -17,8 +20,16 @@ use ZfcUser\Authentication\Adapter\AdapterChainEvent; */ class DbTest extends PHPUnit_Framework_TestCase { + /** + * @var Db|\PHPUnit_Framework_MockObject_MockObject + */ protected $adapter; + protected $moduleOptions; + + /** + * @var User|\PHPUnit_Framework_MockObject_MockObject + */ protected $mapper; /** @@ -45,6 +56,7 @@ class DbTest extends PHPUnit_Framework_TestCase $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->expects($this->any()) ->method('get') @@ -68,13 +80,14 @@ class DbTest extends PHPUnit_Framework_TestCase public function getException() { return [ - [new PDOException()], + //[new PDOException()], [new ServiceNotFoundException()], ]; } /** * @dataProvider getException + * @param \Exception $exception */ public function testAuthenticateReturnsFalseIfExceptionThrown($exception) { @@ -85,8 +98,12 @@ class DbTest extends PHPUnit_Framework_TestCase $request = new Request(); $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"])); - $event = new AdapterChainEvent(); - $event->setRequest($request); + /** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */ + $event = $this->createMock(EventInterface::class); + $event + ->expects($this->exactly(2)) + ->method('getTarget') + ->willReturn((new AdapterChainEvent())->setRequest($request)); $result = $this->adapter->authenticate($event); $this->assertFalse($result); @@ -97,8 +114,12 @@ class DbTest extends PHPUnit_Framework_TestCase $request = new Request(); $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"])); - $event = new AdapterChainEvent(); - $event->setRequest($request); + /** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */ + $event = $this->createMock(EventInterface::class); + $event + ->expects($this->exactly(2)) + ->method('getTarget') + ->willReturn((new AdapterChainEvent())->setRequest($request)); $result = $this->adapter->authenticate($event); $this->assertFalse($result); diff --git a/tests/UnicaenAuthTest/Authentication/Adapter/LdapTest.php b/tests/UnicaenAuthTest/Authentication/Adapter/LdapTest.php index bf14c6bdf0a98b4642e8953b4ea1afc12ecef9a3..3bbf47bc3d3c4b452b1afffdeaf0604c28b08d4f 100644 --- a/tests/UnicaenAuthTest/Authentication/Adapter/LdapTest.php +++ b/tests/UnicaenAuthTest/Authentication/Adapter/LdapTest.php @@ -3,8 +3,11 @@ namespace UnicaenAuthTest\Authentication\Adapter; use PHPUnit_Framework_TestCase; +use UnicaenApp\Mapper\Ldap\People; use UnicaenAuth\Authentication\Adapter\Ldap; use Zend\Authentication\Result; +use Zend\Authentication\Storage\StorageInterface; +use Zend\EventManager\EventInterface; use Zend\EventManager\EventManager; use Zend\Http\Request; use Zend\ServiceManager\ServiceManager; @@ -19,7 +22,7 @@ use ZfcUser\Authentication\Adapter\AdapterChainEvent; class LdapTest extends PHPUnit_Framework_TestCase { /** - * @var Ldap + * @var Ldap|\PHPUnit_Framework_MockObject_MockObject */ protected $adapter; @@ -82,12 +85,14 @@ class LdapTest extends PHPUnit_Framework_TestCase })); $this->adapter = new Ldap(); - $this->adapter->setServiceManager($serviceManager) + $this->adapter//->setServiceManager($serviceManager) ->setEventManager(new EventManager()); } public function testCanProvideDefaultLdapAuthAdapter() { + $this->adapter->setAppModuleOptions($this->appModuleOptions); + $adapter = $this->adapter->getLdapAuthAdapter(); $this->assertInstanceOf('Zend\Authentication\Adapter\Ldap', $adapter); @@ -99,10 +104,35 @@ class LdapTest extends PHPUnit_Framework_TestCase public function testAuthenticatingReturnsNullIfAlreadyStatisfied() { - $event = new AdapterChainEvent(); - $this->adapter->setSatisfied(); + /** @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); + + /** @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->assertEquals($event->getCode(), Result::SUCCESS); } public function testUsurpationWithAllowedUsernameAndSuccessfulAuthentication() @@ -228,7 +258,7 @@ class LdapTest extends PHPUnit_Framework_TestCase $this->assertFalse($event->propagationIsStopped()); } - protected function _authenticateWithUsurpation($authenticationResultCode, AdapterChainEvent &$event) + protected function _authenticateWithUsurpation($authenticationResultCode, AdapterChainEvent &$adapterChainEvent) { $usernameUsurpateur = 'usurpateur'; $usernameUsurpe = 'usurpe'; @@ -246,10 +276,39 @@ class LdapTest extends PHPUnit_Framework_TestCase ->will($this->returnValue(new Result($authenticationResultCode, $usernameUsurpateur))); $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->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); } } \ No newline at end of file