public
Last active

Testing nested embedded documents in Doctrine ODM (BETA3)

  • Download Gist
MODM140Test.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
<?php
 
namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
 
use Doctrine\Common\Collections\ArrayCollection,
Documents\Functional\EmbeddedTestLevel0,
Documents\Functional\EmbeddedTestLevel1,
Documents\Functional\EmbeddedTestLevel2;
 
class MODM140Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
 
public function testInsertingNestedEmbeddedCollections()
{
$category = new Category;
$category->name = "My Category";
 
$post1 = new Post;
$post1->versions->add(new PostVersion('P1V1'));
$post1->versions->add(new PostVersion('P1V2'));
 
$category->posts->add($post1);
 
$this->dm->persist($category);
$this->dm->flush();
$this->dm->clear();
 
$category = $this->dm->getRepository(__NAMESPACE__ . '\Category')->findOneByName('My Category');
$post2 = new Post;
$post2->versions->add(new PostVersion('P2V1'));
$post2->versions->add(new PostVersion('P2V2'));
$category->posts->add($post2);
 
$this->dm->flush();
$this->dm->clear();
 
$category = $this->dm->getRepository(__NAMESPACE__ . '\Category')->findOneByName('My Category');
// Should be: 1 Category, 2 Post, 2 PostVersion in each Post
$this->assertEquals(2, $category->posts->count());
$this->assertEquals(2, $category->posts->get(0)->versions->count());
$this->assertEquals(2, $category->posts->get(1)->versions->count());
}
 
public function testAddingAnotherEmbeddedDocument()
{
$test = new EmbeddedTestLevel0();
$test->name = 'test';
 
$this->dm->persist($test);
$this->dm->flush();
$this->dm->clear();
 
$test = $this->dm->getRepository('Documents\Functional\EmbeddedTestLevel0')->findOneBy(array('name' => 'test'));
$this->assertInstanceOf('Documents\Functional\EmbeddedTestLevel0', $test);
 
$level1 = new EmbeddedTestLevel1();
$level1->name = "test level 1 #1";
 
$level2 = new EmbeddedTestLevel2();
$level2->name = "test level 2 #1 in level 1 #1";
$level1->level2[] = $level2;
 
$level2 = new EmbeddedTestLevel2();
$level2->name = "test level 2 #2 in level 1 #1";
$level1->level2[] = $level2;
 
$test->level1[] = $level1;
 
$this->dm->flush();
$this->dm->clear();
 
$test = $this->dm->getRepository('Documents\Functional\EmbeddedTestLevel0')->findOneBy(array('name' => 'test'));
$this->assertEquals(1, count($test->level1));
$this->assertEquals(2, count($test->level1[0]->level2));
 
$level1 = new EmbeddedTestLevel1();
$level1->name = "test level 1 #2";
 
$level2 = new EmbeddedTestLevel2();
$level2->name = "test level 2 #1 in level 1 #2";
$level1->level2[] = $level2;
 
$level2 = new EmbeddedTestLevel2();
$level2->name = "test level 2 #2 in level 1 #2";
$level1->level2[] = $level2;
 
$test->level1[] = $level1;
 
$this->dm->flush();
$this->dm->clear();
 
$test = $this->dm->getRepository('Documents\Functional\EmbeddedTestLevel0')->findOneBy(array('name' => 'test'));
$this->assertEquals(2, count($test->level1));
$this->assertEquals(2, count($test->level1[0]->level2));
$this->assertEquals(2, count($test->level1[1]->level2));
}
}
 
/** @Document(collection="tests", db="tests") */
class Category
{
/** @Id */
protected $id;
/** @String */
public $name;
/** @EmbedMany(targetDocument="Post") */
public $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
}
 
/** @EmbeddedDocument */
class Post
{
/** @EmbedMany(targetDocument="PostVersion") */
public $versions;
public function __construct()
{
$this->versions = new ArrayCollection();
}
}
 
/** @EmbeddedDocument */
class PostVersion
{
/** @String */
public $name;
public function __construct($name)
{
$this->name = $name;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.