AbstractFactoryTest.php 3.19 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 TestCase
17
18
19
20
21
22
23
{
    protected $factory;

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

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

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

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

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

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

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

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

91
        $adapter = $this->factory->createServiceWithName($serviceLocator, null, $serviceClassName);
92

93
        $this->assertInstanceOf($serviceClassName, $adapter);
94

95
96
97
98
99
        if ($adapter instanceof EventManagerAwareInterface) {
            $this->assertSame($eventManager, $adapter->getEventManager());
        }
    }
}