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(); }
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')); }
/** * 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; }
/** * @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); }