public function testBadRelationIgnore()
 {
     $org = $this->em->getRepository('\\OpenLdapObject\\Tests\\Manager\\OrganisationBadRelation')->find('bad-relation');
     $this->assertEquals(count($org->getMember()), 2);
     $listUid = array();
     foreach ($org->getMember() as $member) {
         $listUid[] = $member->getUid();
     }
     $this->assertEquals($listUid, array('pdeparis', 'mdupont'));
 }
Exemple #2
0
 /**
  * Hydrate an entity
  * @param array $data
  * @return mixed Entity
  * @throws \OpenLdapObject\Exception\InvalidHydrateException
  */
 public function hydrate(array &$data)
 {
     $entity = new $this->className();
     // To fix a bug: Ldap column name is always to lower case
     $column = array();
     foreach ($this->analyzer->listColumns() as $name => $info) {
         $column[strtolower($name)] = array_merge($info, array('realname' => $name));
     }
     foreach ($data as $key => $value) {
         $keyLow = strtolower($key);
         if (!array_key_exists($keyLow, $column)) {
             continue;
         }
         if (is_array($value) && $column[$keyLow]['type'] === 'string') {
             throw new InvalidHydrateException('Column ' . $key . ' define as a string but data is array');
         }
         if ($column[$keyLow]['type'] === 'array') {
             // $method = 'add' . Utils::capitalize($column[$keyLow]['realname']);
             if (!is_array($value)) {
                 $data[$key] = array($value);
                 $value = array($value);
             }
             $property = $this->analyzer->getReflection()->getProperty($column[$keyLow]['realname']);
             $isAccessible = $property->isPublic();
             $property->setAccessible(true);
             $property->setValue($entity, new ArrayCollection($value));
             if (!$isAccessible) {
                 $property->setAccessible(false);
             }
         } elseif ($column[$keyLow]['type'] === 'entity') {
             $multi = $this->analyzer->isEntityRelationMultiple($column[$keyLow]['realname']);
             if ($multi) {
                 $property = $this->analyzer->getReflection()->getProperty($column[$keyLow]['realname']);
                 $isAccessible = $property->isPublic();
                 $property->setAccessible(true);
                 // Manage multi entity but only one
                 if (!is_array($value)) {
                     $data[$key] = array($value);
                     $value = array($value);
                 }
                 $property->setValue($entity, new EntityCollection(EntityCollection::DN, $this->em->getRepository($column[$keyLow]['relation']['classname']), $value));
                 if (!$isAccessible) {
                     $property->setAccessible(false);
                 }
             } else {
                 $method = 'set' . Utils::capitalize($column[$keyLow]['realname']);
                 $entity->{$method}($this->repository->read($value));
             }
         } else {
             $method = 'set' . Utils::capitalize($column[$keyLow]['realname']);
             $entity->{$method}($value);
         }
     }
     return $entity;
 }
 public function setUp()
 {
     $connect = new Connection(LDAP_HOST, LDAP_PORT);
     $connect->identify(LDAP_USER, LDAP_PASSWORD);
     $client = $connect->connect();
     $client->setBaseDn(LDAP_BASE_DN);
     try {
         EntityManager::addEntityManager('default', $client);
     } catch (\InvalidArgumentException $e) {
     }
     $this->repository = EntityManager::getEntityManager()->getRepository('\\OpenLdapObject\\Tests\\Manager\\People');
 }
 public function testNewWithEntityCollection()
 {
     $org = new Organisation();
     $org->setCn('test');
     $org->addMember($this->em->getRepository('\\OpenLdapObject\\Tests\\Manager\\People')->find('pdeparis'));
     $this->em->persist($org);
     $this->em->flush();
     $this->em->remove($org);
     $this->em->flush();
 }
 private function initEntityManager($hostname, $port, $base_dn, $dn, $password)
 {
     $port = $port ? $port : self::DEFAULT_PORT;
     $connect = new Connection($hostname, $port);
     if ($dn && $password) {
         $connect->identify($dn, $password);
     }
     $client = $connect->connect();
     $client->setBaseDn($base_dn);
     try {
         \OpenLdapObject\Manager\EntityManager::addEntityManager('default', $client, true);
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
 private function getNewDn($entity, $currentData, EntityAnalyzer $analyzer)
 {
     $index = $analyzer->getIndex();
     if ($index === false) {
         throw new InflushableException('Entity ' . get_class($entity) . 'have no index');
     }
     $dnPiece = array();
     $dnPiece[] = $index . '=' . $currentData[$index];
     if (is_string($analyzer->getBaseDn())) {
         $dnPiece[] = $analyzer->getBaseDn();
     }
     if (is_string($this->em->getClient()->getBaseDn())) {
         $dnPiece[] = $this->em->getClient()->getBaseDn();
     }
     return implode(',', $dnPiece);
 }
 public function boot()
 {
     parent::boot();
     if (!is_null($this->container->getParameter('ldap_object.host'))) {
         $connect = new Connection($this->container->getParameter('ldap_object.host'), $this->container->getParameter('ldap_object.port'));
         if (!is_null($this->container->getParameter('ldap_object.dn')) && !is_null($this->container->getParameter('ldap_object.password'))) {
             $connect->identify($this->container->getParameter('ldap_object.dn'), $this->container->getParameter('ldap_object.password'));
         }
         $client = $connect->connect();
         $client->setBaseDn($this->container->getParameter('ldap_object.base_dn'));
         try {
             EntityManager::addEntityManager('default', $client);
         } catch (Exception $e) {
             // Nothing
         }
     }
 }
 /**
  * @expectedException OpenLdapObject\Exception\InvalidEntityException
  */
 public function testAddBadEntity()
 {
     $org = new Organisation();
     $org->setCn('test');
     $org->addMember($this->em->getRepository('\\OpenLdapObject\\Tests\\Manager\\Organisation')->find('state'));
 }
 public function getRepository($entityName)
 {
     return $this->em->getRepository($entityName);
 }