AbstractFactoryTest.php 4.06 KB
Newer Older
1
2
3
<?php
namespace UnicaenAuthTest\Authentication\Adapter;

4
use PHPUnit_Framework_TestCase;
5
use UnicaenApp\Mapper\Ldap\People;
6
7
8
9
10
use UnicaenAuth\Authentication\Adapter\AbstractFactory;
use UnicaenAuth\Service\User;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
11
12
use UnicaenApp\Exception\LogicException;
use ZfcUser\Options\ModuleOptions;
13
14
15
16
17
18

/**
 * Description of AbstractFactoryTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
19
class AbstractFactoryTest extends PHPUnit_Framework_TestCase
20
{
21
22
23
    /**
     * @var AbstractFactory
     */
24
25
26
27
28
29
    protected $factory;

    protected function setUp()
    {
        $this->factory = new AbstractFactory();
    }
30

31
32
    public function getInvalidServiceClassName()
    {
33
34
35
36
        return [
            'unknown-class'   => ['UnicaenAuth\Authentication\Adapter\Xxxx'],
            'wrong-namespace' => ['Any\Other\Namespaced\Class'],
        ];
37
    }
38

39
40
41
42
43
44
45
46
    /**
     * @dataProvider getInvalidServiceClassName
     * @param string $serviceClassName
     */
    public function testCanRefuseCreatingServiceWithInvalidName($serviceClassName)
    {
        $this->assertFalse($this->factory->canCreateServiceWithName(new ServiceManager(), null, $serviceClassName));
    }
47

48
49
    public function getValidServiceClassName()
    {
50
51
52
53
54
        return [
            'cas'  => ['UnicaenAuth\Authentication\Adapter\Cas'],
            'db'   => ['UnicaenAuth\Authentication\Adapter\Db'],
            'ldap' => ['UnicaenAuth\Authentication\Adapter\Ldap'],
        ];
55
    }
56

57
58
59
60
61
62
63
64
    /**
     * @dataProvider getValidServiceClassName
     * @param string $serviceClassName
     */
    public function testCanAcceptCreatingServiceWithValidName($serviceClassName)
    {
        $this->assertTrue($this->factory->canCreateServiceWithName(new ServiceManager(), null, $serviceClassName));
    }
65

66
67
    /**
     * @dataProvider getInvalidServiceClassName
68
     * @expectedException \UnicaenApp\Exception\LogicException
69
70
71
72
73
74
     * @param string $serviceClassName
     */
    public function testCreateServiceWithNameThrowsExceptionIfInvalidServiceSpecified($serviceClassName)
    {
        $this->factory->createServiceWithName(new ServiceManager(), null, $serviceClassName);
    }
75

76
77
78
79
80
81
82
    /**
     * @dataProvider getValidServiceClassName
     * @param string $serviceClassName
     */
    public function testCanCreateServiceWithName($serviceClassName)
    {
        $eventManager = new EventManager();
83

84
        $serviceLocator = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
85
86
87
88
89
90
        $serviceLocator->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($eventManager) {
                           if ('unicaen-auth_user_service' === $serviceName) {
                               return new User();
                           }
91
                           if ('EventManager' === $serviceName) {
92
93
                               return $eventManager;
                           }
94
95
96
97
98
99
100
101
102
103
104
105
                           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();
                           }
106
107
                           return null;
                       }));
108

109
        $adapter = $this->factory->createServiceWithName($serviceLocator, null, $serviceClassName);
110

111
        $this->assertInstanceOf($serviceClassName, $adapter);
112

113
114
115
116
117
        if ($adapter instanceof EventManagerAwareInterface) {
            $this->assertSame($eventManager, $adapter->getEventManager());
        }
    }
}