Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Mapping / ClassMetadataBuilderTest.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the LGPL. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20
21 namespace Doctrine\Tests\ORM\Mapping;
22
23 use Doctrine\ORM\Mapping\ClassMetadata;
24 use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
25
26 /**
27  * @group DDC-659
28  */
29 class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase
30 {
31     /**
32      * @var ClassMetadata
33      */
34     private $cm;
35     /**
36      * @var ClassMetadataBuilder
37      */
38     private $builder;
39
40     public function setUp()
41     {
42         $this->cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
43         $this->cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
44         $this->builder = new ClassMetadataBuilder($this->cm);
45     }
46
47     public function testSetMappedSuperClass()
48     {
49         $this->assertIsFluent($this->builder->setMappedSuperClass());
50         $this->assertTrue($this->cm->isMappedSuperclass);
51     }
52
53     public function testSetCustomRepositoryClass()
54     {
55         $this->assertIsFluent($this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup'));
56         $this->assertEquals('Doctrine\Tests\Models\CMS\CmsGroup', $this->cm->customRepositoryClassName);
57     }
58
59     public function testSetReadOnly()
60     {
61         $this->assertIsFluent($this->builder->setReadOnly());
62         $this->assertTrue($this->cm->isReadOnly);
63     }
64
65     public function testSetTable()
66     {
67         $this->assertIsFluent($this->builder->setTable('users'));
68         $this->assertEquals('users', $this->cm->table['name']);
69     }
70
71     public function testAddIndex()
72     {
73         $this->assertIsFluent($this->builder->addIndex(array('username', 'name'), 'users_idx'));
74         $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['indexes']);
75     }
76
77     public function testAddUniqueConstraint()
78     {
79         $this->assertIsFluent($this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'));
80         $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['uniqueConstraints']);
81     }
82
83     public function testSetPrimaryTableRelated()
84     {
85         $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx');
86         $this->builder->addIndex(array('username', 'name'), 'users_idx');
87         $this->builder->setTable('users');
88
89         $this->assertEquals(
90             array(
91                 'name' => 'users',
92                 'indexes' => array('users_idx' => array('columns' => array('username', 'name'))),
93                 'uniqueConstraints' => array('users_idx' => array('columns' => array('username', 'name'))),
94             ),
95             $this->cm->table
96         );
97     }
98
99     public function testSetInheritanceJoined()
100     {
101         $this->assertIsFluent($this->builder->setJoinedTableInheritance());
102         $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_JOINED, $this->cm->inheritanceType);
103     }
104
105     public function testSetInheritanceSingleTable()
106     {
107         $this->assertIsFluent($this->builder->setSingleTableInheritance());
108         $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE, $this->cm->inheritanceType);
109     }
110
111     public function testSetDiscriminatorColumn()
112     {
113         $this->assertIsFluent($this->builder->setDiscriminatorColumn('discr', 'string', '124'));
114         $this->assertEquals(array('fieldName' => 'discr', 'name' => 'discr', 'type' => 'string', 'length' => '124'), $this->cm->discriminatorColumn);
115     }
116
117     public function testAddDiscriminatorMapClass()
118     {
119         $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test', 'Doctrine\Tests\Models\CMS\CmsUser'));
120         $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test2', 'Doctrine\Tests\Models\CMS\CmsGroup'));
121
122         $this->assertEquals(array('test' => 'Doctrine\Tests\Models\CMS\CmsUser', 'test2' => 'Doctrine\Tests\Models\CMS\CmsGroup'), $this->cm->discriminatorMap);
123         $this->assertEquals('test', $this->cm->discriminatorValue);
124     }
125
126     public function testChangeTrackingPolicyExplicit()
127     {
128         $this->assertIsFluent($this->builder->setChangeTrackingPolicyDeferredExplicit());
129         $this->assertEquals(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT, $this->cm->changeTrackingPolicy);
130     }
131
132     public function testChangeTrackingPolicyNotify()
133     {
134         $this->assertIsFluent($this->builder->setChangeTrackingPolicyNotify());
135         $this->assertEquals(ClassMetadata::CHANGETRACKING_NOTIFY, $this->cm->changeTrackingPolicy);
136     }
137
138     public function testAddField()
139     {
140         $this->assertIsFluent($this->builder->addField('name', 'string'));
141         $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']);
142     }
143
144     public function testCreateField()
145     {
146         $fieldBuilder = ($this->builder->createField('name', 'string'));
147         $this->assertInstanceOf('Doctrine\ORM\Mapping\Builder\FieldBuilder', $fieldBuilder);
148
149         $this->assertFalse(isset($this->cm->fieldMappings['name']));
150         $this->assertIsFluent($fieldBuilder->build());
151         $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']);
152     }
153
154     public function testCreateVersionedField()
155     {
156         $this->builder->createField('name', 'integer')->columnName('username')->length(124)->nullable()->columnDefinition('foobar')->unique()->isVersionField()->build();
157         $this->assertEquals(array(
158             'columnDefinition' => 'foobar',
159             'columnName' => 'username',
160             'default' => 1,
161             'fieldName' => 'name',
162             'length' => 124,
163             'type' => 'integer',
164             'nullable' => true,
165             'unique' => true,
166         ), $this->cm->fieldMappings['name']);
167     }
168
169     public function testCreatePrimaryField()
170     {
171         $this->builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
172
173         $this->assertEquals(array('id'), $this->cm->identifier);
174         $this->assertEquals(array('columnName' => 'id', 'fieldName' => 'id', 'id' => true, 'type' => 'integer'), $this->cm->fieldMappings['id']);
175     }
176
177     public function testAddLifecycleEvent()
178     {
179         $this->builder->addLifecycleEvent('getStatus', 'postLoad');
180
181         $this->assertEquals(array('postLoad' => array('getStatus')), $this->cm->lifecycleCallbacks);
182     }
183
184     public function testCreateManyToOne()
185     {
186         $this->assertIsFluent(
187             $this->builder->createManyToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
188                               ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
189                               ->cascadeAll()
190                               ->fetchExtraLazy()
191                               ->build()
192         );
193
194         $this->assertEquals(array('groups' => array (
195                 'fieldName' => 'groups',
196                 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
197                 'cascade' => array (
198                   0 => 'remove',
199                   1 => 'persist',
200                   2 => 'refresh',
201                   3 => 'merge',
202                   4 => 'detach',
203                 ),
204                 'fetch' => 4,
205                 'joinColumns' => array (
206                   0 =>
207                   array (
208                     'name' => 'group_id',
209                     'referencedColumnName' => 'id',
210                     'nullable' => true,
211                     'unique' => false,
212                     'onDelete' => 'CASCADE',
213                     'columnDefinition' => NULL,
214                   ),
215                 ),
216                 'type' => 2,
217                 'mappedBy' => NULL,
218                 'inversedBy' => NULL,
219                 'isOwningSide' => true,
220                 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
221                 'isCascadeRemove' => true,
222                 'isCascadePersist' => true,
223                 'isCascadeRefresh' => true,
224                 'isCascadeMerge' => true,
225                 'isCascadeDetach' => true,
226                 'sourceToTargetKeyColumns' =>
227                 array (
228                   'group_id' => 'id',
229                 ),
230                 'joinColumnFieldNames' =>
231                 array (
232                   'group_id' => 'group_id',
233                 ),
234                 'targetToSourceKeyColumns' =>
235                 array (
236                   'id' => 'group_id',
237                 ),
238                 'orphanRemoval' => false,
239               ),
240             ), $this->cm->associationMappings);
241     }
242
243     public function testCreateOneToOne()
244     {
245         $this->assertIsFluent(
246             $this->builder->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
247                               ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
248                               ->cascadeAll()
249                               ->fetchExtraLazy()
250                               ->build()
251         );
252
253         $this->assertEquals(array('groups' => array (
254                 'fieldName' => 'groups',
255                 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
256                 'cascade' => array (
257                   0 => 'remove',
258                   1 => 'persist',
259                   2 => 'refresh',
260                   3 => 'merge',
261                   4 => 'detach',
262                 ),
263                 'fetch' => 4,
264                 'joinColumns' => array (
265                   0 =>
266                   array (
267                     'name' => 'group_id',
268                     'referencedColumnName' => 'id',
269                     'nullable' => true,
270                     'unique' => true,
271                     'onDelete' => 'CASCADE',
272                     'columnDefinition' => NULL,
273                   ),
274                 ),
275                 'type' => 1,
276                 'mappedBy' => NULL,
277                 'inversedBy' => NULL,
278                 'isOwningSide' => true,
279                 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
280                 'isCascadeRemove' => true,
281                 'isCascadePersist' => true,
282                 'isCascadeRefresh' => true,
283                 'isCascadeMerge' => true,
284                 'isCascadeDetach' => true,
285                 'sourceToTargetKeyColumns' =>
286                 array (
287                   'group_id' => 'id',
288                 ),
289                 'joinColumnFieldNames' =>
290                 array (
291                   'group_id' => 'group_id',
292                 ),
293                 'targetToSourceKeyColumns' =>
294                 array (
295                   'id' => 'group_id',
296                 ),
297                 'orphanRemoval' => false,
298               ),
299             ), $this->cm->associationMappings);
300     }
301
302     public function testCreateManyToMany()
303     {
304         $this->assertIsFluent(
305             $this->builder->createManyToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
306                               ->setJoinTable('groups_users')
307                               ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
308                               ->addInverseJoinColumn('user_id', 'id')
309                               ->cascadeAll()
310                               ->fetchExtraLazy()
311                               ->build()
312         );
313
314         $this->assertEquals(array(
315             'groups' =>
316             array(
317                 'fieldName' => 'groups',
318                 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
319                 'cascade' =>
320                 array(
321                     0 => 'remove',
322                     1 => 'persist',
323                     2 => 'refresh',
324                     3 => 'merge',
325                     4 => 'detach',
326                 ),
327                 'fetch' => 4,
328                 'joinTable' =>
329                 array(
330                     'joinColumns' =>
331                     array(
332                         0 =>
333                         array(
334                             'name' => 'group_id',
335                             'referencedColumnName' => 'id',
336                             'nullable' => true,
337                             'unique' => false,
338                             'onDelete' => 'CASCADE',
339                             'columnDefinition' => NULL,
340                         ),
341                     ),
342                     'inverseJoinColumns' =>
343                     array(
344                         0 =>
345                         array(
346                             'name' => 'user_id',
347                             'referencedColumnName' => 'id',
348                             'nullable' => true,
349                             'unique' => false,
350                             'onDelete' => NULL,
351                             'columnDefinition' => NULL,
352                         ),
353                     ),
354                     'name' => 'groups_users',
355                 ),
356                 'type' => 8,
357                 'mappedBy' => NULL,
358                 'inversedBy' => NULL,
359                 'isOwningSide' => true,
360                 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
361                 'isCascadeRemove' => true,
362                 'isCascadePersist' => true,
363                 'isCascadeRefresh' => true,
364                 'isCascadeMerge' => true,
365                 'isCascadeDetach' => true,
366                 'isOnDeleteCascade' => true,
367                 'relationToSourceKeyColumns' =>
368                 array(
369                     'group_id' => 'id',
370                 ),
371                 'joinTableColumns' =>
372                 array(
373                     0 => 'group_id',
374                     1 => 'user_id',
375                 ),
376                 'relationToTargetKeyColumns' =>
377                 array(
378                     'user_id' => 'id',
379                 ),
380                 'orphanRemoval' => false,
381             ),
382                 ), $this->cm->associationMappings);
383     }
384
385     public function testCreateOneToMany()
386     {
387         $this->assertIsFluent(
388                 $this->builder->createOneToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
389                         ->mappedBy('test')
390                         ->setOrderBy(array('test'))
391                         ->setIndexBy('test')
392                         ->build()
393         );
394
395         $this->assertEquals(array(
396             'groups' =>
397             array(
398                 'fieldName' => 'groups',
399                 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
400                 'mappedBy' => 'test',
401                 'orderBy' =>
402                 array(
403                     0 => 'test',
404                 ),
405                 'indexBy' => 'test',
406                 'type' => 4,
407                 'inversedBy' => NULL,
408                 'isOwningSide' => false,
409                 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
410                 'fetch' => 2,
411                 'cascade' =>
412                 array(
413                 ),
414                 'isCascadeRemove' => false,
415                 'isCascadePersist' => false,
416                 'isCascadeRefresh' => false,
417                 'isCascadeMerge' => false,
418                 'isCascadeDetach' => false,
419                 'orphanRemoval' => false,
420             ),
421                 ), $this->cm->associationMappings);
422     }
423
424     public function assertIsFluent($ret)
425     {
426         $this->assertSame($this->builder, $ret, "Return Value has to be same instance as used builder");
427     }
428 }