LdapDbTest.php 6.27 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
<?php
namespace UnicaenAuthTest\Authentication\Storage;

use PHPUnit_Framework_TestCase;
use UnicaenApp\Entity\Ldap\People;
use UnicaenAppTest\Entity\Ldap\TestAsset\People as PeopleTestAsset;
use UnicaenAuth\Authentication\Storage\Db;
use UnicaenAuth\Authentication\Storage\Ldap;
use UnicaenAuth\Authentication\Storage\LdapDb;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Entity\User;

/**
 * Description of LdapDbTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class LdapDbTest extends PHPUnit_Framework_TestCase
{
    protected $storage;
    protected $serviceManager;
    protected $options;
    protected $dbStorage;
    protected $ldapStorage;
    
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->options = $options = new \UnicaenAuth\Options\ModuleOptions();
        
        $this->serviceManager = new ServiceManager();
        $this->serviceManager->setFactory('unicaen-auth_module_options', function() use ($options) {
            return $options;
        });
        
        $this->dbStorage   = $this->getMock('UnicaenAuth\Authentication\Storage\Db', array('read'));
        $this->ldapStorage = $this->getMock('UnicaenAuth\Authentication\Storage\Ldap', array('read'));
        
        $this->storage = new LdapDb();
        $this->storage->setDbStorage($this->dbStorage)
                      ->setLdapStorage($this->ldapStorage)
                      ->setServiceManager($this->serviceManager);
    }
    
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    public function testServiceManagerIsNullByDefault()
    {
        $storage = new LdapDb();
        $this->assertNull($storage->getServiceManager());
    }
    
    public function testCanRetrieveDefaultOptionsFromServiceManager()
    {
        $this->serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', array('get'));
        $this->serviceManager->expects($this->once())
                             ->method('get')
                             ->will($this->returnValue(new \UnicaenAuth\Options\ModuleOptions()));
        $this->storage->setServiceManager($this->serviceManager);
        $this->storage->getOptions();
    }
    
64
65
66
67
68
69
70
    public function testInnerStoragesAreNullByDefault()
    {
        $storage = new LdapDb();
        $this->assertNull($storage->getDbStorage());
        $this->assertNull($storage->getLdapStorage());
    }
    
71
    public function testStorageIsEmptyOnlyIfBothInnerStoragesAreEmpty()
72
73
    {
        $this->storage->getDbStorage()->write('content');
74
        $this->storage->getLdapStorage()->write('content');
75
76
        $this->assertFalse($this->storage->isEmpty());
        
77
        $this->storage->clear();
78
79
80
        $this->storage->getLdapStorage()->write('content');
        $this->assertFalse($this->storage->isEmpty());
        
81
82
83
84
85
        $this->storage->clear();
        $this->storage->getDbStorage()->write('content');
        $this->assertFalse($this->storage->isEmpty());
        
        $this->storage->clear();
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        $this->assertTrue($this->storage->isEmpty());
    }
    
    public function testCanWriteToInnerStorages()
    {
        $entity = new User();
        
        $this->storage->setDbStorage(new Db());
        $this->storage->setLdapStorage(new Ldap());
        
        $this->storage->write($entity);
        $this->assertFalse($this->storage->isEmpty());
        $this->assertFalse($this->storage->getDbStorage()->isEmpty());
        $this->assertFalse($this->storage->getLdapStorage()->isEmpty());
        $this->assertEquals($entity, $this->storage->getDbStorage()->read());
        $this->assertEquals($entity, $this->storage->getLdapStorage()->read());
        
        $this->storage->clear();
        $this->assertTrue($this->storage->isEmpty());
        $this->assertTrue($this->storage->getDbStorage()->isEmpty());
        $this->assertTrue($this->storage->getLdapStorage()->isEmpty());
    }
    
    public function testReadingReturnsStoredDbUser()
    {
        $entity = new User();
        
        $this->dbStorage->expects($this->once())
                        ->method('read')
                        ->will($this->returnValue($entity));
        
        $result = $this->storage->read();
        $this->assertSame($entity, $result);
    }
    
    public function testReadingReturnsLdapUserAdapterIfNoDbUserFound()
    {
        $entity = new People(PeopleTestAsset::$data1);
        
        $this->dbStorage->expects($this->once())
                        ->method('read')
                        ->will($this->returnValue(null));
        $this->ldapStorage->expects($this->once())
                        ->method('read')
                        ->will($this->returnValue($entity));
        
        $result = $this->storage->read();
        $this->assertInstanceOf('UnicaenAuth\Entity\Ldap\PeopleAdapter', $result);
        $this->assertInstanceOf('UnicaenApp\Entity\Ldap\People', $result);
135
        $this->assertEquals($result->getId(), $entity->getUid()); 
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    }
    
    public function testReadingReturnsNullIfNoUserFoundInDbOrLdap()
    {
        $this->dbStorage->expects($this->once())
                        ->method('read')
                        ->will($this->returnValue(null));
        $this->ldapStorage->expects($this->once())
                          ->method('read')
                          ->will($this->returnValue(null));
                
        $result = $this->storage->read();
        $this->assertNull($result);
    }
    
    public function testReadingReturnsLdapUserAdapterIfDUserFoundInDbAndLdap()
    {
        $dbEntity   = new User();
        $dbEntity->setId(12)
                 ->setPassword('ldap'); // requis
        
        $ldapEntity = new People(PeopleTestAsset::$data1);
        
        $this->dbStorage->expects($this->once())
                        ->method('read')
                        ->will($this->returnValue($dbEntity));
        $this->ldapStorage->expects($this->once())
                        ->method('read')
                        ->will($this->returnValue($ldapEntity));
        
        $result = $this->storage->read();
        $this->assertInstanceOf('UnicaenAuth\Entity\Ldap\PeopleAdapter', $result);
        $this->assertInstanceOf('UnicaenApp\Entity\Ldap\People', $result);
        $this->assertEquals($result->getId(), $dbEntity->getId());
    }
}