Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC992Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6
7 require_once __DIR__ . '/../../../TestInit.php';
8
9 /**
10  * @group DDC-992
11  */
12 class DDC992Test extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     public function setUp()
15     {
16         parent::setUp();
17         try {
18             $this->_schemaTool->createSchema(array(
19                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC992Role'),
20                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC992Parent'),
21                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC992Child'),
22             ));
23         } catch(\Exception $e) {
24
25         }
26     }
27
28     public function testIssue()
29     {
30         $role = new DDC992Role();
31         $role->name = "Parent";
32         $child = new DDC992Role();
33         $child->name = "child";
34
35         $role->extendedBy[] = $child;
36         $child->extends[] = $role;
37
38         $this->_em->persist($role);
39         $this->_em->persist($child);
40         $this->_em->flush();
41         $this->_em->clear();
42
43         $child = $this->_em->getRepository(get_class($role))->find($child->roleID);
44         $parents = count($child->extends);
45         $this->assertEquals(1, $parents);
46         foreach ($child->extends AS $parent) {
47             $this->assertEquals($role->getRoleID(), $parent->getRoleID());
48         }
49     }
50
51     public function testOneToManyChild()
52     {
53         $parent = new DDC992Parent();
54         $child = new DDC992Child();
55         $child->parent = $parent;
56         $parent->childs[] = $child;
57
58         $this->_em->persist($parent);
59         $this->_em->persist($child);
60         $this->_em->flush();
61         $this->_em->clear();
62
63         $parentRepository = $this->_em->getRepository(get_class($parent));
64         $childRepository = $this->_em->getRepository(get_class($child));
65
66         $parent = $parentRepository->find($parent->id);
67         $this->assertEquals(1, count($parent->childs));
68         $this->assertEquals(0, count($parent->childs[0]->childs()));
69
70         $child = $parentRepository->findOneBy(array("id" => $child->id));
71         $this->assertSame($parent->childs[0], $child);
72
73         $this->_em->clear();
74
75         $child = $parentRepository->find($child->id);
76         $this->assertEquals(0, count($child->childs));
77
78         $this->_em->clear();
79
80         $child = $childRepository->find($child->id);
81         $this->assertEquals(0, count($child->childs));
82     }
83 }
84
85 /**
86  * @Entity
87  * @InheritanceType("JOINED")
88  * @DiscriminatorMap({"child" = "DDC992Child", "parent" = "DDC992Parent"})
89  */
90 class DDC992Parent
91 {
92     /** @Id @GeneratedValue @Column(type="integer") */
93     public $id;
94     /** @ManyToOne(targetEntity="DDC992Parent", inversedBy="childs") */
95     public $parent;
96     /** @OneToMany(targetEntity="DDC992Child", mappedBy="parent") */
97     public $childs;
98 }
99
100 /**
101  * @Entity
102  */
103 class DDC992Child extends DDC992Parent
104 {
105     public function childs()
106     {
107         return $this->childs;
108     }
109 }
110
111 /**
112  * @Entity
113  */
114 class DDC992Role
115 {
116     public function getRoleID()
117     {
118         return $this->roleID;
119     }
120
121     /**
122      *  @Id  @Column(name="roleID", type="integer")
123      *  @GeneratedValue(strategy="AUTO")
124      */
125     public $roleID;
126     /**
127      * @Column (name="name", type="string", length=45)
128      */
129     public $name;
130     /**
131      * @ManyToMany (targetEntity="DDC992Role", mappedBy="extends")
132      */
133     public $extendedBy;
134     /**
135      * @ManyToMany (targetEntity="DDC992Role", inversedBy="extendedBy")
136      * @JoinTable (name="RoleRelations",
137      *      joinColumns={@JoinColumn(name="roleID", referencedColumnName="roleID")},
138      *      inverseJoinColumns={@JoinColumn(name="extendsRoleID", referencedColumnName="roleID")}
139      *      )
140      */
141     public $extends;
142
143     public function __construct() {
144         $this->extends = new ArrayCollection;
145         $this->extendedBy = new ArrayCollection;
146     }
147 }