ChainTest.php 4.17 KB
Newer Older
1
2
3
<?php
namespace UnicaenAuthTest\Provider\Identity;

4
use PHPUnit\Framework\TestCase;
5
6
7
8
9
10
11
12
use UnicaenAuth\Provider\Identity\Chain;
use UnicaenAuth\Acl\NamedRole;

/**
 * Description of ChainTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
13
class ChainTest extends TestCase
14
15
16
17
18
19
20
21
{
    protected $provider;
    protected $authorize;
    protected $authService;
    protected $serviceManager;
    protected $eventManager;
    protected $event;
    protected $acl;
22

23
24
25
26
27
28
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
29
30
31
        $this->authorize      = $this->getMock('BjyAuthorize\Service\Authorize', ['getAcl'], [], '', false);
        $this->serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', ['get']);
        $this->authService    = $this->getMock('Zend\Authentication\AuthenticationService', ['getIdentity']);
32
        $this->provider       = new Chain($this->authService);
33
34
35
36
        $this->eventManager   = $this->getMock('Zend\EventManager\EventManager', ['trigger']);
        $this->event          = $this->getMock('UnicaenAuth\Provider\Identity\ChainEvent', ['getRoles']);
        $this->acl            = $this->getMock('Zend\Permissions\Acl\Acl', ['hasRole', 'getRole']);

37
38
39
40
41
42
43
        $this->authorize->expects($this->any())
                ->method('getAcl')
                ->will($this->returnValue($this->acl));

        $this->serviceManager->expects($this->any())
                ->method('get')
                ->will($this->returnValue($this->authorize));
44

45
46
47
48
        $this->provider->setServiceLocator($this->serviceManager)
                ->setEventManager($this->eventManager)
                ->setEvent($this->event);
    }
49

50
51
52
53
54
    public function testCanRetrieveDefaultEventManager()
    {
        $provider = new Chain($this->authService);
        $this->assertInstanceOf('Zend\EventManager\EventManager', $provider->getEventManager());
    }
55

56
57
58
59
60
61
    public function testCanRetrieveDefaultEvent()
    {
        $provider = new Chain($this->authService);
        $this->assertInstanceOf('UnicaenAuth\Provider\Identity\ChainEvent', $event = $provider->getEvent());
        $this->assertSame($provider, $event->getTarget());
    }
62

63
64
65
66
    public function testCanSetServiceLocator()
    {
        $this->assertSame($this->serviceManager, $this->provider->getServiceLocator());
    }
67

68
69
70
71
    public function testGettingIdentityRolesReturnsEmptyArrayWhenZeroRolesCollected()
    {
        $this->event->expects($this->once())
                ->method('getRoles')
72
73
74
75
76
                ->will($this->returnValue([]));

        $this->assertEquals([], $this->provider->getIdentityRoles());
        $this->assertEquals([], $this->readAttribute($this->provider, 'roles'));

77
78
        return $this->provider;
    }
79

80
81
82
83
84
85
86
    /**
     * @depends testGettingIdentityRolesReturnsEmptyArrayWhenZeroRolesCollected
     */
    public function testGettingIdentityRolesDoesNotCollectRolesTwice($provider)
    {
        $this->eventManager->expects($this->never())
                ->method('trigger');
87
88

        $this->assertEquals([], $provider->getIdentityRoles());
89
    }
90

91
92
93
94
    public function testGettingIdentityRolesDoesNotIncludeUnknownRoles()
    {
        $this->event->expects($this->once())
                ->method('getRoles')
95
96
                ->will($this->returnValue(['role 1']));

97
98
99
100
        $this->acl->expects($this->once())
                ->method('hasRole')
                ->with('role 1')
                ->will($this->returnValue(false));
101
102

        $this->assertEquals([], $this->provider->getIdentityRoles());
103
    }
104

105
106
107
108
    public function testGettingIdentityRolesDoesNotIncludeSameRoleTwice()
    {
        $this->event->expects($this->once())
                ->method('getRoles')
109
110
                ->will($this->returnValue(['role 1', 'role 1']));

111
112
113
        $this->acl->expects($this->exactly(2))
                ->method('hasRole')
                ->will($this->returnValue(true));
114

115
116
117
        $this->acl->expects($this->exactly(1))
                ->method('getRole')
                ->will($this->returnValue($role = new NamedRole('role 1')));
118
119

        $this->assertEquals(['role 1' => $role], $this->provider->getIdentityRoles());
120
121
    }
}