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

SessionManager: déplacement de Container::setDefaultManager() en dehors de la...

SessionManager: déplacement de Container::setDefaultManager() en dehors de la factory, c'est plus mieux bien ; test unitaire en échec.
parent 3057d7ce
Pipeline #4147 passed with stage
in 20 seconds
......@@ -21,6 +21,8 @@ use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\Mvc\Console\View\ExceptionStrategy as ConsoleExceptionStrategy;
use Zend\Mvc\View\Http\ExceptionStrategy as HttpExceptionStrategy;
use Zend\ServiceManager\ServiceManager;
use Zend\Session\Container;
use Zend\Session\SessionManager;
use Zend\Validator\AbstractValidator;
use Zend\View\Helper\Navigation;
use Zend\View\HelperPluginManager;
......@@ -151,12 +153,14 @@ class Module implements
return;
}
$session = $e->getApplication()->getServiceManager()->get('Zend\Session\SessionManager');
$session->start();
/** @var SessionManager $sessionManager */
$sessionManager = $e->getApplication()->getServiceManager()->get(SessionManager::class);
Container::setDefaultManager($sessionManager);
$sessionManager->start();
$container = new \Zend\Session\Container('initialized');
$container = new Container('initialized');
if (!isset($container->init)) {
$session->regenerateId(true);
$sessionManager->regenerateId(true);
$container->init = 1;
}
}
......
......@@ -3,10 +3,8 @@
namespace UnicaenApp\Session;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use UnicaenApp\Options\ModuleOptions;
use Zend\Session\Config\SessionConfig;
use Zend\Session\Container;
use Zend\Session\SessionManager;
use Zend\Session\Validator\HttpUserAgent;
use Zend\Session\Validator\RemoteAddr;
......@@ -16,21 +14,18 @@ use Zend\Session\Validator\RemoteAddr;
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class SessionManagerFactory implements FactoryInterface
class SessionManagerFactory
{
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container)
{
return $this->__invoke($serviceLocator, '?');
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$appInfos = $container->get('unicaen-app_module_options')->getAppInfos();
/** @var ModuleOptions $moduleOptions */
$moduleOptions = $container->get('unicaen-app_module_options');
$appInfos = $moduleOptions->getAppInfos();
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions(array(
$sessionConfig->setOptions([
'name' => md5($appInfos['nom']),
));
]);
$sessionManager = new SessionManager($sessionConfig);
......@@ -38,8 +33,6 @@ class SessionManagerFactory implements FactoryInterface
$chain->attach('session.validate', array(new RemoteAddr(), 'isValid'));
$chain->attach('session.validate', array(new HttpUserAgent(), 'isValid'));
Container::setDefaultManager($sessionManager);
return $sessionManager;
}
}
\ No newline at end of file
......@@ -2,9 +2,11 @@
namespace UnicaenAppTest\Session;
use UnicaenApp\Options\ModuleOptions;
use UnicaenApp\Session\SessionManagerFactory;
use UnicaenAppTest\BaseServiceFactoryTest;
use Zend\Session\Container;
use Zend\Session\SessionManager;
/**
* Description of ModuleOptionsFactoryTest
......@@ -18,29 +20,31 @@ class ModuleOptionsFactoryTest extends BaseServiceFactoryTest
*/
protected $factory;
protected $factoryClass = 'UnicaenApp\Session\SessionManagerFactory';
protected $serviceClass = 'Zend\Session\SessionManager';
protected $factoryClass = SessionManagerFactory::class;
protected $serviceClass = SessionManager::class;
public function testCanCreateService()
{
$appInfos = array(
$appInfos = [
'nom' => "Mon application",
);
$moduleOptions = $this->createMock('UnicaenApp\Options\ModuleOptions'/*, array('getAppInfos')*/);
$moduleOptions->expects($this->once())
->method('getAppInfos')
->will($this->returnValue($appInfos));
$this->serviceManager->expects($this->once())
->method('get')
->with('unicaen-app_module_options')
->will($this->returnValue($moduleOptions));
$service = $this->factory->__invoke($this->serviceManager, ''); /* @var $service \Zend\Session\SessionManager */
];
$moduleOptions = $this->createMock(ModuleOptions::class);
$moduleOptions
->expects($this->once())
->method('getAppInfos')
->willReturn($appInfos);
$this->serviceManager
->expects($this->once())
->method('get')
->with('unicaen-app_module_options')
->willReturn($moduleOptions);
$service = $this->factory->__invoke($this->serviceManager);
/* @var $service \Zend\Session\SessionManager */
$this->assertInstanceOf($this->serviceClass, $service);
$this->assertSame($service, Container::getDefaultManager());
$this->assertNotEmpty($service->getValidatorChain()->getListeners('session.validate'));
$this->assertEquals($service->getConfig()->getName(), md5($appInfos['nom']));
}
......
Supports Markdown
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