/** * Tests exceptions handling while saving a Ldap node * * @return void */ public function testSaveExceptionHandling() { $manager = new Manager($this->minimal, $this->driver); $node = new Node(); $attr = new NodeAttribute('attr'); $attr->add('value'); $node->mergeAttribute($attr); $this->assertBindingFirst($manager, 'save', array($node)); $manager->connect(); $this->assertBindingFirst($manager, 'save', array($node)); $manager->bind(); try { $manager->save($node); $this->fail('Node Dn has to be set for saving it'); } catch (PersistenceException $e) { $this->assertRegExp('/Cannot save: dn missing for the entry/', $e->getMessage()); } $node->setDn('test'); $this->driver->getConnection()->setFailure(Connection::ERR_DEFAULT, Connection::FAIL_COND_PERSIST); try { $manager->save($node); $this->fail('Underlying Ldap connection failed to add the entry'); } catch (PersistenceException $e) { $this->assertRegExp('/could not add entry/', $e->getMessage()); } $this->assertEquals(array('attr' => array('value')), $node->getDiffAdditions(), 'In case any failure happens while persisting, snapshot is not performed'); $this->driver->getConnection()->setFailure(Connection::ERR_DEFAULT, Connection::FAIL_COND_PERSIST); $entry = new Entry('test', array('other' => array('value2'))); $this->driver->getConnection()->stackResults(array($entry)); try { $manager->save($node); $this->fail('Underlying Ldap connection failed to update the entry'); } catch (PersistenceException $e) { $this->assertRegExp('/could not add attributes/', $e->getMessage()); } $this->assertEquals(array('attr' => array('value')), $node->getDiffAdditions(), 'In case any failure happens while updating, snapshot is not performed'); $this->assertFalse($manager->save($node), 'No more Ldap failure - A correct node is created and saved in the Ldap store'); $this->assertEquals(array(), $node->getDiffAdditions(), 'Save occured so snapshot took place'); }
/** * Tests retrieving raw node attributes for Ldap persistence * * @return void */ public function testGetRawAttributes() { $node = new Node(); $this->assertEquals(array(), $node->getRawAttributes(), 'No attributes set so an empty array is retrieved'); $node->mergeAttribute(new NodeAttribute('attr1')); $attr = new NodeAttribute('attr2'); $attr->add(array('value1', 'value2')); $node->mergeAttribute($attr); $attr = new NodeAttribute('attr3'); $attr->add('value3'); $node->mergeAttribute($attr); $this->assertEquals(array('attr1' => array(), 'attr2' => array('value1', 'value2'), 'attr3' => array('value3')), $node->getRawAttributes(), 'All combinations are parsed correctly'); }
/** * Node factory * * @param string $dn Distinguished name for the node * @param array $attributes Array of attributes * * @return Node node */ protected function buildNode($dn, $attributes) { $node = new Node(); $node->setDn($dn); foreach ($attributes as $name => $data) { $attr = new NodeAttribute($name); $attr->add($data); $node->mergeAttribute($attr); } return $node; }