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;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->_generator = new EntityGenerator();
|
|
|
|
$this->_generator->setGenerateAnnotations(true);
|
|
|
|
$this->_generator->setGenerateStubMethods(true);
|
|
|
|
$this->_generator->setRegenerateEntityIfExists(false);
|
|
|
|
$this->_generator->setUpdateEntityIfExists(true);
|
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
public function testWriteEntityClass()
|
|
|
|
{
|
|
|
|
$metadata = new ClassMetadataInfo('EntityGeneratorBook');
|
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
|
|
|
),
|
|
|
|
));
|
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
|
|
|
|
|
2010-03-19 01:35:16 +03:00
|
|
|
$this->_generator->writeEntityClass($metadata, __DIR__);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
$path = __DIR__ . '/EntityGeneratorBook.php';
|
|
|
|
$this->assertTrue(file_exists($path));
|
|
|
|
require_once $path;
|
2010-03-19 01:35:16 +03:00
|
|
|
|
|
|
|
return $metadata;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWriteEntityClass
|
2010-03-19 01:35:16 +03:00
|
|
|
* @param ClassMetadata $metadata
|
2010-03-19 00:38:42 +03:00
|
|
|
*/
|
2010-03-20 01:38:45 +03:00
|
|
|
public function testGeneratedEntityClass($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'getId'));
|
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'setName'));
|
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'getName'));
|
2010-03-20 01:38:45 +03:00
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'setAuthor'));
|
2010-03-19 00:38:42 +03:00
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'getAuthor'));
|
2010-03-20 01:38:45 +03:00
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'getComments'));
|
|
|
|
$this->assertTrue(method_exists('\EntityGeneratorBook', 'addComments'));
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
$book = new \EntityGeneratorBook();
|
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);
|
|
|
|
$this->assertEquals(array($comment), $book->getComments());
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-19 01:35:16 +03:00
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-20 01:38:45 +03:00
|
|
|
* @depends testGeneratedEntityClass
|
2010-03-19 01:35:16 +03:00
|
|
|
* @param ClassMetadata $metadata
|
|
|
|
*/
|
|
|
|
public function testEntityUpdatingWorks($metadata)
|
|
|
|
{
|
2010-03-20 01:38:45 +03:00
|
|
|
$metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
|
2010-03-19 01:35:16 +03:00
|
|
|
$this->_generator->writeEntityClass($metadata, __DIR__);
|
|
|
|
|
|
|
|
$code = file_get_contents(__DIR__ . '/EntityGeneratorBook.php');
|
2010-03-20 01:38:45 +03:00
|
|
|
|
2010-03-19 01:35:16 +03:00
|
|
|
$this->assertTrue(strstr($code, 'private $test;') !== false);
|
|
|
|
$this->assertTrue(strstr($code, 'private $test;') !== false);
|
|
|
|
$this->assertTrue(strstr($code, 'public function getTest(') !== false);
|
|
|
|
$this->assertTrue(strstr($code, 'public function setTest(') !== false);
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
unlink(__DIR__ . '/EntityGeneratorBook.php');
|
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class EntityGeneratorAuthor {}
|
|
|
|
class EntityGeneratorComment {}
|