Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1655Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 /**
6  * @group DDC-1655
7  * @group DDC-1640
8  * @group DDC-1556
9  */
10 class DDC1655Test extends \Doctrine\Tests\OrmFunctionalTestCase
11 {
12     public function setUp()
13     {
14         parent::setUp();
15         try {
16             $this->_schemaTool->createSchema(array(
17                 $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Foo'),
18                 $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Bar'),
19                 $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Baz'),
20             ));
21         } catch(\Exception $e) {
22
23         }
24     }
25
26     public function testPostLoadOneToManyInheritance()
27     {
28         $cm = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1655Foo');
29         $this->assertEquals(array("postLoad" => array("postLoad")), $cm->lifecycleCallbacks);
30
31         $cm = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1655Bar');
32         $this->assertEquals(array("postLoad" => array("postLoad", "postSubLoaded")), $cm->lifecycleCallbacks);
33
34         $baz = new DDC1655Baz();
35         $foo = new DDC1655Foo();
36         $foo->baz = $baz;
37         $bar = new DDC1655Bar();
38         $bar->baz = $baz;
39
40         $this->_em->persist($foo);
41         $this->_em->persist($bar);
42         $this->_em->persist($baz);
43         $this->_em->flush();
44         $this->_em->clear();
45
46         $baz = $this->_em->find(get_class($baz), $baz->id);
47         foreach ($baz->foos as $foo) {
48             $this->assertEquals(1, $foo->loaded, "should have loaded callback counter incremented for " . get_class($foo));
49         }
50     }
51
52     /**
53      * Check that post load is not executed several times when the entity
54      * is rehydrated again although its already known.
55      */
56     public function testPostLoadInheritanceChild()
57     {
58         $bar = new DDC1655Bar();
59
60         $this->_em->persist($bar);
61         $this->_em->flush();
62         $this->_em->clear();
63
64         $bar = $this->_em->find(get_class($bar), $bar->id);
65         $this->assertEquals(1, $bar->loaded);
66         $this->assertEquals(1, $bar->subLoaded);
67
68         $bar = $this->_em->find(get_class($bar), $bar->id);
69         $this->assertEquals(1, $bar->loaded);
70         $this->assertEquals(1, $bar->subLoaded);
71
72         $dql = "SELECT b FROM " . __NAMESPACE__ . "\DDC1655Bar b WHERE b.id = ?1";
73         $bar = $this->_em->createQuery($dql)->setParameter(1, $bar->id)->getSingleResult();
74
75         $this->assertEquals(1, $bar->loaded);
76         $this->assertEquals(1, $bar->subLoaded);
77
78         $this->_em->refresh($bar);
79
80         $this->assertEquals(2, $bar->loaded);
81         $this->assertEquals(2, $bar->subLoaded);
82     }
83 }
84
85 /**
86  * @Entity
87  * @InheritanceType("SINGLE_TABLE")
88  * @DiscriminatorMap({
89  *    "foo" = "DDC1655Foo",
90  *    "bar" = "DDC1655Bar"
91  * })
92  * @HasLifecycleCallbacks
93  */
94 class DDC1655Foo
95 {
96     /** @Id @GeneratedValue @Column(type="integer") */
97     public $id;
98
99     public $loaded = 0;
100
101     /**
102      * @ManyToOne(targetEntity="DDC1655Baz", inversedBy="foos")
103      */
104     public $baz;
105
106     /**
107      * @PostLoad
108      */
109     public function postLoad()
110     {
111         $this->loaded++;
112     }
113 }
114
115 /**
116  * @Entity
117  * @HasLifecycleCallbacks
118  */
119 class DDC1655Bar extends DDC1655Foo
120 {
121     public $subLoaded;
122
123     /**
124      * @PostLoad
125      */
126     public function postSubLoaded()
127     {
128         $this->subLoaded++;
129     }
130 }
131
132 /**
133  * @Entity
134  */
135 class DDC1655Baz
136 {
137     /** @Id @GeneratedValue @Column(type="integer") */
138     public $id;
139
140     /**
141      * @OneToMany(targetEntity="DDC1655Foo", mappedBy="baz")
142      */
143     public $foos = array();
144 }