LdapTest.php 6.33 KB
Newer Older
1
2
3
4
<?php
namespace UnicaenAuthTest\Authentication\Storage;

use PHPUnit_Framework_TestCase;
5
use UnicaenApp\Entity\Ldap\People;
6
use UnicaenAuth\Entity\Ldap\People as AuthPeople;
7
use UnicaenAppTest\Entity\Ldap\TestAsset\People as PeopleTestAsset;
8
use UnicaenAuth\Authentication\Storage\Ldap;
9
use UnicaenAuth\Authentication\Storage\ChainEvent;
10
11
12
13
14
15
16
17
18
19
20
21

/**
 * Description of LdapTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class LdapTest extends PHPUnit_Framework_TestCase
{
    protected $storage;
    protected $mapper;
    protected $serviceManager;
    protected $options;
22
23
    protected $event;
    protected $innerStorage;
24

25
26
27
28
29
30
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
31
        $this->options        = new \UnicaenAuth\Options\ModuleOptions();
32
33
34
        $this->serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', ['get']);
        $this->innerStorage   = $this->getMock('Zend\Authentication\Storage\Session', ['write', 'clear', 'read']);
        $this->mapper         = $this->getMock('UnicaenApp\Mapper\Ldap\People', ['findOneByUsername']);
35
36
37
        $this->event          = new ChainEvent();
        $this->storage        = new Ldap();

38
        $this->storage->setMapper($this->mapper)
39
                      ->setServiceLocator($this->serviceManager)
40
                      ->setStorage($this->innerStorage);
41
    }
42

43
44
    public function testCanRetrieveDefaultInnerStorage()
    {
45
46
        $storage = new Ldap();
        $this->assertInstanceOf('Zend\Authentication\Storage\StorageInterface', $storage->getStorage());
47
    }
48

49
    public function testCanRetrieveMapperFromLdapService()
50
51
52
    {
        $this->serviceManager->expects($this->once())
                             ->method('get')
53
54
                             ->with('ldap_people_mapper')
                             ->will($this->returnValue('result'));
55

56
57
        $this->storage->setMapper(null);
        $this->assertEquals('result', $this->storage->getMapper());
58
    }
59

60
    public function testCanRetrieveOptionsFromServiceManager()
61
    {
62
63
64
65
        $this->serviceManager->expects($this->once())
                             ->method('get')
                             ->with('unicaen-auth_module_options')
                             ->will($this->returnValue($options = new \UnicaenAuth\Options\ModuleOptions()));
66

67
        $this->assertSame($options, $this->storage->getOptions());
68
    }
69

70
    public function testCanWrite()
71
    {
72
        $this->event->setParam('contents', 12);
73

74
75
76
        $this->innerStorage->expects($this->once())
                     ->method('write')
                     ->with(12);
77

78
        $this->storage->write($this->event);
79
    }
80

81
    public function testCanClear()
82
    {
83
84
        $this->innerStorage->expects($this->once())
                           ->method('clear');
85

86
        $this->storage->clear($this->event);
87
    }
88

89
90
    public function testReadingReturnsNullIfInnerStorageIsEmpty()
    {
91
92
93
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue(null));
94
95
        $this->mapper->expects($this->never())
                     ->method('findOneByUsername');
96

97
        $this->storage->read($this->event);
98
        $this->assertEquals(['ldap' => null], $this->event->getContents());
99
    }
100

101
102
    public function testReadingReturnsNullIfNoUserFound()
    {
103
104
105
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue(12));
106
107
108
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
                     ->will($this->returnValue(null));
109

110
        $this->storage->read($this->event);
111
        $this->assertEquals(['ldap' => null], $this->event->getContents());
112
    }
113

114
115
    public function getException()
    {
116
117
118
119
        return [
            [new \Zend\Ldap\Exception\LdapException()],
            [new \UnicaenApp\Exception()],
        ];
120
    }
121

122
123
124
125
126
127
128
129
130
131
132
    /**
     * @dataProvider getException
     */
    public function testReadingReturnsNullIfFindByUsernameThrowsException($exception)
    {
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue(12));
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
                     ->will($this->throwException($exception));
133

134
        $this->storage->read($this->event);
135
        $this->assertEquals(['ldap' => null], $this->event->getContents());
136
    }
137

138
    public function testReadingReturnsEntityIfUserFoundByUsername()
139
    {
140
        $entity = new People(PeopleTestAsset::$data1);
141

142
143
144
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue('username'));
145
146
147
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
                     ->will($this->returnValue($entity));
148

149
        $this->storage->read($this->event);
150
        $this->assertEquals(['ldap' => new AuthPeople($entity)], $this->event->getContents());
151
    }
152

153
    public function testReadingReturnsEntityInInnerStorageWithoutFetching()
154
    {
155
        $entity = new People(PeopleTestAsset::$data1);
156

157
158
159
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue($entity));
160
161
        $this->mapper->expects($this->never())
                     ->method('findOneByUsername');
162

163
        $this->storage->read($this->event);
164
        $this->assertEquals(['ldap' => new AuthPeople($entity)], $this->event->getContents());
165
    }
166

167
168
169
    public function testReadingReturnsResolvedEntityWithoutReadingInnerStorage()
    {
        $entity = new People(PeopleTestAsset::$data1);
170

171
172
173
174
175
176
        $this->innerStorage->expects($this->once())
                     ->method('read')
                     ->will($this->returnValue(12));
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
                     ->will($this->returnValue($entity));
177

178
        $firstResult = $this->storage->read($this->event);
179

180
181
        $this->innerStorage->expects($this->never())
                     ->method('read');
182

183
        $nextResult = $this->storage->read($this->event);
184

185
        $this->assertSame($firstResult, $nextResult);
186
187
    }
}