ChainTest.php 4.37 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace UnicaenAuthTest\Provider\Identity;

use PHPUnit_Framework_TestCase;
use UnicaenAuth\Provider\Identity\Chain;
use UnicaenAuth\Acl\NamedRole;

/**
 * Description of ChainTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class ChainTest extends PHPUnit_Framework_TestCase
{
    protected $provider;
    protected $authorize;
    protected $authService;
    protected $serviceManager;
    protected $eventManager;
    protected $event;
    protected $acl;
    
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->authorize      = $this->getMock('BjyAuthorize\Service\Authorize', array('getAcl'), array(), '', false);
        $this->serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', array('get'));
        $this->authService    = $this->getMock('Zend\Authentication\AuthenticationService', array('getIdentity'));
        $this->provider       = new Chain($this->authService);
        $this->eventManager   = $this->getMock('Zend\EventManager\EventManager', array('trigger'));
        $this->event          = $this->getMock('UnicaenAuth\Provider\Identity\ChainEvent', array('getRoles'));
        $this->acl            = $this->getMock('Zend\Permissions\Acl\Acl', array('hasRole', 'getRole'));
        
        $this->authorize->expects($this->any())
                ->method('getAcl')
                ->will($this->returnValue($this->acl));

        $this->serviceManager->expects($this->any())
                ->method('get')
                ->will($this->returnValue($this->authorize));
        
        $this->provider->setServiceLocator($this->serviceManager)
                ->setEventManager($this->eventManager)
                ->setEvent($this->event);
    }
    
    public function testCanRetrieveDefaultEventManager()
    {
        $provider = new Chain($this->authService);
        $this->assertInstanceOf('Zend\EventManager\EventManager', $provider->getEventManager());
    }
    
    public function testCanRetrieveDefaultEvent()
    {
        $provider = new Chain($this->authService);
        $this->assertInstanceOf('UnicaenAuth\Provider\Identity\ChainEvent', $event = $provider->getEvent());
        $this->assertSame($provider, $event->getTarget());
    }
    
    public function testCanSetServiceLocator()
    {
        $this->assertSame($this->serviceManager, $this->provider->getServiceLocator());
    }
    
    public function testGettingIdentityRolesReturnsEmptyArrayWhenZeroRolesCollected()
    {
        $this->event->expects($this->once())
                ->method('getRoles')
                ->will($this->returnValue(array()));
        
        $this->assertEquals(array(), $this->provider->getIdentityRoles());
        $this->assertEquals(array(), $this->readAttribute($this->provider, 'roles'));
        
        return $this->provider;
    }
    
    /**
     * @depends testGettingIdentityRolesReturnsEmptyArrayWhenZeroRolesCollected
     */
    public function testGettingIdentityRolesDoesNotCollectRolesTwice($provider)
    {
        $this->eventManager->expects($this->never())
                ->method('trigger');
        
        $this->assertEquals(array(), $provider->getIdentityRoles());
    }
    
    public function testGettingIdentityRolesDoesNotIncludeUnknownRoles()
    {
        $this->event->expects($this->once())
                ->method('getRoles')
                ->will($this->returnValue(array('role 1')));
        
        $this->acl->expects($this->once())
                ->method('hasRole')
                ->with('role 1')
                ->will($this->returnValue(false));
        
        $this->assertEquals(array(), $this->provider->getIdentityRoles());
    }
    
    public function testGettingIdentityRolesDoesNotIncludeSameRoleTwice()
    {
        $this->event->expects($this->once())
                ->method('getRoles')
                ->will($this->returnValue(array('role 1', 'role 1')));
        
        $this->acl->expects($this->exactly(2))
                ->method('hasRole')
                ->will($this->returnValue(true));
        
        $this->acl->expects($this->exactly(1))
                ->method('getRole')
                ->will($this->returnValue($role = new NamedRole('role 1')));
        
119
        $this->assertEquals(array('role 1' => $role), $this->provider->getIdentityRoles());
120
121
    }
}