2009-06-06 01:40:47 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Mapping;
|
|
|
|
|
2009-08-13 15:03:26 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata,
|
|
|
|
Doctrine\ORM\Mapping\Driver\XmlDriver,
|
|
|
|
Doctrine\ORM\Mapping\Driver\YamlDriver;
|
2009-06-06 01:40:47 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
|
2009-06-06 01:40:47 +04:00
|
|
|
{
|
2010-02-02 00:48:27 +03:00
|
|
|
abstract protected function _loadDriver();
|
|
|
|
|
|
|
|
public function testLoadMapping()
|
2009-06-06 01:40:47 +04:00
|
|
|
{
|
2009-08-13 15:03:26 +04:00
|
|
|
$className = 'Doctrine\Tests\ORM\Mapping\User';
|
2010-02-02 00:48:27 +03:00
|
|
|
$mappingDriver = $this->_loadDriver();
|
|
|
|
|
2009-06-06 01:40:47 +04:00
|
|
|
$class = new ClassMetadata($className);
|
2010-02-02 00:48:27 +03:00
|
|
|
$mappingDriver->loadMetadataForClass($className, $class);
|
|
|
|
|
|
|
|
return $class;
|
2009-08-13 15:03:26 +04:00
|
|
|
}
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testLoadMapping
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testEntityTableNameAndInheritance($class)
|
2009-08-13 15:03:26 +04:00
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertEquals('cms_users', $class->getTableName());
|
2010-03-15 20:19:00 +03:00
|
|
|
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->inheritanceType);
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testEntityTableNameAndInheritance
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testFieldMappings($class)
|
|
|
|
{
|
|
|
|
$this->assertEquals(3, count($class->fieldMappings));
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertTrue(isset($class->fieldMappings['id']));
|
|
|
|
$this->assertTrue(isset($class->fieldMappings['name']));
|
2010-02-02 00:48:27 +03:00
|
|
|
$this->assertTrue(isset($class->fieldMappings['email']));
|
|
|
|
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertEquals('string', $class->fieldMappings['name']['type']);
|
2010-01-28 02:00:55 +03:00
|
|
|
$this->assertTrue($class->fieldMappings['name']['nullable']);
|
|
|
|
$this->assertTrue($class->fieldMappings['name']['unique']);
|
2010-02-02 00:48:27 +03:00
|
|
|
$this->assertEquals("user_email", $class->fieldMappings['email']['columnName']);
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testFieldMappings
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testIdentifier($class)
|
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertEquals(array('id'), $class->identifier);
|
2010-03-15 20:19:00 +03:00
|
|
|
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $class->generatorType, "ID-Generator is not ClassMetadata::GENERATOR_TYPE_AUTO");
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testIdentifier
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testAssocations($class)
|
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertEquals(3, count($class->associationMappings));
|
|
|
|
$this->assertEquals(1, count($class->inverseMappings));
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAssocations
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testOwningOneToOneAssocation($class)
|
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertTrue($class->associationMappings['address'] instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
|
|
|
|
$this->assertTrue(isset($class->associationMappings['address']));
|
|
|
|
$this->assertTrue($class->associationMappings['address']->isOwningSide);
|
2010-01-22 18:10:13 +03:00
|
|
|
// Check cascading
|
|
|
|
$this->assertTrue($class->associationMappings['address']->isCascadeRemove);
|
|
|
|
$this->assertFalse($class->associationMappings['address']->isCascadePersist);
|
|
|
|
$this->assertFalse($class->associationMappings['address']->isCascadeRefresh);
|
|
|
|
$this->assertFalse($class->associationMappings['address']->isCascadeDetach);
|
|
|
|
$this->assertFalse($class->associationMappings['address']->isCascadeMerge);
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testOwningOneToOneAssocation
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testInverseOneToManyAssociation($class)
|
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertTrue($class->associationMappings['phonenumbers'] instanceof \Doctrine\ORM\Mapping\OneToManyMapping);
|
|
|
|
$this->assertTrue(isset($class->associationMappings['phonenumbers']));
|
|
|
|
$this->assertFalse($class->associationMappings['phonenumbers']->isOwningSide);
|
2009-07-25 20:33:29 +04:00
|
|
|
$this->assertTrue($class->associationMappings['phonenumbers']->isCascadePersist);
|
2010-01-30 23:24:38 +03:00
|
|
|
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeRemove);
|
|
|
|
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeRefresh);
|
|
|
|
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeDetach);
|
|
|
|
$this->assertFalse($class->associationMappings['phonenumbers']->isCascadeMerge);
|
2010-02-02 00:48:27 +03:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
// Test Order By
|
2010-02-16 01:50:35 +03:00
|
|
|
$this->assertEquals(array('number' => 'ASC'), $class->associationMappings['phonenumbers']->orderBy);
|
2010-02-14 22:38:22 +03:00
|
|
|
|
2010-02-02 00:48:27 +03:00
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInverseOneToManyAssociation
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testManyToManyAssociationWithCascadeAll($class)
|
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
|
|
|
|
$this->assertTrue(isset($class->associationMappings['groups']));
|
|
|
|
$this->assertTrue($class->associationMappings['groups']->isOwningSide);
|
2010-01-22 18:10:13 +03:00
|
|
|
// Make sure that cascade-all works as expected
|
|
|
|
$this->assertTrue($class->associationMappings['groups']->isCascadeRemove);
|
|
|
|
$this->assertTrue($class->associationMappings['groups']->isCascadePersist);
|
|
|
|
$this->assertTrue($class->associationMappings['groups']->isCascadeRefresh);
|
|
|
|
$this->assertTrue($class->associationMappings['groups']->isCascadeDetach);
|
|
|
|
$this->assertTrue($class->associationMappings['groups']->isCascadeMerge);
|
2010-01-28 02:00:55 +03:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
$this->assertNull($class->associationMappings['groups']->orderBy);
|
|
|
|
|
2010-02-02 00:48:27 +03:00
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testManyToManyAssociationWithCascadeAll
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testLifecycleCallbacks($class)
|
|
|
|
{
|
|
|
|
$this->assertEquals(count($class->lifecycleCallbacks), 2);
|
|
|
|
$this->assertEquals($class->lifecycleCallbacks['prePersist'][0], 'doStuffOnPrePersist');
|
|
|
|
$this->assertEquals($class->lifecycleCallbacks['postPersist'][0], 'doStuffOnPostPersist');
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-15 22:29:07 +03:00
|
|
|
* @depends testManyToManyAssociationWithCascadeAll
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
2010-03-17 01:41:09 +03:00
|
|
|
public function testLifecycleCallbacksSupportMultipleMethodNames($class)
|
|
|
|
{
|
2010-03-15 22:29:07 +03:00
|
|
|
$this->assertEquals(count($class->lifecycleCallbacks['prePersist']), 2);
|
|
|
|
$this->assertEquals($class->lifecycleCallbacks['prePersist'][1], 'doOtherStuffOnPrePersistToo');
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testLifecycleCallbacksSupportMultipleMethodNames
|
2010-02-02 00:48:27 +03:00
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testJoinColumnUniqueAndNullable($class)
|
|
|
|
{
|
2010-01-28 02:00:55 +03:00
|
|
|
// Non-Nullability of Join Column
|
|
|
|
$this->assertFalse($class->associationMappings['groups']->joinTable['joinColumns'][0]['nullable']);
|
|
|
|
$this->assertFalse($class->associationMappings['groups']->joinTable['joinColumns'][0]['unique']);
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testJoinColumnUniqueAndNullable
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testColumnDefinition($class)
|
|
|
|
{
|
|
|
|
$this->assertEquals("CHAR(32) NOT NULL", $class->fieldMappings['email']['columnDefinition']);
|
|
|
|
$this->assertEquals("INT NULL", $class->associationMappings['groups']->joinTable['inverseJoinColumns'][0]['columnDefinition']);
|
|
|
|
|
|
|
|
return $class;
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2010-03-17 17:20:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testColumnDefinition
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
*/
|
|
|
|
public function testJoinColumnOnDeleteAndOnUpdate($class)
|
|
|
|
{
|
|
|
|
$this->assertEquals('CASCADE', $class->associationMappings['address']->joinColumns[0]['onDelete']);
|
|
|
|
$this->assertEquals('CASCADE', $class->associationMappings['address']->joinColumns[0]['onUpdate']);
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
2009-08-13 15:03:26 +04:00
|
|
|
}
|
2009-06-06 01:40:47 +04:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
* @HasLifecycleCallbacks
|
|
|
|
* @Table(name="cms_users")
|
|
|
|
*/
|
|
|
|
class User
|
|
|
|
{
|
2010-03-18 00:25:19 +03:00
|
|
|
/** @Id @Column(type="integer") @generatedValue(strategy="AUTO") */
|
2010-02-14 22:38:22 +03:00
|
|
|
public $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(length=50, nullable=true, unique=true)
|
|
|
|
*/
|
|
|
|
public $name;
|
2010-02-02 00:48:27 +03:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
/**
|
|
|
|
* @Column(name="user_email", columnDefinition="CHAR(32) NOT NULL")
|
|
|
|
*/
|
|
|
|
public $email;
|
2009-08-25 01:05:55 +04:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
/**
|
|
|
|
* @OneToOne(targetEntity="Address", cascade={"remove"})
|
2010-03-17 17:20:18 +03:00
|
|
|
* @JoinColumn(onDelete="CASCADE", onUpdate="CASCADE")
|
2010-02-14 22:38:22 +03:00
|
|
|
*/
|
|
|
|
public $address;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @OneToMany(targetEntity="Phonenumber", mappedBy="user", cascade={"persist"})
|
2010-02-27 00:26:06 +03:00
|
|
|
* @OrderBy({"number"="ASC"})
|
2010-02-14 22:38:22 +03:00
|
|
|
*/
|
|
|
|
public $phonenumbers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ManyToMany(targetEntity="Group", cascade={"all"})
|
|
|
|
* @JoinTable(name="cms_user_groups",
|
|
|
|
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id", nullable=false, unique=false)},
|
|
|
|
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id", columnDefinition="INT NULL")}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public $groups;
|
|
|
|
|
2010-03-15 22:29:07 +03:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
/**
|
|
|
|
* @PrePersist
|
|
|
|
*/
|
2009-08-25 01:05:55 +04:00
|
|
|
public function doStuffOnPrePersist()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-15 22:29:07 +03:00
|
|
|
/**
|
|
|
|
* @PrePersist
|
|
|
|
*/
|
|
|
|
public function doOtherStuffOnPrePersistToo() {
|
|
|
|
}
|
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
/**
|
|
|
|
* @PostPersist
|
|
|
|
*/
|
2009-08-25 01:05:55 +04:00
|
|
|
public function doStuffOnPostPersist()
|
|
|
|
{
|
2010-02-02 00:48:27 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|