Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC599Test.php
1 <?php
2 namespace Doctrine\Tests\ORM\Functional\Ticket;
3
4 require_once __DIR__ . '/../../../TestInit.php';
5
6 class DDC599Test extends \Doctrine\Tests\OrmFunctionalTestCase
7 {
8     protected function setUp()
9     {
10         parent::setUp();
11         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
12         try {
13             $this->_schemaTool->createSchema(array(
14                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Item'),
15                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Subitem'),
16                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Child'),
17             ));
18         } catch (\Exception $ignored) {}
19     }
20
21     public function testCascadeRemoveOnInheritanceHierachy()
22     {
23         $item = new DDC599Subitem;
24         $item->elem = "foo";
25         $child = new DDC599Child;
26         $child->parent = $item;
27         $item->getChildren()->add($child);
28         $this->_em->persist($item);
29         $this->_em->persist($child);
30         $this->_em->flush();
31         $this->_em->clear();
32
33         $item = $this->_em->find(__NAMESPACE__ . '\DDC599Item', $item->id);
34
35         $this->_em->remove($item);
36         $this->_em->flush(); // Should not fail
37
38         $this->assertFalse($this->_em->contains($item));
39         $children = $item->getChildren();
40         $this->assertFalse($this->_em->contains($children[0]));
41
42         $this->_em->clear();
43
44
45         $item2 = new DDC599Subitem;
46         $item2->elem = "bar";
47         $this->_em->persist($item2);
48         $this->_em->flush();
49
50         $child2 = new DDC599Child;
51         $child2->parent = $item2;
52         $item2->getChildren()->add($child2);
53         $this->_em->persist($child2);
54         $this->_em->flush();
55
56         $this->_em->remove($item2);
57         $this->_em->flush(); // should not fail
58
59         $this->assertFalse($this->_em->contains($item));
60         $children = $item->getChildren();
61         $this->assertFalse($this->_em->contains($children[0]));
62     }
63
64     public function testCascadeRemoveOnChildren()
65     {
66         $class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Subitem');
67
68         $this->assertArrayHasKey('children', $class->associationMappings);
69         $this->assertTrue($class->associationMappings['children']['isCascadeRemove']);
70     }
71 }
72
73 /**
74  * @Entity
75  * @InheritanceType("SINGLE_TABLE")
76  * @DiscriminatorColumn(name="type", type="integer")
77  * @DiscriminatorMap({"0" = "DDC599Item", "1" = "DDC599Subitem"})
78  */
79 class DDC599Item
80 {
81     /**
82      * @Id
83      * @Column(type="integer")
84      * @GeneratedValue(strategy="AUTO")
85      */
86     public $id;
87
88     /**
89      * @OneToMany(targetEntity="DDC599Child", mappedBy="parent", cascade={"remove"})
90      */
91     protected $children;
92
93     public function __construct()
94     {
95         $this->children = new \Doctrine\Common\Collections\ArrayCollection;
96     }
97
98     public function getChildren()
99     {
100         return $this->children;
101     }
102 }
103
104 /**
105  * @Entity
106  */
107 class DDC599Subitem extends DDC599Item
108 {
109     /**
110      * @Column(type="string")
111      */
112     public $elem;
113 }
114
115 /**
116  * @Entity
117  */
118 class DDC599Child
119 {
120     /**
121      * @Id
122      * @Column(type="integer")
123      * @GeneratedValue(strategy="AUTO")
124      */
125     public $id;
126
127     /**
128      * @ManyToOne(targetEntity="DDC599Item", inversedBy="children")
129      * @JoinColumn(name="parentId", referencedColumnName="id")
130      */
131     public $parent;
132 }