| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DoL\LdapBundle\Tests\Ldap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DoL\LdapBundle\Hydrator\HydratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DoL\LdapBundle\Ldap\LdapManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Security\Core\User\UserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @covers \DoL\LdapBundle\Ldap\LdapManager | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class LdapManagerTest extends \PHPUnit_Framework_TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** @var array */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     protected $paramSets; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @var \DoL\LdapBundle\Driver\LdapDriverInterface|\PHPUnit_Framework_MockObject_MockObject | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     protected $driver; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var HydratorInterface|\PHPUnit_Framework_MockObject_MockObject | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected $hydrator; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var LdapManager | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     protected $ldapManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Sets up the fixture, for example, opens a network connection. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * This method is called before a test is executed. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     protected function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->paramSets = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             'server1' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 'driver' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                     // SOME ATTRIBUTES | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                 'user' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                     'baseDn' => 'ou=Groups,dc=example,dc=com', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                     'filter' => '(attr0=value0)', | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                     'attributes' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                         [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                             'ldap_attr' => 'uid', | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                             'user_method' => 'setUsername', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->driver = $this->getMock('DoL\LdapBundle\Driver\LdapDriverInterface'); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->hydrator = $this->getMock('DoL\LdapBundle\Hydrator\HydratorInterface'); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->ldapManager = new LdapManager($this->driver, $this->hydrator, $this->paramSets); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @covers \DoL\LdapBundle\Ldap\LdapManager::findUserByUsername | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 66 |  | View Code Duplication |     public function testFindUserByUsername() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $username = 'test_username'; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $ldapResponse = $this->ldapResponse($username); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $this->driver | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             ->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             ->method('search') | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             ->with($this->equalTo('ou=Groups,dc=example,dc=com'), | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 $this->equalTo('(&(attr0=value0)(uid=test_username))') | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             ->will($this->returnValue($ldapResponse)) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $resultUser = $this->ldapManager->findUserByUsername($username); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         self::assertEquals($username, $resultUser->getUsername()); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @covers \DoL\LdapBundle\Ldap\LdapManager::findUserBy | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 89 |  | View Code Duplication |     public function testFindUserBy() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $username = 'test_username'; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $ldapResponse = $this->ldapResponse($username); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $this->driver | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             ->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             ->method('search') | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             ->with($this->equalTo('ou=Groups,dc=example,dc=com'), | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 $this->equalTo('(&(attr0=value0)(uid=test_username))') | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             ->will($this->returnValue($ldapResponse)) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         $criteria = ['uid' => 'test_username']; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         $resultUser = $this->ldapManager->findUserBy($criteria); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         self::assertEquals($username, $resultUser->getUsername()); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @covers \DoL\LdapBundle\Ldap\LdapManager::buildFilter | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     public function testBuildFilter() | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         $reflectionClass = new \ReflectionClass('DoL\LdapBundle\Ldap\LdapManager'); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         $method = $reflectionClass->getMethod('buildFilter'); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         $method->setAccessible(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $params = $reflectionClass->getProperty('params'); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         $params->setAccessible(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         $params->setValue($this->ldapManager, $this->paramSets['server1']['user']); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         $criteria = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             'attr1' => 'value1', | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             'attr2' => 'value2', | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         $expected = '(&(attr0=value0)(attr1=value1)(attr2=value2))'; | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         self::assertEquals($expected, $method->invoke($this->ldapManager, $criteria)); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * @covers \DoL\LdapBundle\Ldap\LdapManager::bind | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 134 |  | View Code Duplication |     public function testBind() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $password = 'password'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         /** @var UserInterface $user */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $this->driver->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ->method('bind') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             ->with($user, $this->equalTo($password)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             ->will($this->returnValue(true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  |         self::assertTrue($this->ldapManager->bind($user, $password)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * @param $username | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     protected function ldapResponse($username) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $entry = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             'dn' => 'ou=group, dc=host, dc=foo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             'uid' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 'count' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 0 => $username, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $entries = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             'count' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $entry, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $user->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             ->method('getUsername') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             ->willReturn($username) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $this->hydrator->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             ->method('hydrate') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             ->with($entry) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             ->willReturn($user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |         return $entries; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 183 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 184 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.