Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / PersistentCollectionTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Common\Persistence\PersistentObject;
6
7 /**
8  */
9 class PersistentCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
10 {
11     protected function setUp()
12     {
13         parent::setUp();
14         try {
15             $this->_schemaTool->createSchema(array(
16                 $this->_em->getClassMetadata(__NAMESPACE__ . '\PersistentCollectionHolder'),
17                 $this->_em->getClassMetadata(__NAMESPACE__ . '\PersistentCollectionContent'),
18             ));
19         } catch (\Exception $e) {
20
21         }
22         PersistentObject::setObjectManager($this->_em);
23     }
24
25     public function testPersist()
26     {
27         $collectionHolder = new PersistentCollectionHolder();
28         $content = new PersistentCollectionContent('first element');
29         $collectionHolder->addElement($content);
30
31         $this->_em->persist($collectionHolder);
32         $this->_em->flush();
33         $this->_em->clear();
34
35         $collectionHolder = $this->_em->find(__NAMESPACE__ . '\PersistentCollectionHolder', $collectionHolder->getId());
36         $collectionHolder->getCollection();
37
38         $content = new PersistentCollectionContent('second element');
39         $collectionHolder->addElement($content);
40
41         $this->assertEquals(2, $collectionHolder->getCollection()->count());
42     }
43
44 }
45
46 /**
47  * @Entity
48  */
49 class PersistentCollectionHolder extends PersistentObject
50 {
51     /**
52      * @Id @Column(type="integer") @GeneratedValue
53      * @var int
54      */
55     protected $id;
56
57     /**
58      * @var \Doctrine\Common\Collections\Collection
59      * @ManyToMany(targetEntity="PersistentCollectionContent", cascade={"all"})
60      */
61     protected $collection;
62
63     public function __construct()
64     {
65         $this->collection = new \Doctrine\Common\Collections\ArrayCollection();
66     }
67
68     /**
69      * @param PersistentCollectionContent $element
70      */
71     public function addElement(PersistentCollectionContent $element)
72     {
73         $this->collection->add($element);
74     }
75
76     /**
77      * @return \Doctrine\Common\Collections\Collection
78      */
79     public function getCollection()
80     {
81         return clone $this->collection;
82     }
83
84 }
85
86 /**
87  * @Entity
88  */
89 class PersistentCollectionContent extends PersistentObject
90 {
91
92     /**
93      * @Id @Column(type="integer") @GeneratedValue
94      * @var int
95      */
96     protected $id;
97
98 }