Permalink
Browse files

[MODM-92] Failing test case for demonstrating duplicate embedded obje…

…ct creation when changing embedMany collections
  • Loading branch information...
1 parent 846ee5a commit 336a700ac6fe0cdf013fc864d8e70f43cb5b73e2 Jeremy Mikola committed Oct 22, 2010
Showing with 84 additions and 0 deletions.
  1. +84 −0 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM92Test.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
+
+require_once __DIR__ . '/../../../../../../TestInit.php';
+
+use Doctrine\Common\Collections\ArrayCollection;
+
+class MODM92Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
+{
+ public function testDocumentWithEmbeddedDocuments()
+ {
+ $embeddedDocuments = array(new MODM92TestEmbeddedDocument('foo'));
+
+ $testDoc = new MODM92TestDocument();
+ $testDoc->setEmbeddedDocuments($embeddedDocuments);
+ $this->dm->persist($testDoc);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $testDoc = $this->dm->findOne(__NAMESPACE__.'\MODM92TestDocument');
+ $this->assertEquals($embeddedDocuments, $testDoc->embeddedDocuments->toArray());
+
+ $embeddedDocuments = array(new MODM92TestEmbeddedDocument('bar'));
+
+ $testDoc->setEmbeddedDocuments($embeddedDocuments);
+ $this->assertEquals($embeddedDocuments, $testDoc->embeddedDocuments->toArray());
+
+ $this->dm->flush();
+ $this->dm->clear();
+ $testDoc = $this->dm->findOne(__NAMESPACE__.'\MODM92TestDocument');
+
+ $this->assertEquals($embeddedDocuments, $testDoc->embeddedDocuments->toArray());
+ }
+}
+
+/** @Document */
+class MODM92TestDocument
+{
+ /** @Id */
+ public $id;
+
+ // Note: Test case fails with default "pushPull" strategy, but "set" works
+ /** @EmbedMany(targetDocument="MODM92TestEmbeddedDocument") */
+ public $embeddedDocuments;
+
+ public function __construct() {
+ $this->embeddedDocuments = new ArrayCollection();
+ }
+
+ /**
+ * Sets children
+ *
+ * If $images is not an array or Traversable object, this method will simply
+ * clear the images collection property. If any elements in the parameter
+ * are not an Image object, this method will attempt to convert them to one
+ * by mapping array indexes (size URL's are required, cropMetadata is not).
+ * Any invalid elements will be ignored.
+ *
+ * @param array|Traversable $children
+ */
+ public function setEmbeddedDocuments($embeddedDocuments) {
+ $this->embeddedDocuments->clear();
+
+ if (! (is_array($embeddedDocuments) || $embeddedDocuments instanceof \Traversable)) {
+ return;
+ }
+
+ foreach ($embeddedDocuments as $embeddedDocument) {
+ $this->embeddedDocuments->add($embeddedDocument);
+ }
+ }
+}
+
+/** @EmbeddedDocument */
+class MODM92TestEmbeddedDocument
+{
+ /** @String */
+ public $name;
+
+ public function __construct($name) {
+ $this->name = $name;
+ }
+}

0 comments on commit 336a700

Please sign in to comment.