2010-03-19 00:38:42 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Tools;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Tools\SchemaTool,
|
|
|
|
Doctrine\ORM\Tools\EntityGenerator,
|
|
|
|
Doctrine\ORM\Tools\Export\ClassMetadataExporter,
|
|
|
|
Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EntityGenerator
|
|
|
|
*/
|
2010-03-19 01:35:16 +03:00
|
|
|
private $_generator;
|
2011-02-03 01:21:42 +03:00
|
|
|
private $_tmpDir;
|
|
|
|
private $_namespace;
|
2010-03-19 01:35:16 +03:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_namespace = uniqid("doctrine_");
|
|
|
|
$this->_tmpDir = \sys_get_temp_dir();
|
|
|
|
\mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace);
|
2010-03-19 01:35:16 +03:00
|
|
|
$this->_generator = new EntityGenerator();
|
2011-10-15 22:44:25 +04:00
|
|
|
$this->_generator->setAnnotationPrefix("");
|
2010-03-19 01:35:16 +03:00
|
|
|
$this->_generator->setGenerateAnnotations(true);
|
|
|
|
$this->_generator->setGenerateStubMethods(true);
|
|
|
|
$this->_generator->setRegenerateEntityIfExists(false);
|
|
|
|
$this->_generator->setUpdateEntityIfExists(true);
|
2012-01-13 14:24:35 +04:00
|
|
|
$this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED);
|
2010-03-19 01:35:16 +03:00
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
public function tearDown()
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2011-02-03 01:21:42 +03:00
|
|
|
$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
|
|
|
|
foreach ($ri AS $file) {
|
|
|
|
/* @var $file \SplFileInfo */
|
|
|
|
if ($file->isFile()) {
|
|
|
|
\unlink($file->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir($this->_tmpDir . '/' . $this->_namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateBookEntityFixture()
|
|
|
|
{
|
|
|
|
$metadata = new ClassMetadataInfo($this->_namespace . '\EntityGeneratorBook');
|
|
|
|
$metadata->namespace = $this->_namespace;
|
|
|
|
$metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository';
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
$metadata->table['name'] = 'book';
|
2012-01-28 14:16:36 +04:00
|
|
|
$metadata->table['uniqueConstraints']['name_uniq'] = array('columns' => array('name'));
|
|
|
|
$metadata->table['indexes']['status_idx'] = array('columns' => array('status'));
|
2010-03-20 01:38:45 +03:00
|
|
|
$metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
|
|
|
|
$metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
|
2010-03-19 00:38:42 +03:00
|
|
|
$metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
|
2010-03-20 01:38:45 +03:00
|
|
|
$metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
|
2010-03-19 00:38:42 +03:00
|
|
|
$joinColumns = array(
|
2010-03-20 01:38:45 +03:00
|
|
|
array('name' => 'author_id', 'referencedColumnName' => 'id')
|
2010-03-19 00:38:42 +03:00
|
|
|
);
|
|
|
|
$metadata->mapManyToMany(array(
|
2010-03-20 01:38:45 +03:00
|
|
|
'fieldName' => 'comments',
|
|
|
|
'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
|
2013-05-09 12:55:12 +04:00
|
|
|
'fetch' => ClassMetadataInfo::FETCH_EXTRA_LAZY,
|
2010-03-19 00:38:42 +03:00
|
|
|
'joinTable' => array(
|
2010-03-20 01:38:45 +03:00
|
|
|
'name' => 'book_comment',
|
|
|
|
'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
|
|
|
|
'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
|
2010-03-19 00:38:42 +03:00
|
|
|
),
|
|
|
|
));
|
2011-02-03 01:21:42 +03:00
|
|
|
$metadata->addLifecycleCallback('loading', 'postLoad');
|
|
|
|
$metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
|
2010-03-19 00:38:42 +03:00
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
|
2010-03-19 01:35:16 +03:00
|
|
|
|
|
|
|
return $metadata;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 21:59:21 +04:00
|
|
|
private function generateEntityTypeFixture(array $field)
|
|
|
|
{
|
|
|
|
$metadata = new ClassMetadataInfo($this->_namespace . '\EntityType');
|
|
|
|
$metadata->namespace = $this->_namespace;
|
|
|
|
|
|
|
|
$metadata->table['name'] = 'entity_type';
|
|
|
|
$metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
|
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
|
|
|
|
|
|
|
|
$name = $field['fieldName'];
|
|
|
|
$type = $field['dbType'];
|
|
|
|
$metadata->mapField(array('fieldName' => $name, 'type' => $type));
|
|
|
|
|
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
/**
|
2011-02-03 01:21:42 +03:00
|
|
|
* @param ClassMetadataInfo $metadata
|
|
|
|
* @return EntityGeneratorBook
|
2010-03-19 00:38:42 +03:00
|
|
|
*/
|
2011-02-03 01:21:42 +03:00
|
|
|
public function newInstance($metadata)
|
|
|
|
{
|
|
|
|
$path = $this->_tmpDir . '/'. $this->_namespace . '/EntityGeneratorBook.php';
|
|
|
|
$this->assertFileExists($path);
|
|
|
|
require_once $path;
|
|
|
|
|
|
|
|
return new $metadata->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneratedEntityClass()
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2011-02-03 01:21:42 +03:00
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
$this->assertTrue(class_exists($metadata->name), "Class does not exist.");
|
2011-02-03 01:30:16 +03:00
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing.");
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
|
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
|
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
|
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing.");
|
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing.");
|
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing.");
|
2012-05-27 22:31:01 +04:00
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addComment'), "EntityGeneratorBook::addComment() missing.");
|
2012-07-04 23:03:50 +04:00
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'removeComment'), "EntityGeneratorBook::removeComment() missing.");
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->assertEquals('published', $book->getStatus());
|
2012-04-01 21:59:21 +04:00
|
|
|
|
|
|
|
$book->setName('Jonathan H. Wage');
|
2010-03-19 00:38:42 +03:00
|
|
|
$this->assertEquals('Jonathan H. Wage', $book->getName());
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$author = new EntityGeneratorAuthor();
|
|
|
|
$book->setAuthor($author);
|
|
|
|
$this->assertEquals($author, $book->getAuthor());
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$comment = new EntityGeneratorComment();
|
2012-05-27 22:31:01 +04:00
|
|
|
$book->addComment($comment);
|
2011-02-03 01:30:16 +03:00
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
|
|
|
|
$this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
|
2012-07-04 23:03:50 +04:00
|
|
|
$book->removeComment($comment);
|
|
|
|
$this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
|
2010-03-19 01:35:16 +03:00
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
public function testEntityUpdatingWorks()
|
2010-03-19 01:35:16 +03:00
|
|
|
{
|
2011-02-03 01:21:42 +03:00
|
|
|
$metadata = $this->generateBookEntityFixture();
|
2010-03-20 01:38:45 +03:00
|
|
|
$metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
|
2010-03-19 01:35:16 +03:00
|
|
|
|
2011-05-13 06:05:45 +04:00
|
|
|
$this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
|
2010-03-20 01:38:45 +03:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
$reflClass = new \ReflectionClass($metadata->name);
|
2010-03-19 01:35:16 +03:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
|
|
|
|
$this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
|
|
|
|
$this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
|
|
|
|
|
|
|
|
$this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
|
2012-01-13 17:14:28 +04:00
|
|
|
$this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
|
|
|
|
$this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
|
|
|
|
$this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
|
|
|
|
$this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
|
2010-08-28 18:29:08 +04:00
|
|
|
}
|
|
|
|
|
2012-11-06 03:53:07 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-2121
|
|
|
|
*/
|
|
|
|
public function testMethodDocBlockShouldStartWithBackSlash()
|
|
|
|
{
|
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
|
|
|
|
$this->assertPhpDocVarType('\Doctrine\Common\Collections\Collection', new \ReflectionProperty($book, 'comments'));
|
|
|
|
$this->assertPhpDocReturnType('\Doctrine\Common\Collections\Collection', new \ReflectionMethod($book, 'getComments'));
|
|
|
|
$this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorComment', new \ReflectionMethod($book, 'addComment'));
|
|
|
|
$this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorComment', new \ReflectionMethod($book, 'removeComment'));
|
|
|
|
|
|
|
|
$this->assertPhpDocVarType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionProperty($book, 'author'));
|
|
|
|
$this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'getAuthor'));
|
|
|
|
$this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'setAuthor'));
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
public function testEntityExtendsStdClass()
|
2010-08-28 18:29:08 +04:00
|
|
|
{
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_generator->setClassToExtend('stdClass');
|
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
$this->assertInstanceOf('stdClass', $book);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLifecycleCallbacks()
|
|
|
|
{
|
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
$reflClass = new \ReflectionClass($metadata->name);
|
|
|
|
|
|
|
|
$this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
|
|
|
|
$this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoadMetadata()
|
|
|
|
{
|
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
|
|
|
|
$cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
|
2012-01-03 00:32:18 +04:00
|
|
|
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
|
|
|
|
|
2011-05-25 02:26:20 +04:00
|
|
|
$driver = $this->createAnnotationDriver();
|
2011-02-03 01:21:42 +03:00
|
|
|
$driver->loadMetadataForClass($cm->name, $cm);
|
|
|
|
|
|
|
|
$this->assertEquals($cm->columnNames, $metadata->columnNames);
|
|
|
|
$this->assertEquals($cm->getTableName(), $metadata->getTableName());
|
|
|
|
$this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
|
|
|
|
$this->assertEquals($cm->identifier, $metadata->identifier);
|
|
|
|
$this->assertEquals($cm->idGenerator, $metadata->idGenerator);
|
|
|
|
$this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
|
2013-05-09 12:55:12 +04:00
|
|
|
|
|
|
|
$this->assertEquals(ClassMetadataInfo::FETCH_EXTRA_LAZY, $cm->associationMappings['comments']['fetch']);
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoadPrefixedMetadata()
|
|
|
|
{
|
2011-10-15 22:44:25 +04:00
|
|
|
$this->_generator->setAnnotationPrefix('ORM\\');
|
2011-02-03 01:21:42 +03:00
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
|
2011-10-15 22:44:25 +04:00
|
|
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
|
|
|
|
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array());
|
2011-02-03 01:21:42 +03:00
|
|
|
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
|
|
|
|
$cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
|
2012-01-03 00:32:18 +04:00
|
|
|
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$driver->loadMetadataForClass($cm->name, $cm);
|
2010-08-28 18:29:08 +04:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->assertEquals($cm->columnNames, $metadata->columnNames);
|
|
|
|
$this->assertEquals($cm->getTableName(), $metadata->getTableName());
|
|
|
|
$this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
|
|
|
|
$this->assertEquals($cm->identifier, $metadata->identifier);
|
|
|
|
$this->assertEquals($cm->idGenerator, $metadata->idGenerator);
|
|
|
|
$this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2011-04-30 12:42:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getParseTokensInEntityFileData
|
|
|
|
*/
|
|
|
|
public function testParseTokensInEntityFile($php, $classes)
|
|
|
|
{
|
|
|
|
$r = new \ReflectionObject($this->_generator);
|
2012-04-01 20:29:49 +04:00
|
|
|
$m = $r->getMethod('parseTokensInEntityFile');
|
2011-04-30 12:42:38 +04:00
|
|
|
$m->setAccessible(true);
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$p = $r->getProperty('staticReflection');
|
2011-04-30 12:42:38 +04:00
|
|
|
$p->setAccessible(true);
|
|
|
|
|
|
|
|
$ret = $m->invoke($this->_generator, $php);
|
|
|
|
$this->assertEquals($classes, array_keys($p->getValue($this->_generator)));
|
|
|
|
}
|
|
|
|
|
2012-04-19 03:39:21 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-1784
|
|
|
|
*/
|
|
|
|
public function testGenerateEntityWithSequenceGenerator()
|
|
|
|
{
|
|
|
|
$metadata = new ClassMetadataInfo($this->_namespace . '\DDC1784Entity');
|
|
|
|
$metadata->namespace = $this->_namespace;
|
|
|
|
$metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
|
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
|
|
|
|
$metadata->setSequenceGeneratorDefinition(array(
|
|
|
|
'sequenceName' => 'DDC1784_ID_SEQ',
|
|
|
|
'allocationSize' => 1,
|
|
|
|
'initialValue' => 2
|
|
|
|
));
|
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
|
|
|
|
|
|
|
|
$filename = $this->_tmpDir . DIRECTORY_SEPARATOR
|
|
|
|
. $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php';
|
|
|
|
|
|
|
|
$this->assertFileExists($filename);
|
|
|
|
require_once $filename;
|
|
|
|
|
|
|
|
|
|
|
|
$reflection = new \ReflectionProperty($metadata->name, 'id');
|
|
|
|
$docComment = $reflection->getDocComment();
|
|
|
|
|
|
|
|
$this->assertContains('@Id', $docComment);
|
|
|
|
$this->assertContains('@Column(name="id", type="integer")', $docComment);
|
|
|
|
$this->assertContains('@GeneratedValue(strategy="SEQUENCE")', $docComment);
|
|
|
|
$this->assertContains('@SequenceGenerator(sequenceName="DDC1784_ID_SEQ", allocationSize=1, initialValue=2)', $docComment);
|
|
|
|
}
|
|
|
|
|
2012-10-20 22:37:13 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-2079
|
|
|
|
*/
|
|
|
|
public function testGenerateEntityWithMultipleInverseJoinColumns()
|
|
|
|
{
|
|
|
|
$metadata = new ClassMetadataInfo($this->_namespace . '\DDC2079Entity');
|
|
|
|
$metadata->namespace = $this->_namespace;
|
|
|
|
$metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
|
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
|
|
|
|
$metadata->mapManyToMany(array(
|
|
|
|
'fieldName' => 'centroCustos',
|
|
|
|
'targetEntity' => 'DDC2079CentroCusto',
|
|
|
|
'joinTable' => array(
|
|
|
|
'name' => 'unidade_centro_custo',
|
|
|
|
'joinColumns' => array(
|
|
|
|
array('name' => 'idorcamento', 'referencedColumnName' => 'idorcamento'),
|
|
|
|
array('name' => 'idunidade', 'referencedColumnName' => 'idunidade')
|
|
|
|
),
|
|
|
|
'inverseJoinColumns' => array(
|
|
|
|
array('name' => 'idcentrocusto', 'referencedColumnName' => 'idcentrocusto'),
|
|
|
|
array('name' => 'idpais', 'referencedColumnName' => 'idpais'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
|
|
|
|
|
|
|
|
$filename = $this->_tmpDir . DIRECTORY_SEPARATOR
|
|
|
|
. $this->_namespace . DIRECTORY_SEPARATOR . 'DDC2079Entity.php';
|
|
|
|
|
|
|
|
$this->assertFileExists($filename);
|
|
|
|
require_once $filename;
|
|
|
|
|
2012-10-20 22:44:09 +04:00
|
|
|
$property = new \ReflectionProperty($metadata->name, 'centroCustos');
|
|
|
|
$docComment = $property->getDocComment();
|
2012-10-20 22:37:13 +04:00
|
|
|
|
|
|
|
//joinColumns
|
|
|
|
$this->assertContains('@JoinColumn(name="idorcamento", referencedColumnName="idorcamento"),', $docComment);
|
|
|
|
$this->assertContains('@JoinColumn(name="idunidade", referencedColumnName="idunidade")', $docComment);
|
|
|
|
//inverseJoinColumns
|
|
|
|
$this->assertContains('@JoinColumn(name="idcentrocusto", referencedColumnName="idcentrocusto"),', $docComment);
|
|
|
|
$this->assertContains('@JoinColumn(name="idpais", referencedColumnName="idpais")', $docComment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-27 20:29:02 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-2172
|
|
|
|
*/
|
|
|
|
public function testGetInheritanceTypeString()
|
|
|
|
{
|
|
|
|
$reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
|
|
|
|
$method = new \ReflectionMethod($this->_generator, 'getInheritanceTypeString');
|
|
|
|
$constants = $reflection->getConstants();
|
|
|
|
$pattern = '/^INHERITANCE_TYPE_/';
|
|
|
|
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
foreach ($constants as $name => $value) {
|
2012-11-27 20:38:18 +04:00
|
|
|
if( ! preg_match($pattern, $name)) {
|
|
|
|
continue;
|
2012-11-27 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
$expected = preg_replace($pattern, '', $name);
|
|
|
|
$actual = $method->invoke($this->_generator, $value);
|
2012-11-27 20:29:02 +04:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
$this->setExpectedException('\InvalidArgumentException', 'Invalid provided InheritanceType: INVALID');
|
|
|
|
$method->invoke($this->_generator, 'INVALID');
|
2012-11-27 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-2172
|
|
|
|
*/
|
2012-11-27 20:29:02 +04:00
|
|
|
public function testGetChangeTrackingPolicyString()
|
|
|
|
{
|
|
|
|
$reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
|
|
|
|
$method = new \ReflectionMethod($this->_generator, 'getChangeTrackingPolicyString');
|
|
|
|
$constants = $reflection->getConstants();
|
|
|
|
$pattern = '/^CHANGETRACKING_/';
|
|
|
|
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
foreach ($constants as $name => $value) {
|
2012-11-27 20:38:18 +04:00
|
|
|
if( ! preg_match($pattern, $name)) {
|
|
|
|
continue;
|
2012-11-27 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
$expected = preg_replace($pattern, '', $name);
|
|
|
|
$actual = $method->invoke($this->_generator, $value);
|
2012-11-27 20:29:02 +04:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
$this->setExpectedException('\InvalidArgumentException', 'Invalid provided ChangeTrackingPolicy: INVALID');
|
|
|
|
$method->invoke($this->_generator, 'INVALID');
|
2012-11-27 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-2172
|
|
|
|
*/
|
|
|
|
public function testGetIdGeneratorTypeString()
|
|
|
|
{
|
|
|
|
$reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
|
|
|
|
$method = new \ReflectionMethod($this->_generator, 'getIdGeneratorTypeString');
|
|
|
|
$constants = $reflection->getConstants();
|
|
|
|
$pattern = '/^GENERATOR_TYPE_/';
|
|
|
|
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
foreach ($constants as $name => $value) {
|
2012-11-27 20:38:18 +04:00
|
|
|
if( ! preg_match($pattern, $name)) {
|
|
|
|
continue;
|
2012-11-27 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
$expected = preg_replace($pattern, '', $name);
|
|
|
|
$actual = $method->invoke($this->_generator, $value);
|
2012-11-27 20:29:02 +04:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2012-11-27 20:38:18 +04:00
|
|
|
$this->setExpectedException('\InvalidArgumentException', 'Invalid provided IdGeneratorType: INVALID');
|
|
|
|
$method->invoke($this->_generator, 'INVALID');
|
2012-11-27 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
2012-04-01 21:59:21 +04:00
|
|
|
* @dataProvider getEntityTypeAliasDataProvider
|
|
|
|
*
|
2012-04-01 20:29:49 +04:00
|
|
|
* @group DDC-1694
|
|
|
|
*/
|
2012-04-01 21:59:21 +04:00
|
|
|
public function testEntityTypeAlias(array $field)
|
2012-04-01 20:29:49 +04:00
|
|
|
{
|
2012-04-01 21:59:21 +04:00
|
|
|
$metadata = $this->generateEntityTypeFixture($field);
|
|
|
|
$path = $this->_tmpDir . '/'. $this->_namespace . '/EntityType.php';
|
|
|
|
|
|
|
|
$this->assertFileExists($path);
|
|
|
|
require_once $path;
|
2012-04-01 20:29:49 +04:00
|
|
|
|
2012-04-01 21:59:21 +04:00
|
|
|
$entity = new $metadata->name;
|
|
|
|
$reflClass = new \ReflectionClass($metadata->name);
|
|
|
|
|
|
|
|
$type = $field['phpType'];
|
|
|
|
$name = $field['fieldName'];
|
|
|
|
$value = $field['value'];
|
|
|
|
$getter = "get" . ucfirst($name);
|
|
|
|
$setter = "set" . ucfirst($name);
|
|
|
|
|
|
|
|
$this->assertPhpDocVarType($type, $reflClass->getProperty($name));
|
|
|
|
$this->assertPhpDocParamType($type, $reflClass->getMethod($setter));
|
|
|
|
$this->assertPhpDocReturnType($type, $reflClass->getMethod($getter));
|
|
|
|
|
|
|
|
$this->assertSame($entity, $entity->{$setter}($value));
|
|
|
|
$this->assertEquals($value, $entity->{$getter}());
|
|
|
|
}
|
2012-04-19 03:39:21 +04:00
|
|
|
|
2012-04-01 21:59:21 +04:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEntityTypeAliasDataProvider()
|
|
|
|
{
|
2012-04-01 22:09:23 +04:00
|
|
|
return array(
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'datetimetz',
|
|
|
|
'phpType' => '\\DateTime',
|
|
|
|
'dbType' => 'datetimetz',
|
|
|
|
'value' => new \DateTime
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'datetime',
|
|
|
|
'phpType' => '\\DateTime',
|
|
|
|
'dbType' => 'datetime',
|
|
|
|
'value' => new \DateTime
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'date',
|
|
|
|
'phpType' => '\\DateTime',
|
|
|
|
'dbType' => 'date',
|
|
|
|
'value' => new \DateTime
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'time',
|
|
|
|
'phpType' => '\DateTime',
|
|
|
|
'dbType' => 'time',
|
|
|
|
'value' => new \DateTime
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'object',
|
|
|
|
'phpType' => '\stdClass',
|
|
|
|
'dbType' => 'object',
|
|
|
|
'value' => new \stdClass()
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'bigint',
|
|
|
|
'phpType' => 'integer',
|
|
|
|
'dbType' => 'bigint',
|
|
|
|
'value' => 11
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'smallint',
|
|
|
|
'phpType' => 'integer',
|
2012-04-01 22:09:23 +04:00
|
|
|
'dbType' => 'smallint',
|
2012-04-01 21:59:21 +04:00
|
|
|
'value' => 22
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'text',
|
|
|
|
'phpType' => 'string',
|
|
|
|
'dbType' => 'text',
|
|
|
|
'value' => 'text'
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
2012-04-01 21:59:21 +04:00
|
|
|
'fieldName' => 'blob',
|
|
|
|
'phpType' => 'string',
|
|
|
|
'dbType' => 'blob',
|
|
|
|
'value' => 'blob'
|
2012-04-01 22:09:23 +04:00
|
|
|
)),
|
|
|
|
array(array(
|
|
|
|
'fieldName' => 'decimal',
|
|
|
|
'phpType' => 'float',
|
|
|
|
'dbType' => 'decimal',
|
2012-04-01 21:59:21 +04:00
|
|
|
'value' => 33.33
|
|
|
|
),
|
|
|
|
));
|
2012-04-01 20:29:49 +04:00
|
|
|
}
|
2012-04-01 21:59:21 +04:00
|
|
|
|
2011-04-30 12:42:38 +04:00
|
|
|
public function getParseTokensInEntityFileData()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'<?php namespace Foo\Bar; class Baz {}',
|
|
|
|
array('Foo\Bar\Baz'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'<?php namespace Foo\Bar; use Foo; class Baz {}',
|
|
|
|
array('Foo\Bar\Baz'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'<?php namespace /*Comment*/ Foo\Bar; /** Foo */class /* Comment */ Baz {}',
|
|
|
|
array('Foo\Bar\Baz'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'
|
|
|
|
<?php namespace
|
|
|
|
/*Comment*/
|
|
|
|
Foo\Bar
|
|
|
|
;
|
|
|
|
|
|
|
|
/** Foo */
|
|
|
|
class
|
|
|
|
/* Comment */
|
|
|
|
Baz {}
|
|
|
|
',
|
|
|
|
array('Foo\Bar\Baz'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param \ReflectionProperty $property
|
|
|
|
*/
|
|
|
|
private function assertPhpDocVarType($type, \ReflectionProperty $property)
|
|
|
|
{
|
|
|
|
$this->assertEquals(1, preg_match('/@var\s+([^\s]+)/',$property->getDocComment(), $matches));
|
|
|
|
$this->assertEquals($type, $matches[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param \ReflectionProperty $method
|
|
|
|
*/
|
|
|
|
private function assertPhpDocReturnType($type, \ReflectionMethod $method)
|
|
|
|
{
|
|
|
|
$this->assertEquals(1, preg_match('/@return\s+([^\s]+)/', $method->getDocComment(), $matches));
|
|
|
|
$this->assertEquals($type, $matches[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param \ReflectionProperty $method
|
|
|
|
*/
|
|
|
|
private function assertPhpDocParamType($type, \ReflectionMethod $method)
|
|
|
|
{
|
|
|
|
$this->assertEquals(1, preg_match('/@param\s+([^\s]+)/', $method->getDocComment(), $matches));
|
|
|
|
$this->assertEquals($type, $matches[1]);
|
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class EntityGeneratorAuthor {}
|
2012-01-03 00:32:18 +04:00
|
|
|
class EntityGeneratorComment {}
|