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

4
use PHPUnit_Framework_TestCase;
5
6
7
8
9
10
11
12
13
14
15
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>
 */
16
class AbstractFactoryTest extends PHPUnit_Framework_TestCase
17
{
18
19
20
    /**
     * @var AbstractFactory
     */
21
22
23
24
25
26
    protected $factory;

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

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

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

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

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

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

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

81
        $serviceLocator = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
82
83
84
85
86
87
        $serviceLocator->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($eventManager) {
                           if ('unicaen-auth_user_service' === $serviceName) {
                               return new User();
                           }
88
                           if ('event_manager' === $serviceName) {
89
90
91
92
                               return $eventManager;
                           }
                           return null;
                       }));
93

94
        $adapter = $this->factory->createServiceWithName($serviceLocator, null, $serviceClassName);
95

96
        $this->assertInstanceOf($serviceClassName, $adapter);
97

98
99
100
101
102
        if ($adapter instanceof EventManagerAwareInterface) {
            $this->assertSame($eventManager, $adapter->getEventManager());
        }
    }
}