Commit d0195c34 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Storage d'authentification Ldap : tests unitaires ; légères améliorations.

parent 9b715b05
......@@ -27,6 +27,11 @@ class Ldap implements Storage\StorageInterface, ServiceManagerAwareInterface
* @var LdapPeopleService
*/
protected $mapper;
/**
* @var ModuleOptions
*/
protected $options;
/**
* @var People
......
<?php
namespace UnicaenAuthTest\Authentication\Storage;
use PHPUnit_Framework_TestCase;
use UnicaenAppTest\Entity\Ldap\TestAsset\People;
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();
$this->mapper = $mapper = $this->getMock('UnicaenApp\Service\Ldap\People', array('findOneByUsername'));
$this->serviceManager = new ServiceManager();
$this->serviceManager->setFactory('ldap_people_service', function() use ($mapper) {
$service = new \UnicaenApp\Service\Ldap\People();
$service->setMapper($mapper);
return $service;
});
$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());
}
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()
{
$entity = new People();
$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')
->will($this->returnValue(new People()));
$this->storage->write('username');
$firestResult = $this->storage->read();
$this->mapper->expects($this->never())
->method('findOneByUsername');
$nextResult = $this->storage->read();
$this->assertSame($firestResult, $nextResult);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment