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
|
|
|
|
{
|
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();
|
|
|
|
$this->_generator->setGenerateAnnotations(true);
|
|
|
|
$this->_generator->setGenerateStubMethods(true);
|
|
|
|
$this->_generator->setRegenerateEntityIfExists(false);
|
|
|
|
$this->_generator->setUpdateEntityIfExists(true);
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
$metadata->table['name'] = 'book';
|
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',
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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.");
|
|
|
|
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addComments'), "EntityGeneratorBook::addComments() missing.");
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$this->assertEquals('published', $book->getStatus());
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
$book->setName('Jonathan H. Wage');
|
|
|
|
$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();
|
|
|
|
$book->addComments($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());
|
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-02-03 01:21:42 +03:00
|
|
|
|
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
|
2010-03-19 01:35:16 +03:00
|
|
|
|
2011-02-03 01:21:42 +03: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.");
|
|
|
|
$this->assertTrue($reflClass->getProperty('test')->isPrivate(), "Check for private property test failed.");
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
|
|
|
|
$reader->setDefaultAnnotationNamespace("Doctrine\\ORM\\Mapping\\");
|
|
|
|
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoadPrefixedMetadata()
|
|
|
|
{
|
|
|
|
$this->_generator->setAnnotationPrefix('orm:');
|
|
|
|
$metadata = $this->generateBookEntityFixture();
|
|
|
|
|
|
|
|
|
|
|
|
$book = $this->newInstance($metadata);
|
|
|
|
|
|
|
|
$cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
|
|
|
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
|
|
|
|
$reader->setAnnotationNamespaceAlias("Doctrine\\ORM\\Mapping\\", "orm");
|
|
|
|
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
|
|
|
|
$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
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class EntityGeneratorAuthor {}
|
|
|
|
class EntityGeneratorComment {}
|