diff --git a/src/UnicaenAuth/Authentication/Adapter/AbstractFactory.php b/src/UnicaenAuth/Authentication/Adapter/AbstractFactory.php index f2e6ff2a672479fbe085acddc04b16f5449b3b63..0b351631db0af4fef25bde31e1ace47770b654a4 100644 --- a/src/UnicaenAuth/Authentication/Adapter/AbstractFactory.php +++ b/src/UnicaenAuth/Authentication/Adapter/AbstractFactory.php @@ -54,9 +54,10 @@ class AbstractFactory implements AbstractFactoryInterface } if ($adapter instanceof \Zend\EventManager\EventManagerAwareInterface) { - $userService = $serviceLocator->get('unicaen-auth_user_service'); - $adapter->setEventManager($serviceLocator->get('event_manager')); - $adapter->getEventManager()->attach('userAuthenticated', array($userService, 'userAuthenticated'), 1); + $userService = $serviceLocator->get('unicaen-auth_user_service'); + $eventManager = $serviceLocator->get('event_manager'); + $eventManager->attach('userAuthenticated', array($userService, 'userAuthenticated'), 1); + $adapter->setEventManager($eventManager); } return $adapter; diff --git a/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php b/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9eac1c865c013b76b56affedc7a5810e8c4f6c8a --- /dev/null +++ b/tests/UnicaenAuthTest/Authentication/Adapter/AbstractFactoryTest.php @@ -0,0 +1,99 @@ +<?php +namespace UnicaenAuthTest\Authentication\Adapter; + +use PHPUnit_Framework_TestCase; +use UnicaenAuth\Authentication\Adapter\AbstractFactory; +use UnicaenAuth\Service\User; +use Zend\EventManager\EventManager; +use Zend\EventManager\EventManagerAwareInterface; +use Zend\ServiceManager\ServiceManager; + +/** + * Description of AbstractFactoryTest + * + * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + */ +class AbstractFactoryTest extends PHPUnit_Framework_TestCase +{ + protected $factory; + + protected function setUp() + { + $this->factory = new AbstractFactory(); + } + + public function getInvalidServiceClassName() + { + return array( + 'unknown-class' => array('UnicaenAuth\Authentication\Adapter\Xxxx'), + 'wrong-namespace' => array('Any\Other\Namespaced\Class'), + ); + } + + /** + * @dataProvider getInvalidServiceClassName + * @param string $serviceClassName + */ + public function testCanRefuseCreatingServiceWithInvalidName($serviceClassName) + { + $this->assertFalse($this->factory->canCreateServiceWithName(new ServiceManager(), null, $serviceClassName)); + } + + public function getValidServiceClassName() + { + return array( + 'cas' => array('UnicaenAuth\Authentication\Adapter\Cas'), + 'db' => array('UnicaenAuth\Authentication\Adapter\Db'), + 'ldap' => array('UnicaenAuth\Authentication\Adapter\Ldap'), + ); + } + + /** + * @dataProvider getValidServiceClassName + * @param string $serviceClassName + */ + public function testCanAcceptCreatingServiceWithValidName($serviceClassName) + { + $this->assertTrue($this->factory->canCreateServiceWithName(new ServiceManager(), null, $serviceClassName)); + } + + /** + * @dataProvider getInvalidServiceClassName + * @expectedException \UnicaenApp\Exception + * @param string $serviceClassName + */ + public function testCreateServiceWithNameThrowsExceptionIfInvalidServiceSpecified($serviceClassName) + { + $this->factory->createServiceWithName(new ServiceManager(), null, $serviceClassName); + } + + /** + * @dataProvider getValidServiceClassName + * @param string $serviceClassName + */ + public function testCanCreateServiceWithName($serviceClassName) + { + $eventManager = new EventManager(); + + $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceManager', array('get')); + $serviceLocator->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($serviceName) use ($eventManager) { + if ('unicaen-auth_user_service' === $serviceName) { + return new User(); + } + if ('event_manager' === $serviceName) { + return $eventManager; + } + return null; + })); + + $adapter = $this->factory->createServiceWithName($serviceLocator, null, $serviceClassName); + + $this->assertInstanceOf($serviceClassName, $adapter); + + if ($adapter instanceof EventManagerAwareInterface) { + $this->assertSame($eventManager, $adapter->getEventManager()); + } + } +} \ No newline at end of file