Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into remove_duplicate_original_data

Conflicts:
	lib/Doctrine/ODM/PHPCR/UnitOfWork.php
  • Loading branch information...
commit d202d1a1154736df1d62414039c3673521ec1aaa 2 parents 3eb8ca8 + 97d9512
@lsmith77 lsmith77 authored
View
6 lib/Doctrine/ODM/PHPCR/DocumentManager.php
@@ -645,10 +645,14 @@ public function getReferrers($document, $type = null, $name = null)
* Flush all current changes, that is save them within the phpcr session
* and commit that session to permanent storage.
*
- * @param object $document
+ * @param object|array|null $document
*/
public function flush($document = null)
{
+ if (null !== $document && !is_object($document) && !is_array($document)) {
+ throw new \InvalidArgumentException(gettype($document));
+ }
+
$this->errorIfClosed();
$this->unitOfWork->commit($document);
}
View
106 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
@@ -686,62 +686,68 @@ private function getDocumentState($document)
}
/**
- * Detects the changes that need to be persisted
+ * Detects the changes for a single document
*
* @param object $document
- *
* @return void
*/
- private function detectChangedDocuments($document = null)
+ private function computeSingleDocumentChangeSet($document)
{
- if ($document) {
- $state = $this->getDocumentState($document);
- if ($state !== self::STATE_MANAGED && $state !== self::STATE_MOVED) {
- throw new \InvalidArgumentException('Document has to be managed or moved for single computation '.self::objToStr($document, $this->dm));
- }
+ $state = $this->getDocumentState($document);
+ if ($state !== self::STATE_MANAGED && $state !== self::STATE_MOVED) {
+ throw new \InvalidArgumentException('Document has to be managed or moved for single computation '.self::objToStr($document, $this->dm));
+ }
- foreach ($this->scheduledInserts as $insertedDocument) {
- $class = $this->dm->getClassMetadata(get_class($insertedDocument));
- $this->computeChangeSet($class, $insertedDocument);
- }
+ foreach ($this->scheduledInserts as $insertedDocument) {
+ $class = $this->dm->getClassMetadata(get_class($insertedDocument));
+ $this->computeChangeSet($class, $insertedDocument);
+ }
- // Ignore uninitialized proxy objects
- if ($document instanceof Proxy && !$document->__isInitialized()) {
- return;
- }
+ // Ignore uninitialized proxy objects
+ if ($document instanceof Proxy && !$document->__isInitialized()) {
+ return;
+ }
- $oid = spl_object_hash($document);
- if (!isset($this->scheduledInserts[$oid])) {
+ $oid = spl_object_hash($document);
+ if (!isset($this->scheduledInserts[$oid])) {
+ $class = $this->dm->getClassMetadata(get_class($document));
+ $this->computeChangeSet($class, $document);
+ }
+ }
+
+ /**
+ * Detects the changes that need to be persisted
+ *
+ * @return void
+ */
+ private function computeChangeSets()
+ {
+ foreach ($this->identityMap as $document) {
+ $state = $this->getDocumentState($document);
+ if ($state === self::STATE_MANAGED || $state === self::STATE_MOVED) {
$class = $this->dm->getClassMetadata(get_class($document));
$this->computeChangeSet($class, $document);
}
- } else {
- foreach ($this->identityMap as $document) {
- $state = $this->getDocumentState($document);
- if ($state === self::STATE_MANAGED || $state === self::STATE_MOVED) {
- $class = $this->dm->getClassMetadata(get_class($document));
- $this->computeChangeSet($class, $document);
- }
- }
}
}
/**
+ * Get a documents actual data, flattening all the objects to arrays.
+ *
* @param ClassMetadata $class
* @param object $document
- * @return void
+ * @return array
*/
- private function computeChangeSet(ClassMetadata $class, $document)
+ private function getDocumentActualData(ClassMetadata $class, $document)
{
- if ($document instanceof Proxy && !$document->__isInitialized()) {
- return;
- }
-
- $id = $this->getDocumentId($document);
- $oid = spl_object_hash($document);
-
$actualData = array();
foreach ($class->reflFields as $fieldName => $reflProperty) {
+ // do not set the version info fields if they have values, they are not to be managed by the user in write scenarios.
+ if ($fieldName === $class->versionNameField
+ || $fieldName === $class->versionCreatedField
+ ) {
+ continue;
+ }
$value = $reflProperty->getValue($document);
if ($class->isCollectionValuedAssociation($fieldName)
&& $value !== null
@@ -762,12 +768,24 @@ private function computeChangeSet(ClassMetadata $class, $document)
}
}
- // unset the version info fields if they have values, they are not to be managed by the user in write scenarios.
- if ($class->versionable) {
- unset($actualData[$class->versionNameField]);
- unset($actualData[$class->versionCreatedField]);
+ return $actualData;
+ }
+
+ /**
+ * @param ClassMetadata $class
+ * @param object $document
+ * @return void
+ */
+ private function computeChangeSet(ClassMetadata $class, $document)
+ {
+ if ($document instanceof Proxy && !$document->__isInitialized()) {
+ return;
}
+ $actualData = $this->getDocumentActualData($class, $document);
+ $id = $this->getDocumentId($document);
+ $oid = spl_object_hash($document);
+
if ($class->parentMapping && isset($actualData[$class->parentMapping])) {
$parentClass = $this->dm->getClassMetadata(get_class($actualData[$class->parentMapping]));
}
@@ -1056,7 +1074,15 @@ private function cascadeDetach($document, array &$visited)
*/
public function commit($document = null)
{
- $this->detectChangedDocuments($document);
+ if ($document === null) {
+ $this->computeChangeSets();
+ } else if (is_object($document)) {
+ $this->computeSingleDocumentChangeSet($document);
+ } else if (is_array($document)) {
+ foreach ($document as $object) {
+ $this->computeSingleDocumentChangeSet($object);
+ }
+ }
if ($this->evm->hasListeners(Event::onFlush)) {
$this->evm->dispatchEvent(Event::onFlush, new OnFlushEventArgs($this->dm));
View
31 tests/Doctrine/Tests/Models/CMS/CmsTeamUser.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Doctrine\Tests\Models\CMS;
+
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
+use Doctrine\ODM\PHPCR\DocumentRepository;
+use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface;
+
+/**
+ * @PHPCRODM\Document(repositoryClass="Doctrine\Tests\Models\CMS\CmsTeamUserRepository")
+ */
+class CmsTeamUser extends CmsUser
+{
+ /** @PHPCRODM\ParentDocument */
+ public $parent;
+}
+
+class CmsTeamUserRepository extends DocumentRepository implements RepositoryIdInterface
+{
+ /**
+ * Generate a document id
+ *
+ * @param object $document
+ * @return string
+ */
+ public function generateId($document)
+ {
+ return $document->parent->id.'/'.$document->username;
+ }
+}
View
402 tests/Doctrine/Tests/ODM/PHPCR/Functional/BasicCrudTest.php
@@ -6,9 +6,6 @@
Doctrine\ODM\PHPCR\DocumentRepository,
Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
-use Doctrine\Tests\Models\CMS\CmsUser;
-use Doctrine\Tests\Models\CMS\CmsAddress;
-
/**
* @group functional
*/
@@ -132,142 +129,6 @@ public function testMultivaluePropertyWithOnlyOneValueUpdatedToMultiValue()
$this->assertEquals($userNew->numbers->toArray(), $userNew2->numbers->toArray());
}
- public function testMove()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->move($user, '/functional/user2');
- $this->dm->flush();
-
- $user = $this->dm->find($this->type, '/functional/user2');
- $this->assertNotNull($user, 'User must exist');
-
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNull($user, 'User must not exist');
- }
-
- public function testMoveWithClear()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->move($user, '/functional/user2');
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
- $this->dm->flush();
- $this->dm->clear();
-
- $user = $this->dm->find($this->type, '/functional/user2');
- $this->assertNotNull($user, 'User must exist');
- }
-
- public function testMoveWithPersist()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->move($user, '/functional/user2');
- $this->dm->persist($user);
- $this->dm->flush();
- $this->dm->clear();
-
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
- }
-
- public function testMoveFirstPersist()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $user->username = 'new name';
- $this->dm->persist($user);
- $this->dm->move($user, '/functional/user2');
- $this->dm->flush();
- $this->dm->clear();
-
- $user = $this->dm->find($this->type, '/functional/user2');
- $this->assertNotNull($user, 'User must exist');
- $this->assertEquals('new name', $user->username);
- }
-
- public function testMoveThenRemove()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->move($user, '/functional/user2');
- $this->dm->remove($user);
- $this->dm->flush();
-
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNull($user, 'User must be null after deletion');
- $user = $this->dm->find($this->type, '/functional/user2');
- $this->assertNull($user, 'User must be null after deletion');
- }
-
- public function testMoveNoFlush()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->move($user, '/functional/user2');
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
- }
-
- public function testMoveWithChild()
- {
- $this->dm->clear();
- $user1 = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user1, 'User must exist');
-
- $user2 = new TeamUser();
- $user2->username = 'jwage';
- $user2->id = '/functional/user/team';
- $user2->parent = $user1;
- $user3 = new TeamUser();
- $user3->username = 'beberlei';
- $user3->id = '/functional/user/team/team';
- $user3->parent = $user2;
-
- $this->dm->persist($user3);
-
- $this->dm->flush();
-
- $user1 = $this->dm->find($this->type, '/functional/user');
- $this->dm->move($user1, '/functional/user2');
- $this->dm->flush();
-
- $this->dm->clear();
-
- $user1 = $this->dm->find($this->type, '/functional/user2');
-
- $user = $this->dm->find($this->type, '/functional/user2');
- $this->assertNotNull($user, 'User must exist');
- $user = $this->dm->find($this->type, '/functional/user2/team');
- $this->assertNotNull($user, 'User must exist');
- $user = $this->dm->find($this->type, '/functional/user2/team/team');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->move($user1, '/functional/user');
- $this->dm->flush();
-
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
- $user = $this->dm->find($this->type, '/functional/user/team');
- $this->assertNotNull($user, 'User must exist');
- $user = $this->dm->find($this->type, '/functional/user/team/team');
- $this->assertNotNull($user, 'User must exist');
- }
-
public function testRemove()
{
$this->dm->clear();
@@ -297,23 +158,6 @@ public function testRemoveWithClear()
$this->assertNull($user, 'User must be null after deletion');
}
- public function testRemoveThenMove()
- {
- $this->dm->clear();
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNotNull($user, 'User must exist');
-
- $this->dm->remove($user);
- $this->dm->move($user, '/functional/user2');
- $this->dm->flush();
-
- $user = $this->dm->find($this->type, '/functional/user2');
- $this->assertNotNull($user, 'User must exist');
-
- $user = $this->dm->find($this->type, '/functional/user');
- $this->assertNull($user, 'User must be null after deletion');
- }
-
public function testRemoveWithPersist()
{
$this->dm->clear();
@@ -543,252 +387,6 @@ public function testNoIdProperty()
$this->assertEquals('test2', $userNew->username);
$this->assertEquals($user->numbers->toArray(), $userNew->numbers->toArray());
}
-
- public function testFlushSingleDocument()
- {
- $user1 = new User();
- $user1->username = 'romanb';
- $user1->id = '/functional/test';
- $user2 = new User();
- $user2->username = 'jwage';
- $user2->id = '/functional/test2';
- $this->dm->persist($user1);
- $this->dm->persist($user2);
- $this->dm->flush();
- $this->dm->clear();
-
- $user1 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
- $this->assertEquals('romanb', $user1->username);
-
- $user2 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test2');
- $this->assertEquals('jwage', $user2->username);
-
- $user1->username = 'changed';
- $user2->username = 'changed';
- $this->dm->flush($user1);
- $this->dm->clear();
-
- $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
- $this->assertEquals('changed', $check->username);
-
- $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test2');
- $this->assertEquals('jwage', $check->username);
- }
-
- public function testFlushSingleDocumentThenFlush()
- {
- $user1 = new User();
- $user1->username = 'romanb';
- $user1->id = '/functional/test';
- $user2 = new User();
- $user2->username = 'jwage';
- $user2->id = '/functional/test2';
- $this->dm->persist($user1);
- $this->dm->persist($user2);
- $this->dm->flush();
-
- $user1->username = 'changed';
- $user2->username = 'changed';
- $this->dm->flush($user1);
-
- $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
- $this->assertEquals('changed', $check->username);
-
- $this->dm->flush();
-
- $check = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test2');
- $this->assertEquals('changed', $check->username);
- }
-
- public function testFlushSingleDocumentWithParent()
- {
- $user1 = new User();
- $user1->username = 'romanb';
- $user1->id = '/functional/test';
- $user2 = new TeamUser();
- $user2->username = 'jwage';
- $user2->id = '/functional/test/team';
- $user2->parent = $user1;
- $user3 = new TeamUser();
- $user3->username = 'beberlei';
- $user3->id = '/functional/test/team/team';
- $user3->parent = $user2;
- $this->dm->persist($user3);
- $this->dm->flush($user3);
-
- $user1 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
- $this->assertEquals('romanb', $user1->username);
-
- $user2 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team');
- $this->assertEquals('jwage', $user2->username);
-
- $user3 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team/team');
- $this->assertEquals('beberlei', $user3->username);
-
- $user1->username = 'changed';
- $user2->username = 'changed';
- $user3->username = 'changed';
- $this->dm->flush($user3);
-
- $user1 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test');
- $this->assertEquals('changed', $user1->username);
-
- $user2 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team');
- $this->assertEquals('changed', $user2->username);
-
- $user3 = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\User', '/functional/test/team/team');
- $this->assertEquals('changed', $user3->username);
- }
-
- public function testFlushSingleManagedDocument()
- {
- $user = new CmsUser;
- $user->name = 'Dominik';
- $user->username = 'domnikl';
- $user->status = 'developer';
-
- $this->dm->persist($user);
- $this->dm->flush();
-
- $user->status = 'administrator';
- $this->dm->flush($user);
- $this->dm->clear();
-
- $user = $this->dm->find(get_class($user), $user->id);
- $this->assertEquals('administrator', $user->status);
- }
-
- public function testFlushSingleUnmanagedDocument()
- {
- $user = new CmsUser;
- $user->name = 'Dominik';
- $user->username = 'domnikl';
- $user->status = 'developer';
-
- $this->setExpectedException('InvalidArgumentException');
- $this->dm->flush($user);
- }
-
- public function testFlushSingleAndNewDocument()
- {
- $user = new CmsUser;
- $user->name = 'Dominik';
- $user->username = 'domnikl';
- $user->status = 'developer';
-
- $this->dm->persist($user);
- $this->dm->flush();
-
- $otherUser = new CmsUser;
- $otherUser->name = 'Dominik2';
- $otherUser->username = 'domnikl2';
- $otherUser->status = 'developer';
-
- $user->status = 'administrator';
-
- $this->dm->persist($otherUser);
- $this->dm->flush($user);
-
- $this->assertTrue($this->dm->contains($otherUser), "Other user is not contained in DocumentManager");
- $this->assertTrue($otherUser->id != null, "other user has no id");
- }
-
- public function testFlushAndCascadePersist()
- {
- $user = new CmsUser;
- $user->name = 'Dominik';
- $user->username = 'domnikl';
- $user->status = 'developer';
-
- $this->dm->persist($user);
- $this->dm->flush();
-
- $address = new CmsAddress();
- $address->city = "Springfield";
- $address->zip = "12354";
- $address->country = "Germany";
- $address->street = "Foo Street";
- $address->user = $user;
- $user->address = $address;
-
- $this->dm->flush($user);
-
- $this->assertTrue($this->dm->contains($address), "Address is not contained in DocumentManager");
- $this->assertTrue($address->id != null, "address user has no id");
- }
-
- public function testProxyIsIgnored()
- {
- $user = new CmsUser;
- $user->name = 'Dominik';
- $user->username = 'domnikl';
- $user->status = 'developer';
-
- $this->dm->persist($user);
- $this->dm->flush();
- $this->dm->clear();
-
- $user = $this->dm->getReference(get_class($user), $user->id);
-
- $otherUser = new CmsUser;
- $otherUser->name = 'Dominik2';
- $otherUser->username = 'domnikl2';
- $otherUser->status = 'developer';
-
- $this->dm->persist($otherUser);
- $this->dm->flush($user);
-
- $this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager");
- $this->assertTrue($otherUser->id != null, "other user has no id"); }
-
- public function testDetach()
- {
- $user = $this->dm->find($this->type, '/functional/user');
- $user->username = "new-name";
-
- $this->dm->detach($user);
- $this->dm->flush();
- $this->dm->clear();
-
- $newUser = $this->dm->find($this->type, '/functional/user');
- $this->assertEquals('lsmith', $newUser->username);
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testDetachWithPerist()
- {
- $user = $this->dm->find($this->type, '/functional/user');
- $user->username = "new-name";
-
- $this->dm->detach($user);
- $this->dm->persist($user);
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testDetachWithMove()
- {
- $user = $this->dm->find($this->type, '/functional/user');
- $user->username = "new-name";
-
- $this->dm->detach($user);
- $this->dm->move($user, '/functional/user2');
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testDetachWithRemove()
- {
- $user = $this->dm->find($this->type, '/functional/user');
- $user->username = "new-name";
-
- $this->dm->detach($user);
- $this->dm->remove($user);
- }
}
/**
View
60 tests/Doctrine/Tests/ODM/PHPCR/Functional/DetachTest.php
@@ -11,10 +11,19 @@ class DetachTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
private $node;
+ private $type;
+
public function setUp()
{
- $this->dm = $this->createDocumentManager();
+ $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
+ $this->dm = $this->createDocumentManager(array(__DIR__));
$this->node = $this->resetFunctionalNode($this->dm);
+
+ $user = $this->node->addNode('lsmith');
+ $user->setProperty('username', 'lsmith');
+ $user->setProperty('numbers', array(3, 1, 2));
+ $user->setProperty('phpcr:class', $this->type, \PHPCR\PropertyType::STRING);
+ $this->dm->getPhpcrSession()->save();
}
public function testDetachNewObject()
@@ -47,4 +56,53 @@ public function testDetachedKnownObject()
$this->dm->detach($user);
$this->dm->persist($user);
}
+
+ public function testDetach()
+ {
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $user->username = "new-name";
+
+ $this->dm->detach($user);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $newUser = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertEquals('lsmith', $newUser->username);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testDetachWithPerist()
+ {
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $user->username = "new-name";
+
+ $this->dm->detach($user);
+ $this->dm->persist($user);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testDetachWithMove()
+ {
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $user->username = "new-name";
+
+ $this->dm->detach($user);
+ $this->dm->move($user, '/functional/user2');
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testDetachWithRemove()
+ {
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $user->username = "new-name";
+
+ $this->dm->detach($user);
+ $this->dm->remove($user);
+ }
}
View
240 tests/Doctrine/Tests/ODM/PHPCR/Functional/FlushTest.php
@@ -1,2 +1,242 @@
<?php
+namespace Doctrine\Tests\ODM\PHPCR\Functional;
+
+use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface,
+Doctrine\ODM\PHPCR\DocumentRepository,
+Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
+
+use Doctrine\Tests\Models\CMS\CmsUser;
+use Doctrine\Tests\Models\CMS\CmsTeamUser;
+use Doctrine\Tests\Models\CMS\CmsAddress;
+
+/**
+ * @group functional
+ */
+class FlushTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
+{
+ /**
+ * @var DocumentManager
+ */
+ private $dm;
+
+ private $type;
+
+ public function setUp()
+ {
+ $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
+ $this->dm = $this->createDocumentManager(array(__DIR__));
+ $this->resetFunctionalNode($this->dm);
+ }
+
+ public function testFlushSingleDocument()
+ {
+ $user1 = new CmsUser();
+ $user1->username = 'romanb';
+ $user2 = new CmsUser();
+ $user2->username = 'jwage';
+ $this->dm->persist($user1);
+ $this->dm->persist($user2);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $user1 = $this->dm->find($this->type, '/functional/romanb');
+ $this->assertEquals('romanb', $user1->username);
+
+ $user2 = $this->dm->find($this->type, '/functional/jwage');
+ $this->assertEquals('jwage', $user2->username);
+
+ $user1->username = 'changed';
+ $user2->username = 'changed';
+ $this->dm->flush($user1);
+ $this->dm->clear();
+
+ $check = $this->dm->find($this->type, '/functional/romanb');
+ $this->assertEquals('changed', $check->username);
+
+ $check = $this->dm->find($this->type, '/functional/jwage');
+ $this->assertEquals('jwage', $check->username);
+ }
+
+ public function testFlushSingleDocumentThenFlush()
+ {
+ $user1 = new CmsUser();
+ $user1->username = 'romanb';
+ $user2 = new CmsUser();
+ $user2->username = 'jwage';
+ $this->dm->persist($user1);
+ $this->dm->persist($user2);
+ $this->dm->flush();
+
+ $user1->username = 'changed';
+ $user2->username = 'changed';
+ $this->dm->flush($user1);
+
+ $check = $this->dm->find($this->type, '/functional/romanb');
+ $this->assertEquals('changed', $check->username);
+
+ $this->dm->flush();
+
+ $check = $this->dm->find($this->type, '/functional/jwage');
+ $this->assertEquals('changed', $check->username);
+ }
+
+ public function testFlushSingleDocumentWithParent()
+ {
+ $user1 = new CmsUser();
+ $user1->username = 'romanb';
+ $user2 = new CmsTeamUser();
+ $user2->username = 'jwage';
+ $user2->parent = $user1;
+ $user3 = new CmsTeamUser();
+ $user3->username = 'beberlei';
+ $user3->parent = $user2;
+ $this->dm->persist($user3);
+ $this->dm->flush($user3);
+
+ $user1 = $this->dm->find($this->type, '/functional/romanb');
+ $this->assertEquals('romanb', $user1->username);
+
+ $user2 = $this->dm->find($this->type, '/functional/romanb/jwage');
+ $this->assertEquals('jwage', $user2->username);
+
+ $user3 = $this->dm->find($this->type, '/functional/romanb/jwage/beberlei');
+ $this->assertEquals('beberlei', $user3->username);
+
+ $user1->username = 'changed';
+ $user2->username = 'changed';
+ $user3->username = 'changed';
+ $this->dm->flush($user3);
+
+ $user1 = $this->dm->find($this->type, '/functional/romanb');
+ $this->assertEquals('changed', $user1->username);
+
+ $user2 = $this->dm->find($this->type, '/functional/romanb/jwage');
+ $this->assertEquals('changed', $user2->username);
+
+ $user3 = $this->dm->find($this->type, '/functional/romanb/jwage/beberlei');
+ $this->assertEquals('changed', $user3->username);
+ }
+
+ public function testFlushSingleManagedDocument()
+ {
+ $user = new CmsUser();
+ $user->name = 'Dominik';
+ $user->username = 'domnikl';
+ $user->status = 'developer';
+
+ $this->dm->persist($user);
+ $this->dm->flush();
+
+ $user->status = 'administrator';
+ $this->dm->flush($user);
+ $this->dm->clear();
+
+ $user = $this->dm->find(get_class($user), $user->id);
+ $this->assertEquals('administrator', $user->status);
+ }
+
+ public function testFlushManyExplicitDocuments()
+ {
+ $userA = new CmsUser('userA');
+ $userA->username = 'userA';
+ $userB = new CmsUser('userB');
+ $userB->username = 'userB';
+ $userC = new CmsUser('userC');
+ $userC->username = 'userC';
+
+ $this->dm->persist($userA);
+ $this->dm->persist($userB);
+ $this->dm->persist($userC);
+
+ $this->dm->flush(array($userA, $userB, $userC));
+
+ $this->assertNotNull($userA->id);
+ $this->assertNotNull($userB->id);
+ $this->assertNotNull($userC->id);
+ }
+
+ public function testFlushSingleUnmanagedDocument()
+ {
+ $user = new CmsUser();
+ $user->name = 'Dominik';
+ $user->username = 'domnikl';
+ $user->status = 'developer';
+
+ $this->setExpectedException('InvalidArgumentException');
+ $this->dm->flush($user);
+ }
+
+ public function testFlushSingleAndNewDocument()
+ {
+ $user = new CmsUser();
+ $user->name = 'Dominik';
+ $user->username = 'domnikl';
+ $user->status = 'developer';
+
+ $this->dm->persist($user);
+ $this->dm->flush();
+
+ $otherUser = new CmsUser;
+ $otherUser->name = 'Dominik2';
+ $otherUser->username = 'domnikl2';
+ $otherUser->status = 'developer';
+
+ $user->status = 'administrator';
+
+ $this->dm->persist($otherUser);
+ $this->dm->flush($user);
+
+ $this->assertTrue($this->dm->contains($otherUser), "Other user is not contained in DocumentManager");
+ $this->assertTrue($otherUser->id != null, "other user has no id");
+ }
+
+ public function testFlushAndCascadePersist()
+ {
+ $user = new CmsUser();
+ $user->name = 'Dominik';
+ $user->username = 'domnikl';
+ $user->status = 'developer';
+
+ $this->dm->persist($user);
+ $this->dm->flush();
+
+ $address = new CmsAddress();
+ $address->city = "Springfield";
+ $address->zip = "12354";
+ $address->country = "Germany";
+ $address->street = "Foo Street";
+ $address->user = $user;
+ $user->address = $address;
+
+ $this->dm->flush($user);
+
+ $this->assertTrue($this->dm->contains($address), "Address is not contained in DocumentManager");
+ $this->assertTrue($address->id != null, "address user has no id");
+ }
+
+ public function testProxyIsIgnored()
+ {
+ $user = new CmsUser();
+ $user->name = 'Dominik';
+ $user->username = 'domnikl';
+ $user->status = 'developer';
+
+ $this->dm->persist($user);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $user = $this->dm->getReference(get_class($user), $user->id);
+
+ $otherUser = new CmsUser;
+ $otherUser->name = 'Dominik2';
+ $otherUser->username = 'domnikl2';
+ $otherUser->status = 'developer';
+
+ $this->dm->persist($otherUser);
+ $this->dm->flush($user);
+
+ $this->assertTrue($this->dm->contains($otherUser), "Other user is contained in DocumentManager");
+ $this->assertTrue($otherUser->id != null, "other user has no id");
+ }
+}
View
189 tests/Doctrine/Tests/ODM/PHPCR/Functional/MoveTest.php
@@ -0,0 +1,189 @@
+<?php
+
+namespace Doctrine\Tests\ODM\PHPCR\Functional;
+
+use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface,
+ Doctrine\ODM\PHPCR\DocumentRepository,
+ Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
+
+use Doctrine\Tests\Models\CMS\CmsUser;
+use Doctrine\Tests\Models\CMS\CmsTeamUser;
+
+/**
+ * @group functional
+ */
+class MoveTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
+{
+ /**
+ * @var DocumentManager
+ */
+ private $dm;
+
+ private $type;
+
+ private $node;
+
+ public function setUp()
+ {
+ $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
+ $this->dm = $this->createDocumentManager(array(__DIR__));
+ $this->node = $this->resetFunctionalNode($this->dm);
+
+ $user = $this->node->addNode('lsmith');
+ $user->setProperty('username', 'lsmith');
+ $user->setProperty('numbers', array(3, 1, 2));
+ $user->setProperty('phpcr:class', $this->type, \PHPCR\PropertyType::STRING);
+ $this->dm->getPhpcrSession()->save();
+ }
+
+ public function testMove()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->move($user, '/functional/user2');
+ $this->dm->flush();
+
+ $user = $this->dm->find($this->type, '/functional/user2');
+ $this->assertNotNull($user, 'User must exist');
+
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNull($user, 'User must not exist');
+ }
+
+ public function testMoveWithClear()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->move($user, '/functional/user2');
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $user = $this->dm->find($this->type, '/functional/user2');
+ $this->assertNotNull($user, 'User must exist');
+ }
+
+ public function testMoveWithPersist()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->move($user, '/functional/user2');
+ $this->dm->persist($user);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+ }
+
+ public function testMoveFirstPersist()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $user->username = 'new name';
+ $this->dm->persist($user);
+ $this->dm->move($user, '/functional/user2');
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $user = $this->dm->find($this->type, '/functional/user2');
+ $this->assertNotNull($user, 'User must exist');
+ $this->assertEquals('new name', $user->username);
+ }
+
+ public function testMoveThenRemove()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->move($user, '/functional/user2');
+ $this->dm->remove($user);
+ $this->dm->flush();
+
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNull($user, 'User must be null after deletion');
+ $user = $this->dm->find($this->type, '/functional/user2');
+ $this->assertNull($user, 'User must be null after deletion');
+ }
+
+ public function testMoveNoFlush()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->move($user, '/functional/user2');
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+ }
+
+ public function testMoveWithChild()
+ {
+ $this->dm->clear();
+ $user1 = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user1, 'User must exist');
+
+ $user2 = new CmsTeamUser();
+ $user2->username = 'jwage';
+ $user2->parent = $user1;
+ $user3 = new CmsTeamUser();
+ $user3->username = 'beberlei';
+ $user3->parent = $user2;
+
+ $this->dm->persist($user3);
+
+ $this->dm->flush();
+
+ $user1 = $this->dm->find($this->type, '/functional/lsmith');
+ $this->dm->move($user1, '/functional/user2');
+ $this->dm->flush();
+
+ $this->dm->clear();
+
+ $user1 = $this->dm->find($this->type, '/functional/user2');
+
+ $user = $this->dm->find($this->type, '/functional/user2');
+ $this->assertNotNull($user, 'User must exist');
+ $user = $this->dm->find($this->type, '/functional/user2/jwage');
+ $this->assertNotNull($user, 'User must exist');
+ $user = $this->dm->find($this->type, '/functional/user2/jwage/beberlei');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->move($user1, '/functional/lsmith');
+ $this->dm->flush();
+
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+ $user = $this->dm->find($this->type, '/functional/lsmith/jwage');
+ $this->assertNotNull($user, 'User must exist');
+ $user = $this->dm->find($this->type, '/functional/lsmith/jwage/beberlei');
+ $this->assertNotNull($user, 'User must exist');
+ }
+
+ public function testRemoveThenMove()
+ {
+ $this->dm->clear();
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNotNull($user, 'User must exist');
+
+ $this->dm->remove($user);
+ $this->dm->move($user, '/functional/user2');
+ $this->dm->flush();
+
+ $user = $this->dm->find($this->type, '/functional/user2');
+ $this->assertNotNull($user, 'User must exist');
+
+ $user = $this->dm->find($this->type, '/functional/lsmith');
+ $this->assertNull($user, 'User must be null after deletion');
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.