AbstractFactoryTest.php 3.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace UnicaenAuthTest\Authentication\Adapter;

use PHPUnit_Framework_TestCase;
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>
 */
class AbstractFactoryTest extends PHPUnit_Framework_TestCase
{
    protected $factory;

    protected function setUp()
    {
        $this->factory = new AbstractFactory();
    }
    
    public function getInvalidServiceClassName()
    {
        return array(
            'unknown-class'   => array('UnicaenAuth\Authentication\Adapter\Xxxx'),
            'wrong-namespace' => array('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 array(
            'cas'  => array('UnicaenAuth\Authentication\Adapter\Cas'),
            'db'   => array('UnicaenAuth\Authentication\Adapter\Db'),
            'ldap' => array('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
     * @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->getMock('Zend\ServiceManager\ServiceManager', array('get'));
        $serviceLocator->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($eventManager) {
                           if ('unicaen-auth_user_service' === $serviceName) {
                               return new User();
                           }
                           if ('event_manager' === $serviceName) {
                               return $eventManager;
                           }
                           return null;
                       }));
        
        $adapter = $this->factory->createServiceWithName($serviceLocator, null, $serviceClassName);
                       
        $this->assertInstanceOf($serviceClassName, $adapter);
        
        if ($adapter instanceof EventManagerAwareInterface) {
            $this->assertSame($eventManager, $adapter->getEventManager());
        }
    }
}