Commit bf0263f4 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Factory des adapteurs d'authentification : tests unitaires ; refactorisation légères.

parent 255d26e7
......@@ -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;
......
<?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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment