Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1452Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\ORM\UnitOfWork;
7
8 require_once __DIR__ . '/../../../TestInit.php';
9
10 /**
11  * @group DDC-1452
12  */
13 class DDC1452Test extends \Doctrine\Tests\OrmFunctionalTestCase
14 {
15     protected function setUp()
16     {
17         $this->useModelSet('cms');
18         parent::setUp();
19
20         try {
21             $this->_schemaTool->createSchema(array(
22                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1452EntityA'),
23                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1452EntityB'),
24             ));
25         } catch (\Exception $ignored) {
26         }
27     }
28
29     public function testIssue()
30     {
31         $a1 = new DDC1452EntityA();
32         $a1->title = "foo";
33
34         $a2 = new DDC1452EntityA();
35         $a2->title = "bar";
36
37         $b = new DDC1452EntityB();
38         $b->entityAFrom = $a1;
39         $b->entityATo = $a2;
40
41         $this->_em->persist($a1);
42         $this->_em->persist($a2);
43         $this->_em->persist($b);
44         $this->_em->flush();
45         $this->_em->clear();
46
47         $dql = "SELECT a, b, ba FROM " . __NAMESPACE__ . "\DDC1452EntityA AS a LEFT JOIN a.entitiesB AS b LEFT JOIN b.entityATo AS ba";
48         $results = $this->_em->createQuery($dql)->setMaxResults(1)->getResult();
49
50         $this->assertSame($results[0], $results[0]->entitiesB[0]->entityAFrom);
51         $this->assertFalse( $results[0]->entitiesB[0]->entityATo instanceof \Doctrine\ORM\Proxy\Proxy );
52         $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results[0]->entitiesB[0]->entityATo->getEntitiesB());
53     }
54
55     public function testFetchJoinOneToOneFromInverse()
56     {
57         $address = new \Doctrine\Tests\Models\CMS\CmsAddress();
58         $address->city = "Bonn";
59         $address->country = "Germany";
60         $address->street = "Somestreet";
61         $address->zip = 12345;
62
63         $user = new \Doctrine\Tests\Models\CMS\CmsUser();
64         $user->name = "beberlei";
65         $user->username = "beberlei";
66         $user->status = "active";
67         $user->address = $address;
68         $address->user = $user;
69
70         $this->_em->persist($address);
71         $this->_em->persist($user);
72         $this->_em->flush();
73         $this->_em->clear();
74
75         $dql = "SELECT a, u FROM Doctrine\Tests\Models\CMS\CmsAddress a INNER JOIN a.user u";
76         $data = $this->_em->createQuery($dql)->getResult();
77         $this->_em->clear();
78
79         $this->assertFalse($data[0]->user instanceof \Doctrine\ORM\Proxy\Proxy);
80
81         $dql = "SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.address a";
82         $data = $this->_em->createQuery($dql)->getResult();
83
84         $this->assertFalse($data[0]->address instanceof \Doctrine\ORM\Proxy\Proxy);
85     }
86 }
87
88 /**
89  * @Entity
90  */
91 class DDC1452EntityA
92 {
93     /** @Id @Column(type="integer") @GeneratedValue */
94     public $id;
95     /** @Column */
96     public $title;
97     /** @ManyToMany(targetEntity="DDC1452EntityB", mappedBy="entityAFrom") */
98     public $entitiesB;
99
100     public function __construct()
101     {
102         $this->entitiesB = new ArrayCollection();
103     }
104
105     public function getEntitiesB()
106     {
107         return $this->entitiesB;
108     }
109 }
110
111 /**
112  * @Entity
113  */
114 class DDC1452EntityB
115 {
116     /** @Id @Column(type="integer") @GeneratedValue */
117     public $id;
118
119     /**
120      * @ManyToOne(targetEntity="DDC1452EntityA", inversedBy="entitiesB")
121      */
122     public $entityAFrom;
123     /**
124      * @ManyToOne(targetEntity="DDC1452EntityA")
125      */
126     public $entityATo;
127 }