Skip to content
Snippets Groups Projects
Select Git revision
  • 431bbc990b7a7dbf23f7fc7afa10f0babd33e57b
  • master default protected
  • release-1.3.10
  • popover-bootstrap-3.4
  • zf-3.x
  • 3.0.9
  • 3.0.8
  • 1.3.10
  • 3.0.7
  • 3.0.6
  • 3.0.5
  • 3.0.4
  • 3.0.3
  • 3.0.2
  • 3.0.1
  • 3.0.0
  • 1.3.9
  • 1.3.8
  • 1.3.7
  • 1.3.6
  • 1.3.5
  • 1.3.4
  • 1.3.3
  • 1.3.2
  • 1.3.1
25 results

AbstractFactoryTest.php

Blame
  • Forked from lib / unicaen / auth
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    AbstractFactoryTest.php 4.06 KiB
    <?php
    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
     *
     * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
     */
    class AbstractFactoryTest extends PHPUnit_Framework_TestCase
    {
        /**
         * @var AbstractFactory
         */
        protected $factory;
    
        protected function setUp()
        {
            $this->factory = new AbstractFactory();
        }
    
        public function getInvalidServiceClassName()
        {
            return [
                'unknown-class'   => ['UnicaenAuth\Authentication\Adapter\Xxxx'],
                'wrong-namespace' => ['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 [
                'cas'  => ['UnicaenAuth\Authentication\Adapter\Cas'],
                'db'   => ['UnicaenAuth\Authentication\Adapter\Db'],
                'ldap' => ['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\LogicException
         * @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->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
            $serviceLocator->expects($this->any())
                           ->method('get')
                           ->will($this->returnCallback(function($serviceName) use ($eventManager) {
                               if ('unicaen-auth_user_service' === $serviceName) {
                                   return new User();
                               }
                               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;
                           }));
    
            $adapter = $this->factory->createServiceWithName($serviceLocator, null, $serviceClassName);
    
            $this->assertInstanceOf($serviceClassName, $adapter);
    
            if ($adapter instanceof EventManagerAwareInterface) {
                $this->assertSame($eventManager, $adapter->getEventManager());
            }
        }
    }