<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;

class MODM140Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
	
	public function testInstertingNestedEmbdeddedCollections()
	{
		$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');
		$this->assertEquals(2, $category->posts->count());
		$this->assertEquals(2, $category->posts->get(0)->versions->count());
		$this->assertEquals(2, $category->posts->get(1)->versions->count());
	}
	
}

/** @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
{
	
	/** @Id */
	protected $id;
	
	/** @EmbedMany(targetDocument="PostVersion") */
	public $versions;
	
	public function __construct()
	{
		$this->versions = new ArrayCollection();
	}
	
}

/** @EmbeddedDocument */
class PostVersion
{
	
	/** @Id */
	protected $id;
	
	/** @String */
	public $name;
	
	public function __construct($name)
	{
		$this->name = $name;
	}
	
}



