Skip to content
Browse files

[Manager] Refactor test

* CS
* Improve legilibility
  • Loading branch information...
1 parent e07aca3 commit 498cd16bf7b804e7d8f3a02a62c8aeef8d5e1892 @Maks3w committed
Showing with 82 additions and 57 deletions.
  1. +82 −57 Tests/Ldap/LdapManagerTest.php
View
139 Tests/Ldap/LdapManagerTest.php
@@ -7,6 +7,9 @@
class LdapManagerTest extends \PHPUnit_Framework_TestCase
{
+ /** @var array */
+ protected $params;
+
/**
* @var \FR3D\LdapBundle\Driver\LdapConnectionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
@@ -28,7 +31,7 @@ class LdapManagerTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
- $params = array(
+ $this->params = array(
'baseDn' => 'ou=Groups,dc=example,dc=com',
'filter' => '(attr0=value0)',
'attributes' => array(
@@ -46,7 +49,7 @@ protected function setUp()
->method('createUser')
->will($this->returnValue(new TestUser()));
- $this->ldapManager = new LdapManager($this->connection, $this->userManager, $params);
+ $this->ldapManager = new LdapManager($this->connection, $this->userManager, $this->params);
}
/**
@@ -54,22 +57,12 @@ protected function setUp()
*/
public function testConstruct()
{
- $params = array(
- 'baseDn' => 'ou=Groups,dc=example,dc=com',
- 'filter' => '(attr0=value0)',
- 'attributes' => array(
- array(
- 'ldap_attr' => 'uid',
- 'user_method' => 'setUsername',
- ),
- array(
- 'ldap_attr' => 'mail',
- 'user_method' => 'setEmail',
- ),
- ),
+ $this->params['attributes'][] = array(
+ 'ldap_attr' => 'mail',
+ 'user_method' => 'setEmail',
);
- $this->ldapManager = new LdapManager($this->connection, $this->userManager, $params);
+ $this->ldapManager = new LdapManager($this->connection, $this->userManager, $this->params);
$reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
$propertyLdapAttributes = $reflectionClass->getProperty('ldapAttributes');
@@ -89,12 +82,24 @@ public function testFindUserByUsername()
{
$username = 'test_username';
- $entry = array('count' => 1, array('dn' => 'ou=group, dc=host, dc=foo', 'uid' => array('test_username')));
+ $entries = array(
+ 'count' => 1,
+ array(
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'uid' => array(
+ 'count' => 1,
+ 0 => $username,
+ ),
+ ),
+ );
- $this->connection->expects($this->once())
- ->method('search')
- ->with($this->equalTo('ou=Groups,dc=example,dc=com'), $this->equalTo('(&(attr0=value0)(uid=test_username))'), $this->equalTo(array('uid')))
- ->will($this->returnValue($entry));
+ $this->connection
+ ->expects($this->once())
+ ->method('search')
+ ->with($this->equalTo('ou=Groups,dc=example,dc=com'),
+ $this->equalTo('(&(attr0=value0)(uid=test_username))'),
+ $this->equalTo(array('uid')))
+ ->will($this->returnValue($entries));
$resultUser = $this->ldapManager->findUserByUsername($username);
@@ -106,17 +111,31 @@ public function testFindUserByUsername()
*/
public function testFindUserBy()
{
+ $username = 'test_username';
+
$user = new TestUser();
- $user->setUsername('test_username');
+ $user->setUsername($username);
+
+ $entries = array(
+ 'count' => 1,
+ array(
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'uid' => array(
+ 'count' => 1,
+ 0 => $username,
+ ),
+ ),
+ );
- $entry = array('count' => 1, array('dn' => 'ou=group, dc=host, dc=foo', 'uid' => array('test_username')));
+ $this->connection
+ ->expects($this->once())
+ ->method('search')
+ ->with($this->equalTo('ou=Groups,dc=example,dc=com'),
+ $this->equalTo('(&(attr0=value0)(uid=test_username))'),
+ $this->equalTo(array('uid')))
+ ->will($this->returnValue($entries));
- $this->connection->expects($this->once())
- ->method('search')
- ->with($this->equalTo('ou=Groups,dc=example,dc=com'), $this->equalTo('(&(attr0=value0)(uid=test_username))'), $this->equalTo(array('uid')))
- ->will($this->returnValue($entry));
-
- $criteria = array('uid' => 'test_username');
+ $criteria = array('uid' => 'test_username');
$resultUser = $this->ldapManager->findUserBy($criteria);
$this->assertEquals($user->getUsername(), $resultUser->getUsername());
@@ -132,8 +151,8 @@ public function testBuildFilter()
$method->setAccessible(true);
$criteria = array(
- 'attr1' => 'value1',
- 'attr2' => 'value2',
+ 'attr1' => 'value1',
+ 'attr2' => 'value2',
);
$expected = '(&(attr0=value0)(attr1=value1)(attr2=value2))';
@@ -145,6 +164,8 @@ public function testBuildFilter()
*/
public function testHydrate()
{
+ $username = 'test_username';
+
$reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
$method = $reflectionClass->getMethod('hydrate');
$method->setAccessible(true);
@@ -152,13 +173,17 @@ public function testHydrate()
$user = new TestUser();
$entry = array(
- 'dn' => 'ou=group, dc=host, dc=foo',
- 'uid' => array('test_username')
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'count' => 1,
+ 'uid' => array(
+ 'count' => 1,
+ 0 => $username,
+ ),
);
$method->invoke($this->ldapManager, $user, $entry);
- $this->assertEquals('test_username', $user->getUsername());
+ $this->assertEquals($username, $user->getUsername());
$this->assertTrue($user->isEnabled());
}
@@ -167,36 +192,32 @@ public function testHydrate()
*/
public function testHydrateArray()
{
- $params = array(
- 'baseDn' => 'ou=Groups,dc=example,dc=com',
- 'filter' => '(attr0=value0)',
- 'attributes' => array(
- array(
- 'ldap_attr' => 'roles',
- 'user_method' => 'setRoles',
- )
+ $this->params['attributes'] = array(
+ array(
+ 'ldap_attr' => 'roles',
+ 'user_method' => 'setRoles',
),
);
- $this->ldapManager = new LdapManager($this->connection, $this->userManager, $params);
-
- $reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
- $method = $reflectionClass->getMethod('hydrate');
- $method->setAccessible(true);
-
- $user = new TestUser();
+ $user = new TestUser();
$roles = array(
'count' => 3,
- 0 => 'ROLE1',
- 1 => 'ROLE2',
- 2 => 'ROLE3'
+ 0 => 'ROLE1',
+ 1 => 'ROLE2',
+ 2 => 'ROLE3',
);
$entry = array(
- 'dn' => 'ou=group, dc=host, dc=foo',
- 'roles' => $roles,
+ 'dn' => 'ou=group, dc=host, dc=foo',
+ 'roles' => $roles,
);
+ $this->ldapManager = new LdapManager($this->connection, $this->userManager, $this->params);
+
+ $reflectionClass = new \ReflectionClass('FR3D\LdapBundle\Ldap\LdapManager');
+ $method = $reflectionClass->getMethod('hydrate');
+ $method->setAccessible(true);
+
$method->invoke($this->ldapManager, $user, $entry);
$this->assertEquals(array_slice($roles, 1), $user->getRoles());
@@ -208,15 +229,19 @@ public function testHydrateArray()
*/
public function testBind()
{
+ $dn = 'dn=test_username';
+ $password = 'password';
+
$user = new TestUser();
- $user->setDn('dn=test_username');
+ $user->setDn($dn);
$this->connection->expects($this->once())
->method('bind')
- ->with($this->equalTo('dn=test_username'), $this->equalTo('password'))
+ ->with($this->equalTo($dn),
+ $this->equalTo($password))
->will($this->returnValue(true));
- $this->assertTrue($this->ldapManager->bind($user, 'password'));
+ $this->assertTrue($this->ldapManager->bind($user, $password));
}
public function testFilterEscapeBasicOperation()

0 comments on commit 498cd16

Please sign in to comment.
Something went wrong with that request. Please try again.