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

use PHPUnit_Framework_TestCase;
5
6
use UnicaenApp\Entity\Ldap\People;
use UnicaenAppTest\Entity\Ldap\TestAsset\People as PeopleTestAsset;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use UnicaenAuth\Authentication\Storage\Ldap;
use Zend\ServiceManager\ServiceManager;

/**
 * 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;
    
    /**
     * 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();
        
Bertrand Gauthier's avatar
Bertrand Gauthier committed
30
        $this->mapper = $mapper = $this->getMock('UnicaenApp\Mapper\Ldap\People', array('findOneByUsername'));
31
32
        
        $this->serviceManager = new ServiceManager();
33
34
        $this->serviceManager->setFactory('ldap_people_mapper', function() use ($mapper) {
            return $mapper;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        });
        $this->serviceManager->setFactory('unicaen-auth_module_options', function() use ($options) {
            return $options;
        });
        
        $this->storage = new Ldap();
        $this->storage->setMapper($this->mapper)
                      ->setServiceManager($this->serviceManager);
    }
    
    public function testCanRetrieveDefaultInnerStorage()
    {
        $this->assertInstanceOf('Zend\Authentication\Storage\StorageInterface', $this->storage->getStorage());
    }
    
50
51
52
53
54
55
56
57
58
59
60
    public function testCanRetrieveDefaultMapperFromServiceManager()
    {
        $this->serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', array('get'));
        $this->serviceManager->expects($this->once())
                             ->method('get')
                             ->with('ldap_people_mapper');
        $this->storage = new Ldap();
        $this->storage->setServiceManager($this->serviceManager);
        $this->storage->getMapper();
    }
    
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
    public function testCanWriteToInnerStorage()
    {
        $this->storage->write($content = 'content');
        $this->assertFalse($this->storage->isEmpty());
        $this->assertFalse($this->storage->getStorage()->isEmpty());
        $this->assertEquals($content, $this->storage->getStorage()->read());
        
        $this->storage->clear();
        $this->assertTrue($this->storage->isEmpty());
        $this->assertTrue($this->storage->getStorage()->isEmpty());
    }
    
    public function testCanRetrieveMapperFromLdapService()
    {
        $this->assertSame($this->mapper, $this->storage->getMapper());
    }
    
    public function testCanRetrieveOptionsFromServiceManager()
    {
        $this->assertSame($this->options, $this->storage->getOptions());
    }
    
    public function testReadingReturnsNullIfInnerStorageIsEmpty()
    {
        $this->mapper->expects($this->never())
                     ->method('findOneByUsername');
        
        $this->storage->clear();
        $result = $this->storage->read();
        $this->assertNull($result);
    }
    
    public function testReadingReturnsNullIfNoUserFound()
    {
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
                     ->will($this->returnValue(null));
        
        $this->storage->write('username');
        $result = $this->storage->read();
        $this->assertNull($result);
    }
    
    public function testReadingReturnsFoundUser()
    {
106
        $entity = new People(PeopleTestAsset::$data1);
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
                     ->will($this->returnValue($entity));
        
        $this->storage->write($login = 'username');
        $result = $this->storage->read();
        $this->assertSame($entity, $result);
    }
    
    public function testReadingDoesNotFetchUserTwice()
    {
        $this->mapper->expects($this->once())
                     ->method('findOneByUsername')
121
                     ->will($this->returnValue(new People(PeopleTestAsset::$data1)));
122
123
124
125
126
127
128
129
130
        $this->storage->write('username');
        $firestResult = $this->storage->read();
        
        $this->mapper->expects($this->never())
                     ->method('findOneByUsername');
        $nextResult = $this->storage->read();
        $this->assertSame($firestResult, $nextResult);
    }
}