Permalink
Browse files

catch move single child by assignment. move replace child in multiple…

… children to ChildrenTest and actually test replacing (fails)
  • Loading branch information...
1 parent 0ca3ac3 commit b6733b9ba149c6213d65feae19e2c8cd0aaafffa @dbu dbu committed Feb 28, 2013
@@ -20,6 +20,7 @@
namespace Doctrine\ODM\PHPCR;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
+use PHPCR\Util\PathHelper;
use PHPCR\Util\NodeHelper;
use PHPCR\PathNotFoundException;
use Doctrine\ODM\PHPCR\Exception\CascadeException;
@@ -1169,6 +1170,10 @@ private function computeChildChanges($mapping, $child, $parentId, $nodename, $pa
break;
case self::STATE_DETACHED:
throw new \InvalidArgumentException('A detached document was found through a child relationship during cascading a persist operation: '.self::objToStr($child, $this->dm));
+ default:
+ if (PathHelper::getParentPath($this->getDocumentId($child)) !== $parentId) {
+ throw PHPCRException::cannotMoveByAssignment(self::objToStr($child, $this->dm));
+ }
}
return $child;
@@ -225,6 +225,36 @@ public function testChildSetNull()
$this->assertNull($parent->child);
}
+ /**
+ * @expectedException \Doctrine\ODM\PHPCR\PHPCRException
+ */
+ public function testMoveByAssignment()
+ {
+ $original = new ChildTestObj();
+ $child = new ChildChildTestObj();
+ $original->name = 'Parent';
+ $original->id = '/functional/original';
+ $original->child = $child;
+ $child->name = 'Child';
+
+ $this->dm->persist($original);
+
+ $other = new ChildTestObj();
+ $other->name = 'newparent';
+ $other->id = '/functional/newlocation';
+ $this->dm->persist($other);
+
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $original = $this->dm->find($this->type, '/functional/original');
+ $other = $this->dm->find($this->type, '/functional/newlocation');
+
+ $other->child = $original->child;
+
+ $this->dm->flush();
+ }
+
public function testChildReplace()
{
$parent = new ChildTestObj();
@@ -248,16 +278,6 @@ public function testChildReplace()
$parent = $this->dm->find($this->type, '/functional/childtest');
$this->assertEquals('new name', $parent->child->name);
-
- $newerChild = new ChildChildTestObj();
- $newerChild->name = 'newer name';
- $parent->children['test2'] = $newerChild;
-
- $this->dm->flush();
- $this->dm->clear();
-
- $parent = $this->dm->find($this->type, '/functional/childtest');
- $this->assertEquals('newer name', $parent->children['test2']->name);
}
public function testModificationAfterPersist()
@@ -342,8 +362,6 @@ class ChildTestObj
public $name;
/** @PHPCRODM\Child(name="test", cascade="persist") */
public $child;
- /** @PHPCRODM\Children(cascade="persist") */
- public $children;
}
/**
@@ -5,6 +5,7 @@
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface,
Doctrine\ODM\PHPCR\DocumentRepository,
Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
+use Doctrine\ODM\PHPCR\ChildrenCollection;
use PHPCR\RepositoryInterface;
use Doctrine\Common\Collections\ArrayCollection;
@@ -224,6 +225,49 @@ public function testModifyChildren()
$this->dm->clear();
}
+ public function testReplaceChildren()
+ {
+ /** @var $parent ChildrenTestObj */
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
+ $this->assertCount(4, $parent->allChildren);
+
+ $parent->allChildren->remove('child-a');
+
+ $newChild = new ChildrenTestObj();
+ $newChild->name = 'child-a';
+
+ $parent->allChildren->add($newChild);
+
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
+ $this->assertTrue($parent->allChildren->containsKey('child-a'));
+ $this->assertEquals('child-a', $parent->allChildren['child-a']->name);
+ }
+
+ /**
+ * @expectedException \Doctrine\ODM\PHPCR\PHPCRException
+ */
+ public function testMoveByAssignment()
+ {
+ $other = new ChildrenTestObj();
+ $other->id = '/functional/other';
+ $this->dm->persist($other);
+ $this->dm->flush();
+ $this->dm->clear();
+
+
+ /** @var $parent ChildrenTestObj */
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
+ $this->assertCount(4, $parent->allChildren);
+
+ $other = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/other');
+ $other->allChildren->add($parent->allChildren['child-a']);
+
+ $this->dm->flush();
+ }
+
/**
* @depends testModifyChildren
*/
@@ -378,7 +422,10 @@ class ChildrenTestObj
/** @PHPCRODM\Children(filter="*a", fetchDepth=1, cascade="persist") */
public $aChildren;
- /** @PHPCRODM\Children(fetchDepth=2, cascade="persist") */
+ /**
+ * @var \Doctrine\ODM\PHPCR\ChildrenCollection
+ * @PHPCRODM\Children(fetchDepth=2, cascade="persist")
+ */
public $allChildren;
}

0 comments on commit b6733b9

Please sign in to comment.