diff --git a/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php new file mode 100644 index 000000000..b2a97e7f8 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php @@ -0,0 +1,79 @@ +. + */ + +namespace Doctrine\ORM\Mapping\Builder; + +use Doctrine\ORM\Mapping\ClassMetadata; + +/** + * Builder Object for ClassMetadata + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.com + * @since 2.0 + * @author Benjamin Eberlei + */ +class ClassMetadataBuilder +{ + /** + * @var ClassMetadata + */ + private $cm; + + public function __construct(ClassMetadata $cm) + { + $this->cm = $cm; + } + + public function setMappedSuperClass() + { + $this->cm->isMappedSuperclass = true; + } + + public function setCustomRepositoryClass($repositoryClassName) + { + $this->cm->setCustomRepositoryClass($repositoryClassName); + } + + public function setReadOnly() + { + $this->cm->markReadOnly(); + } + + public function setTable($name) + { + $this->cm->setPrimaryTable(array('name' => $name)); + } + + public function addIndex(array $columns, $name) + { + if (!isset($this->cm->table['indexes'])) { + $this->cm->table['indexes'] = array(); + } + $this->cm->table['indexes'][$name] = array('columns' => $columns); + } + + public function addUniqueConstraint(array $columns, $name) + { + if (!isset($this->cm->table['uniqueConstraints'])) { + $this->cm->table['uniqueConstraints'] = array(); + } + $this->cm->table['uniqueConstraints'][$name] = array('columns' => $columns); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php new file mode 100644 index 000000000..551f3bb78 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php @@ -0,0 +1,99 @@ +. + */ + + +namespace Doctrine\Tests\ORM\Mapping; + +use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; + +/** + * @group DDC-659 + */ +class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase +{ + /** + * @var ClassMetadata + */ + private $cm; + /** + * @var ClassMetadataBuilder + */ + private $builder; + + public function setUp() + { + $this->cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $this->builder = new ClassMetadataBuilder($this->cm); + } + + public function testSetMappedSuperClass() + { + $this->builder->setMappedSuperClass(); + + $this->assertTrue($this->cm->isMappedSuperclass); + } + + public function testSetCustomRepositoryClass() + { + $this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup'); + + $this->assertEquals('Doctrine\Tests\Models\CMS\CmsGroup', $this->cm->customRepositoryClassName); + } + + public function testSetReadOnly() + { + $this->builder->setReadOnly(); + $this->assertTrue($this->cm->isReadOnly); + } + + public function testSetTable() + { + $this->builder->setTable('users'); + $this->assertEquals('users', $this->cm->table['name']); + } + + public function testAddIndex() + { + $this->builder->addIndex(array('username', 'name'), 'users_idx'); + $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['indexes']); + } + + public function testAddUniqueConstraint() + { + $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'); + $this->assertEquals(array('users_idx' => array('columns' => array('username', 'name'))), $this->cm->table['uniqueConstraints']); + } + + public function testSetPrimaryTableRelated() + { + $this->builder->addUniqueConstraint(array('username', 'name'), 'users_idx'); + $this->builder->addIndex(array('username', 'name'), 'users_idx'); + $this->builder->setTable('users'); + + $this->assertEquals( + array( + 'name' => 'users', + 'indexes' => array('users_idx' => array('columns' => array('username', 'name'))), + 'uniqueConstraints' => array('users_idx' => array('columns' => array('username', 'name'))), + ), + $this->cm->table + ); + } +} \ No newline at end of file