PageRenderTime 41ms CodeModel.GetById 12ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php

https://github.com/dbruenig/doctrine2
PHP | 675 lines | 605 code | 51 blank | 19 comment | 9 complexity | 366c3a3a3bd44a31975fda14c950c2a7 MD5 | raw file
  1<?php
  2
  3namespace Doctrine\Tests\ORM\Tools;
  4
  5use Doctrine\ORM\Tools\SchemaTool;
  6use Doctrine\ORM\Tools\EntityGenerator;
  7use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  8use Doctrine\ORM\Mapping\ClassMetadataInfo;
  9use Doctrine\ORM\Mapping\ClassMetadataFactory;
 10use Doctrine\Tests\Models\DDC2372\DDC2372User;
 11use Doctrine\Tests\Models\DDC2372\DDC2372Admin;
 12
 13class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
 14{
 15
 16    /**
 17     * @var EntityGenerator
 18     */
 19    private $_generator;
 20    private $_tmpDir;
 21    private $_namespace;
 22
 23    public function setUp()
 24    {
 25        $this->_namespace = uniqid("doctrine_");
 26        $this->_tmpDir = \sys_get_temp_dir();
 27        \mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace);
 28        $this->_generator = new EntityGenerator();
 29        $this->_generator->setAnnotationPrefix("");
 30        $this->_generator->setGenerateAnnotations(true);
 31        $this->_generator->setGenerateStubMethods(true);
 32        $this->_generator->setRegenerateEntityIfExists(false);
 33        $this->_generator->setUpdateEntityIfExists(true);
 34        $this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED);
 35    }
 36
 37    public function tearDown()
 38    {
 39        $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
 40        foreach ($ri AS $file) {
 41            /* @var $file \SplFileInfo */
 42            if ($file->isFile()) {
 43                \unlink($file->getPathname());
 44            }
 45        }
 46        rmdir($this->_tmpDir . '/' . $this->_namespace);
 47    }
 48
 49    public function generateBookEntityFixture()
 50    {
 51        $metadata = new ClassMetadataInfo($this->_namespace . '\EntityGeneratorBook');
 52        $metadata->namespace = $this->_namespace;
 53        $metadata->customRepositoryClassName = $this->_namespace  . '\EntityGeneratorBookRepository';
 54
 55        $metadata->table['name'] = 'book';
 56        $metadata->table['uniqueConstraints']['name_uniq'] = array('columns' => array('name'));
 57        $metadata->table['indexes']['status_idx'] = array('columns' => array('status'));
 58        $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
 59        $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
 60        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
 61        $metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
 62        $joinColumns = array(
 63            array('name' => 'author_id', 'referencedColumnName' => 'id')
 64        );
 65        $metadata->mapManyToMany(array(
 66            'fieldName' => 'comments',
 67            'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
 68            'fetch' => ClassMetadataInfo::FETCH_EXTRA_LAZY,
 69            'joinTable' => array(
 70                'name' => 'book_comment',
 71                'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
 72                'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
 73            ),
 74        ));
 75        $metadata->addLifecycleCallback('loading', 'postLoad');
 76        $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
 77        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
 78
 79        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
 80
 81        return $metadata;
 82    }
 83
 84    private function generateEntityTypeFixture(array $field)
 85    {
 86        $metadata = new ClassMetadataInfo($this->_namespace . '\EntityType');
 87        $metadata->namespace = $this->_namespace;
 88
 89        $metadata->table['name'] = 'entity_type';
 90        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
 91        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
 92
 93        $name  = $field['fieldName'];
 94        $type  = $field['dbType'];
 95        $metadata->mapField(array('fieldName' => $name, 'type' => $type));
 96
 97        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
 98
 99        return $metadata;
100    }
101
102    /**
103     * @param  ClassMetadataInfo $metadata
104     * @return EntityGeneratorBook
105     */
106    public function newInstance($metadata)
107    {
108        $path = $this->_tmpDir . '/'. $this->_namespace . '/EntityGeneratorBook.php';
109        $this->assertFileExists($path);
110        require_once $path;
111
112        return new $metadata->name;
113    }
114
115    public function testGeneratedEntityClass()
116    {
117        $metadata = $this->generateBookEntityFixture();
118
119        $book = $this->newInstance($metadata);
120        $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
121        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing.");
122        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
123        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
124        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
125        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing.");
126        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing.");
127        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing.");
128        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addComment'), "EntityGeneratorBook::addComment() missing.");
129        $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'removeComment'), "EntityGeneratorBook::removeComment() missing.");
130
131        $this->assertEquals('published', $book->getStatus());
132
133        $book->setName('Jonathan H. Wage');
134        $this->assertEquals('Jonathan H. Wage', $book->getName());
135
136        $author = new EntityGeneratorAuthor();
137        $book->setAuthor($author);
138        $this->assertEquals($author, $book->getAuthor());
139
140        $comment = new EntityGeneratorComment();
141        $book->addComment($comment);
142        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
143        $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
144        $book->removeComment($comment);
145        $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
146    }
147
148    public function testEntityUpdatingWorks()
149    {
150        $metadata = $this->generateBookEntityFixture();
151        $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
152
153        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
154
155        $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
156
157        $book = $this->newInstance($metadata);
158        $reflClass = new \ReflectionClass($metadata->name);
159
160        $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
161        $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
162        $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
163
164        $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
165        $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
166        $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
167        $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
168        $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
169        $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
170    }
171
172    /**
173     * @group DDC-3152
174     */
175    public function testDoesNotRegenerateExistingMethodsWithDifferentCase()
176    {
177        $metadata = $this->generateBookEntityFixture();
178
179        // Workaround to change existing fields case (just to simulate the use case)
180        $metadata->fieldMappings['status']['fieldName'] = 'STATUS';
181
182        // Should not throw a PHP fatal error
183        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
184
185        $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
186
187        $this->newInstance($metadata);
188        $reflClass = new \ReflectionClass($metadata->name);
189
190        $this->assertTrue($reflClass->hasProperty('status'));
191        $this->assertTrue($reflClass->hasProperty('STATUS'));
192        $this->assertTrue($reflClass->hasMethod('getStatus'));
193        $this->assertTrue($reflClass->hasMethod('setStatus'));
194    }
195
196    /**
197     * @group DDC-2121
198     */
199    public function testMethodDocBlockShouldStartWithBackSlash()
200    {
201        $metadata   = $this->generateBookEntityFixture();
202        $book       = $this->newInstance($metadata);
203
204        $this->assertPhpDocVarType('\Doctrine\Common\Collections\Collection', new \ReflectionProperty($book, 'comments'));
205        $this->assertPhpDocReturnType('\Doctrine\Common\Collections\Collection', new \ReflectionMethod($book, 'getComments'));
206        $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorComment', new \ReflectionMethod($book, 'addComment'));
207        $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorComment', new \ReflectionMethod($book, 'removeComment'));
208
209        $this->assertPhpDocVarType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionProperty($book, 'author'));
210        $this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'getAuthor'));
211        $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'setAuthor'));
212    }
213
214    public function testEntityExtendsStdClass()
215    {
216        $this->_generator->setClassToExtend('stdClass');
217        $metadata = $this->generateBookEntityFixture();
218
219        $book = $this->newInstance($metadata);
220        $this->assertInstanceOf('stdClass', $book);
221    }
222
223    public function testLifecycleCallbacks()
224    {
225        $metadata = $this->generateBookEntityFixture();
226
227        $book = $this->newInstance($metadata);
228        $reflClass = new \ReflectionClass($metadata->name);
229
230        $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
231        $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
232    }
233
234    public function testLoadMetadata()
235    {
236        $metadata = $this->generateBookEntityFixture();
237
238        $book = $this->newInstance($metadata);
239
240        $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
241        $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
242
243        $driver = $this->createAnnotationDriver();
244        $driver->loadMetadataForClass($cm->name, $cm);
245
246        $this->assertEquals($cm->columnNames, $metadata->columnNames);
247        $this->assertEquals($cm->getTableName(), $metadata->getTableName());
248        $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
249        $this->assertEquals($cm->identifier, $metadata->identifier);
250        $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
251        $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
252
253        $this->assertEquals(ClassMetadataInfo::FETCH_EXTRA_LAZY, $cm->associationMappings['comments']['fetch']);
254    }
255
256    public function testLoadPrefixedMetadata()
257    {
258        $this->_generator->setAnnotationPrefix('ORM\\');
259        $metadata = $this->generateBookEntityFixture();
260
261        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
262        $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array());
263
264        $book = $this->newInstance($metadata);
265
266        $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
267        $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
268
269        $driver->loadMetadataForClass($cm->name, $cm);
270
271        $this->assertEquals($cm->columnNames, $metadata->columnNames);
272        $this->assertEquals($cm->getTableName(), $metadata->getTableName());
273        $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
274        $this->assertEquals($cm->identifier, $metadata->identifier);
275        $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
276        $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
277    }
278
279    /**
280     * @dataProvider getParseTokensInEntityFileData
281     */
282    public function testParseTokensInEntityFile($php, $classes)
283    {
284        $r = new \ReflectionObject($this->_generator);
285        $m = $r->getMethod('parseTokensInEntityFile');
286        $m->setAccessible(true);
287
288        $p = $r->getProperty('staticReflection');
289        $p->setAccessible(true);
290
291        $ret = $m->invoke($this->_generator, $php);
292        $this->assertEquals($classes, array_keys($p->getValue($this->_generator)));
293    }
294
295    /**
296     * @group DDC-1784
297     */
298    public function testGenerateEntityWithSequenceGenerator()
299    {
300        $metadata               = new ClassMetadataInfo($this->_namespace . '\DDC1784Entity');
301        $metadata->namespace    = $this->_namespace;
302        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
303        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
304        $metadata->setSequenceGeneratorDefinition(array(
305            'sequenceName'      => 'DDC1784_ID_SEQ',
306            'allocationSize'    => 1,
307            'initialValue'      => 2
308        ));
309        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
310
311        $filename = $this->_tmpDir . DIRECTORY_SEPARATOR
312                  . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php';
313
314        $this->assertFileExists($filename);
315        require_once $filename;
316
317
318        $reflection = new \ReflectionProperty($metadata->name, 'id');
319        $docComment = $reflection->getDocComment();
320
321        $this->assertContains('@Id', $docComment);
322        $this->assertContains('@Column(name="id", type="integer")', $docComment);
323        $this->assertContains('@GeneratedValue(strategy="SEQUENCE")', $docComment);
324        $this->assertContains('@SequenceGenerator(sequenceName="DDC1784_ID_SEQ", allocationSize=1, initialValue=2)', $docComment);
325    }
326
327    /**
328     * @group DDC-2079
329     */
330    public function testGenerateEntityWithMultipleInverseJoinColumns()
331    {
332        $metadata               = new ClassMetadataInfo($this->_namespace . '\DDC2079Entity');
333        $metadata->namespace    = $this->_namespace;
334        $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
335        $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
336        $metadata->mapManyToMany(array(
337            'fieldName'     => 'centroCustos',
338            'targetEntity'  => 'DDC2079CentroCusto',
339            'joinTable'     => array(
340                'name'                  => 'unidade_centro_custo',
341                'joinColumns'           => array(
342                    array('name' => 'idorcamento',      'referencedColumnName' => 'idorcamento'),
343                    array('name' => 'idunidade',        'referencedColumnName' => 'idunidade')
344                ),
345                'inverseJoinColumns'    => array(
346                    array('name' => 'idcentrocusto',    'referencedColumnName' => 'idcentrocusto'),
347                    array('name' => 'idpais',           'referencedColumnName' => 'idpais'),
348                ),
349            ),
350        ));
351        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
352
353        $filename = $this->_tmpDir . DIRECTORY_SEPARATOR
354            . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC2079Entity.php';
355
356        $this->assertFileExists($filename);
357        require_once $filename;
358
359        $property   = new \ReflectionProperty($metadata->name, 'centroCustos');
360        $docComment = $property->getDocComment();
361
362        //joinColumns
363        $this->assertContains('@JoinColumn(name="idorcamento", referencedColumnName="idorcamento"),', $docComment);
364        $this->assertContains('@JoinColumn(name="idunidade", referencedColumnName="idunidade")', $docComment);
365        //inverseJoinColumns
366        $this->assertContains('@JoinColumn(name="idcentrocusto", referencedColumnName="idcentrocusto"),', $docComment);
367        $this->assertContains('@JoinColumn(name="idpais", referencedColumnName="idpais")', $docComment);
368
369    }
370
371     /**
372     * @group DDC-2172
373     */
374    public function testGetInheritanceTypeString()
375    {
376        $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
377        $method     = new \ReflectionMethod($this->_generator, 'getInheritanceTypeString');
378        $constants  = $reflection->getConstants();
379        $pattern    = '/^INHERITANCE_TYPE_/';
380
381        $method->setAccessible(true);
382
383        foreach ($constants as $name => $value) {
384            if( ! preg_match($pattern, $name)) {
385                continue;
386            }
387
388            $expected = preg_replace($pattern, '', $name);
389            $actual   = $method->invoke($this->_generator, $value);
390
391            $this->assertEquals($expected, $actual);
392        }
393
394        $this->setExpectedException('\InvalidArgumentException', 'Invalid provided InheritanceType: INVALID');
395        $method->invoke($this->_generator, 'INVALID');
396    }
397
398    /**
399    * @group DDC-2172
400    */
401    public function testGetChangeTrackingPolicyString()
402    {
403        $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
404        $method     = new \ReflectionMethod($this->_generator, 'getChangeTrackingPolicyString');
405        $constants  = $reflection->getConstants();
406        $pattern    = '/^CHANGETRACKING_/';
407
408        $method->setAccessible(true);
409
410        foreach ($constants as $name => $value) {
411            if( ! preg_match($pattern, $name)) {
412                continue;
413            }
414
415            $expected = preg_replace($pattern, '', $name);
416            $actual   = $method->invoke($this->_generator, $value);
417
418            $this->assertEquals($expected, $actual);
419        }
420
421        $this->setExpectedException('\InvalidArgumentException', 'Invalid provided ChangeTrackingPolicy: INVALID');
422        $method->invoke($this->_generator, 'INVALID');
423    }
424
425    /**
426     * @group DDC-2172
427     */
428    public function testGetIdGeneratorTypeString()
429    {
430        $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
431        $method     = new \ReflectionMethod($this->_generator, 'getIdGeneratorTypeString');
432        $constants  = $reflection->getConstants();
433        $pattern    = '/^GENERATOR_TYPE_/';
434
435        $method->setAccessible(true);
436
437        foreach ($constants as $name => $value) {
438            if( ! preg_match($pattern, $name)) {
439                continue;
440            }
441
442            $expected = preg_replace($pattern, '', $name);
443            $actual   = $method->invoke($this->_generator, $value);
444
445            $this->assertEquals($expected, $actual);
446        }
447
448        $this->setExpectedException('\InvalidArgumentException', 'Invalid provided IdGeneratorType: INVALID');
449        $method->invoke($this->_generator, 'INVALID');
450    }
451
452    /**
453     * @dataProvider getEntityTypeAliasDataProvider
454     *
455     * @group DDC-1694
456     */
457    public function testEntityTypeAlias(array $field)
458    {
459        $metadata   = $this->generateEntityTypeFixture($field);
460        $path       = $this->_tmpDir . '/'. $this->_namespace . '/EntityType.php';
461
462        $this->assertFileExists($path);
463        require_once $path;
464
465        $entity     = new $metadata->name;
466        $reflClass  = new \ReflectionClass($metadata->name);
467
468        $type   = $field['phpType'];
469        $name   = $field['fieldName'];
470        $value  = $field['value'];
471        $getter = "get" . ucfirst($name);
472        $setter = "set" . ucfirst($name);
473
474        $this->assertPhpDocVarType($type, $reflClass->getProperty($name));
475        $this->assertPhpDocParamType($type, $reflClass->getMethod($setter));
476        $this->assertPhpDocReturnType($type, $reflClass->getMethod($getter));
477
478        $this->assertSame($entity, $entity->{$setter}($value));
479        $this->assertEquals($value, $entity->{$getter}());
480    }
481
482    /**
483     * @group DDC-2372
484     */
485    public function testTraitPropertiesAndMethodsAreNotDuplicated()
486    {
487        if (PHP_VERSION_ID < 50400) {
488            $this->markTestSkipped('Traits are not available before php 5.4.');
489        }
490
491        $cmf = new ClassMetadataFactory();
492        $em = $this->_getTestEntityManager();
493        $cmf->setEntityManager($em);
494
495        $user = new DDC2372User();
496        $metadata = $cmf->getMetadataFor(get_class($user));
497        $metadata->name = $this->_namespace . "\DDC2372User";
498        $metadata->namespace = $this->_namespace;
499
500        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
501
502        $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/DDC2372User.php");
503        require $this->_tmpDir . "/" . $this->_namespace . "/DDC2372User.php";
504
505        $reflClass = new \ReflectionClass($metadata->name);
506
507        $this->assertSame($reflClass->hasProperty('address'), false);
508        $this->assertSame($reflClass->hasMethod('setAddress'), false);
509        $this->assertSame($reflClass->hasMethod('getAddress'), false);
510    }
511
512    /**
513     * @group DDC-2372
514     */
515    public function testTraitPropertiesAndMethodsAreNotDuplicatedInChildClasses()
516    {
517        if (PHP_VERSION_ID < 50400) {
518            $this->markTestSkipped('Traits are not available before php 5.4.');
519        }
520
521        $cmf = new ClassMetadataFactory();
522        $em = $this->_getTestEntityManager();
523        $cmf->setEntityManager($em);
524
525        $user = new DDC2372Admin();
526        $metadata = $cmf->getMetadataFor(get_class($user));
527        $metadata->name = $this->_namespace . "\DDC2372Admin";
528        $metadata->namespace = $this->_namespace;
529
530        $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
531
532        $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/DDC2372Admin.php");
533        require $this->_tmpDir . "/" . $this->_namespace . "/DDC2372Admin.php";
534
535        $reflClass = new \ReflectionClass($metadata->name);
536
537        $this->assertSame($reflClass->hasProperty('address'), false);
538        $this->assertSame($reflClass->hasMethod('setAddress'), false);
539        $this->assertSame($reflClass->hasMethod('getAddress'), false);
540    }
541
542    /**
543     * @return array
544     */
545    public function getEntityTypeAliasDataProvider()
546    {
547        return array(
548            array(array(
549                'fieldName' => 'datetimetz',
550                'phpType' => '\\DateTime',
551                'dbType' => 'datetimetz',
552                'value' => new \DateTime
553            )),
554            array(array(
555                'fieldName' => 'datetime',
556                'phpType' => '\\DateTime',
557                'dbType' => 'datetime',
558                'value' => new \DateTime
559            )),
560            array(array(
561                'fieldName' => 'date',
562                'phpType' => '\\DateTime',
563                'dbType' => 'date',
564                'value' => new \DateTime
565            )),
566            array(array(
567                'fieldName' => 'time',
568                'phpType' => '\DateTime',
569                'dbType' => 'time',
570                'value' => new \DateTime
571            )),
572            array(array(
573                'fieldName' => 'object',
574                'phpType' => '\stdClass',
575                'dbType' => 'object',
576                'value' => new \stdClass()
577            )),
578            array(array(
579                'fieldName' => 'bigint',
580                'phpType' => 'integer',
581                'dbType' => 'bigint',
582                'value' => 11
583            )),
584            array(array(
585                'fieldName' => 'smallint',
586                'phpType' => 'integer',
587                'dbType' => 'smallint',
588                'value' => 22
589            )),
590            array(array(
591                'fieldName' => 'text',
592                'phpType' => 'string',
593                'dbType' => 'text',
594                'value' => 'text'
595            )),
596            array(array(
597                'fieldName' => 'blob',
598                'phpType' => 'string',
599                'dbType' => 'blob',
600                'value' => 'blob'
601            )),
602            array(array(
603                'fieldName' => 'decimal',
604                'phpType' => 'string',
605                'dbType' => 'decimal',
606                'value' => '12.34'
607            ),
608        ));
609    }
610
611    public function getParseTokensInEntityFileData()
612    {
613        return array(
614            array(
615                '<?php namespace Foo\Bar; class Baz {}',
616                array('Foo\Bar\Baz'),
617            ),
618            array(
619                '<?php namespace Foo\Bar; use Foo; class Baz {}',
620                array('Foo\Bar\Baz'),
621            ),
622            array(
623                '<?php namespace /*Comment*/ Foo\Bar; /** Foo */class /* Comment */ Baz {}',
624                array('Foo\Bar\Baz'),
625            ),
626            array(
627                '
628<?php namespace
629/*Comment*/
630Foo\Bar
631;
632
633/** Foo */
634class
635/* Comment */
636 Baz {}
637     ',
638                array('Foo\Bar\Baz'),
639            ),
640        );
641    }
642
643    /**
644     * @param string $type
645     * @param \ReflectionProperty $property
646     */
647    private function assertPhpDocVarType($type, \ReflectionProperty $property)
648    {
649        $this->assertEquals(1, preg_match('/@var\s+([^\s]+)/',$property->getDocComment(), $matches));
650        $this->assertEquals($type, $matches[1]);
651    }
652
653    /**
654     * @param string $type
655     * @param \ReflectionProperty $method
656     */
657    private function assertPhpDocReturnType($type, \ReflectionMethod $method)
658    {
659        $this->assertEquals(1, preg_match('/@return\s+([^\s]+)/', $method->getDocComment(), $matches));
660        $this->assertEquals($type, $matches[1]);
661    }
662
663    /**
664     * @param string $type
665     * @param \ReflectionProperty $method
666     */
667    private function assertPhpDocParamType($type, \ReflectionMethod $method)
668    {
669        $this->assertEquals(1, preg_match('/@param\s+([^\s]+)/', $method->getDocComment(), $matches));
670        $this->assertEquals($type, $matches[1]);
671    }
672}
673
674class EntityGeneratorAuthor {}
675class EntityGeneratorComment {}