Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / DefaultValuesTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 require_once __DIR__ . '/../../TestInit.php';
6
7 /**
8  * Tests basic operations on entities with default values.
9  *
10  * @author robo
11  */
12 class DefaultValuesTest extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     protected function setUp() {
15         parent::setUp();
16         try {
17             $this->_schemaTool->createSchema(array(
18                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\DefaultValueUser'),
19                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\DefaultValueAddress')
20             ));
21         } catch (\Exception $e) {
22             // Swallow all exceptions. We do not test the schema tool here.
23         }
24     }
25
26     public function testSimpleDetachMerge() {
27         $user = new DefaultValueUser;
28         $user->name = 'romanb';
29         $this->_em->persist($user);
30         $this->_em->flush();
31         $this->_em->clear();
32
33         $userId = $user->id; // e.g. from $_REQUEST
34         $user2 = $this->_em->getReference(get_class($user), $userId);
35
36         $this->_em->flush();
37         $this->assertFalse($user2->__isInitialized__);
38
39         $a = new DefaultValueAddress;
40         $a->country = 'de';
41         $a->zip = '12345';
42         $a->city = 'Berlin';
43         $a->street = 'Sesamestreet';
44
45         $a->user = $user2;
46         $this->_em->persist($a);
47         $this->_em->flush();
48
49         $this->assertFalse($user2->__isInitialized__);
50         $this->_em->clear();
51
52         $a2 = $this->_em->find(get_class($a), $a->id);
53         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\DefaultValueUser', $a2->getUser());
54         $this->assertEquals($userId, $a2->getUser()->getId());
55         $this->assertEquals('Poweruser', $a2->getUser()->type);
56     }
57
58     /**
59      * @group DDC-1386
60      */
61     public function testGetPartialReferenceWithDefaultValueNotEvalutedInFlush()
62     {
63         $user = new DefaultValueUser;
64         $user->name = 'romanb';
65         $user->type = 'Normaluser';
66
67         $this->_em->persist($user);
68         $this->_em->flush();
69         $this->_em->clear();
70
71         $user = $this->_em->getPartialReference('Doctrine\Tests\ORM\Functional\DefaultValueUser', $user->id);
72         $this->assertTrue($this->_em->getUnitOfWork()->isReadOnly($user));
73
74         $this->_em->flush();
75         $this->_em->clear();
76
77         $user = $this->_em->find('Doctrine\Tests\ORM\Functional\DefaultValueUser', $user->id);
78
79         $this->assertEquals('Normaluser', $user->type);
80     }
81 }
82
83
84 /**
85  * @Entity @Table(name="defaultvalueuser")
86  */
87 class DefaultValueUser
88 {
89     /**
90      * @Id @Column(type="integer")
91      * @GeneratedValue(strategy="AUTO")
92      */
93     public $id;
94     /**
95      * @Column(type="string")
96      */
97     public $name = '';
98     /**
99      * @Column(type="string")
100      */
101     public $type = 'Poweruser';
102     /**
103      * @OneToOne(targetEntity="DefaultValueAddress", mappedBy="user", cascade={"persist"})
104      */
105     public $address;
106
107     public function getId() {return $this->id;}
108 }
109
110 /**
111  * CmsAddress
112  *
113  * @Entity @Table(name="defaultvalueaddresses")
114  */
115 class DefaultValueAddress
116 {
117     /**
118      * @Column(type="integer")
119      * @Id @GeneratedValue(strategy="AUTO")
120      */
121     public $id;
122
123     /**
124      * @Column(type="string", length=50)
125      */
126     public $country;
127
128     /**
129      * @Column(type="string", length=50)
130      */
131     public $zip;
132
133     /**
134      * @Column(type="string", length=50)
135      */
136     public $city;
137
138     /**
139      * Testfield for Schema Updating Tests.
140      */
141     public $street;
142
143     /**
144      * @OneToOne(targetEntity="DefaultValueUser")
145      * @JoinColumn(name="user_id", referencedColumnName="id")
146      */
147     public $user;
148
149     public function getUser() {return $this->user;}
150 }