123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- /*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
- 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->cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
- $this->builder = new ClassMetadataBuilder($this->cm);
- }
- public function testSetMappedSuperClass()
- {
- $this->assertIsFluent($this->builder->setMappedSuperClass());
- $this->assertTrue($this->cm->isMappedSuperclass);
- }
- public function testSetCustomRepositoryClass()
- {
- $this->assertIsFluent($this->builder->setCustomRepositoryClass('Doctrine\Tests\Models\CMS\CmsGroup'));
- $this->assertEquals('Doctrine\Tests\Models\CMS\CmsGroup', $this->cm->customRepositoryClassName);
- }
- public function testSetReadOnly()
- {
- $this->assertIsFluent($this->builder->setReadOnly());
- $this->assertTrue($this->cm->isReadOnly);
- }
- public function testSetTable()
- {
- $this->assertIsFluent($this->builder->setTable('users'));
- $this->assertEquals('users', $this->cm->table['name']);
- }
- public function testAddIndex()
- {
- $this->assertIsFluent($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->assertIsFluent($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
- );
- }
- public function testSetInheritanceJoined()
- {
- $this->assertIsFluent($this->builder->setJoinedTableInheritance());
- $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_JOINED, $this->cm->inheritanceType);
- }
- public function testSetInheritanceSingleTable()
- {
- $this->assertIsFluent($this->builder->setSingleTableInheritance());
- $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE, $this->cm->inheritanceType);
- }
- public function testSetDiscriminatorColumn()
- {
- $this->assertIsFluent($this->builder->setDiscriminatorColumn('discr', 'string', '124'));
- $this->assertEquals(array('fieldName' => 'discr', 'name' => 'discr', 'type' => 'string', 'length' => '124'), $this->cm->discriminatorColumn);
- }
- public function testAddDiscriminatorMapClass()
- {
- $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test', 'Doctrine\Tests\Models\CMS\CmsUser'));
- $this->assertIsFluent($this->builder->addDiscriminatorMapClass('test2', 'Doctrine\Tests\Models\CMS\CmsGroup'));
- $this->assertEquals(array('test' => 'Doctrine\Tests\Models\CMS\CmsUser', 'test2' => 'Doctrine\Tests\Models\CMS\CmsGroup'), $this->cm->discriminatorMap);
- $this->assertEquals('test', $this->cm->discriminatorValue);
- }
- public function testChangeTrackingPolicyExplicit()
- {
- $this->assertIsFluent($this->builder->setChangeTrackingPolicyDeferredExplicit());
- $this->assertEquals(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT, $this->cm->changeTrackingPolicy);
- }
- public function testChangeTrackingPolicyNotify()
- {
- $this->assertIsFluent($this->builder->setChangeTrackingPolicyNotify());
- $this->assertEquals(ClassMetadata::CHANGETRACKING_NOTIFY, $this->cm->changeTrackingPolicy);
- }
- public function testAddField()
- {
- $this->assertIsFluent($this->builder->addField('name', 'string'));
- $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']);
- }
- public function testCreateField()
- {
- $fieldBuilder = ($this->builder->createField('name', 'string'));
- $this->assertInstanceOf('Doctrine\ORM\Mapping\Builder\FieldBuilder', $fieldBuilder);
- $this->assertFalse(isset($this->cm->fieldMappings['name']));
- $this->assertIsFluent($fieldBuilder->build());
- $this->assertEquals(array('columnName' => 'name', 'fieldName' => 'name', 'type' => 'string'), $this->cm->fieldMappings['name']);
- }
- public function testCreateVersionedField()
- {
- $this->builder->createField('name', 'integer')->columnName('username')->length(124)->nullable()->columnDefinition('foobar')->unique()->isVersionField()->build();
- $this->assertEquals(array(
- 'columnDefinition' => 'foobar',
- 'columnName' => 'username',
- 'default' => 1,
- 'fieldName' => 'name',
- 'length' => 124,
- 'type' => 'integer',
- 'nullable' => true,
- 'unique' => true,
- ), $this->cm->fieldMappings['name']);
- }
- public function testCreatePrimaryField()
- {
- $this->builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
- $this->assertEquals(array('id'), $this->cm->identifier);
- $this->assertEquals(array('columnName' => 'id', 'fieldName' => 'id', 'id' => true, 'type' => 'integer'), $this->cm->fieldMappings['id']);
- }
- public function testAddLifecycleEvent()
- {
- $this->builder->addLifecycleEvent('getStatus', 'postLoad');
- $this->assertEquals(array('postLoad' => array('getStatus')), $this->cm->lifecycleCallbacks);
- }
- public function testCreateManyToOne()
- {
- $this->assertIsFluent(
- $this->builder->createManyToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
- ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
- ->cascadeAll()
- ->fetchExtraLazy()
- ->build()
- );
- $this->assertEquals(array('groups' => array (
- 'fieldName' => 'groups',
- 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
- 'cascade' => array (
- 0 => 'remove',
- 1 => 'persist',
- 2 => 'refresh',
- 3 => 'merge',
- 4 => 'detach',
- ),
- 'fetch' => 4,
- 'joinColumns' => array (
- 0 =>
- array (
- 'name' => 'group_id',
- 'referencedColumnName' => 'id',
- 'nullable' => true,
- 'unique' => false,
- 'onDelete' => 'CASCADE',
- 'columnDefinition' => NULL,
- ),
- ),
- 'type' => 2,
- 'mappedBy' => NULL,
- 'inversedBy' => NULL,
- 'isOwningSide' => true,
- 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
- 'isCascadeRemove' => true,
- 'isCascadePersist' => true,
- 'isCascadeRefresh' => true,
- 'isCascadeMerge' => true,
- 'isCascadeDetach' => true,
- 'sourceToTargetKeyColumns' =>
- array (
- 'group_id' => 'id',
- ),
- 'joinColumnFieldNames' =>
- array (
- 'group_id' => 'group_id',
- ),
- 'targetToSourceKeyColumns' =>
- array (
- 'id' => 'group_id',
- ),
- 'orphanRemoval' => false,
- ),
- ), $this->cm->associationMappings);
- }
- public function testCreateOneToOne()
- {
- $this->assertIsFluent(
- $this->builder->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
- ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
- ->cascadeAll()
- ->fetchExtraLazy()
- ->build()
- );
- $this->assertEquals(array('groups' => array (
- 'fieldName' => 'groups',
- 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
- 'cascade' => array (
- 0 => 'remove',
- 1 => 'persist',
- 2 => 'refresh',
- 3 => 'merge',
- 4 => 'detach',
- ),
- 'fetch' => 4,
- 'joinColumns' => array (
- 0 =>
- array (
- 'name' => 'group_id',
- 'referencedColumnName' => 'id',
- 'nullable' => true,
- 'unique' => true,
- 'onDelete' => 'CASCADE',
- 'columnDefinition' => NULL,
- ),
- ),
- 'type' => 1,
- 'mappedBy' => NULL,
- 'inversedBy' => NULL,
- 'isOwningSide' => true,
- 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
- 'isCascadeRemove' => true,
- 'isCascadePersist' => true,
- 'isCascadeRefresh' => true,
- 'isCascadeMerge' => true,
- 'isCascadeDetach' => true,
- 'sourceToTargetKeyColumns' =>
- array (
- 'group_id' => 'id',
- ),
- 'joinColumnFieldNames' =>
- array (
- 'group_id' => 'group_id',
- ),
- 'targetToSourceKeyColumns' =>
- array (
- 'id' => 'group_id',
- ),
- 'orphanRemoval' => false,
- ),
- ), $this->cm->associationMappings);
- }
- public function testCreateManyToMany()
- {
- $this->assertIsFluent(
- $this->builder->createManyToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
- ->setJoinTable('groups_users')
- ->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
- ->addInverseJoinColumn('user_id', 'id')
- ->cascadeAll()
- ->fetchExtraLazy()
- ->build()
- );
- $this->assertEquals(array(
- 'groups' =>
- array(
- 'fieldName' => 'groups',
- 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
- 'cascade' =>
- array(
- 0 => 'remove',
- 1 => 'persist',
- 2 => 'refresh',
- 3 => 'merge',
- 4 => 'detach',
- ),
- 'fetch' => 4,
- 'joinTable' =>
- array(
- 'joinColumns' =>
- array(
- 0 =>
- array(
- 'name' => 'group_id',
- 'referencedColumnName' => 'id',
- 'nullable' => true,
- 'unique' => false,
- 'onDelete' => 'CASCADE',
- 'columnDefinition' => NULL,
- ),
- ),
- 'inverseJoinColumns' =>
- array(
- 0 =>
- array(
- 'name' => 'user_id',
- 'referencedColumnName' => 'id',
- 'nullable' => true,
- 'unique' => false,
- 'onDelete' => NULL,
- 'columnDefinition' => NULL,
- ),
- ),
- 'name' => 'groups_users',
- ),
- 'type' => 8,
- 'mappedBy' => NULL,
- 'inversedBy' => NULL,
- 'isOwningSide' => true,
- 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
- 'isCascadeRemove' => true,
- 'isCascadePersist' => true,
- 'isCascadeRefresh' => true,
- 'isCascadeMerge' => true,
- 'isCascadeDetach' => true,
- 'isOnDeleteCascade' => true,
- 'relationToSourceKeyColumns' =>
- array(
- 'group_id' => 'id',
- ),
- 'joinTableColumns' =>
- array(
- 0 => 'group_id',
- 1 => 'user_id',
- ),
- 'relationToTargetKeyColumns' =>
- array(
- 'user_id' => 'id',
- ),
- 'orphanRemoval' => false,
- ),
- ), $this->cm->associationMappings);
- }
- public function testCreateOneToMany()
- {
- $this->assertIsFluent(
- $this->builder->createOneToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
- ->mappedBy('test')
- ->setOrderBy(array('test'))
- ->setIndexBy('test')
- ->build()
- );
- $this->assertEquals(array(
- 'groups' =>
- array(
- 'fieldName' => 'groups',
- 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
- 'mappedBy' => 'test',
- 'orderBy' =>
- array(
- 0 => 'test',
- ),
- 'indexBy' => 'test',
- 'type' => 4,
- 'inversedBy' => NULL,
- 'isOwningSide' => false,
- 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
- 'fetch' => 2,
- 'cascade' =>
- array(
- ),
- 'isCascadeRemove' => false,
- 'isCascadePersist' => false,
- 'isCascadeRefresh' => false,
- 'isCascadeMerge' => false,
- 'isCascadeDetach' => false,
- 'orphanRemoval' => false,
- ),
- ), $this->cm->associationMappings);
- }
- public function assertIsFluent($ret)
- {
- $this->assertSame($this->builder, $ret, "Return Value has to be same instance as used builder");
- }
- }
|