2009-01-06 20:22:23 +03:00
|
|
|
<?php
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests\ORM\Mapping;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Mocks\MetadataDriverMock;
|
|
|
|
use Doctrine\Tests\Mocks\DatabasePlatformMock;
|
2009-07-16 00:18:40 +04:00
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock;
|
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock;
|
|
|
|
use Doctrine\Tests\Mocks\DriverMock;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2009-07-16 00:18:40 +04:00
|
|
|
use Doctrine\Common\EventManager;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-24 19:56:44 +03:00
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
2009-01-06 20:22:23 +03:00
|
|
|
|
2009-02-18 10:59:11 +03:00
|
|
|
class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
|
|
|
|
{
|
|
|
|
public function testGetMetadataForSingleClass()
|
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$mockDriver = new MetadataDriverMock();
|
2009-11-04 22:59:34 +03:00
|
|
|
$entityManager = $this->_createEntityManager($mockDriver);
|
2009-07-16 00:18:40 +04:00
|
|
|
|
2009-11-04 22:59:34 +03:00
|
|
|
$conn = $entityManager->getConnection();
|
2009-07-16 00:18:40 +04:00
|
|
|
$mockPlatform = $conn->getDatabasePlatform();
|
2009-03-30 23:43:05 +04:00
|
|
|
$mockPlatform->setPrefersSequences(true);
|
|
|
|
$mockPlatform->setPrefersIdentityColumns(false);
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
// Self-made metadata
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
|
2009-01-06 21:30:51 +03:00
|
|
|
// Add a mapped field
|
2009-01-08 14:23:24 +03:00
|
|
|
$cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar'));
|
2009-03-30 23:43:05 +04:00
|
|
|
// Add a mapped field
|
|
|
|
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
|
2009-01-06 21:30:51 +03:00
|
|
|
// and a mapped association
|
|
|
|
$cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this'));
|
2009-07-20 16:05:19 +04:00
|
|
|
// and an association on the owning side
|
|
|
|
$joinColumns = array(
|
|
|
|
array('name' => 'other_id', 'referencedColumnName' => 'id')
|
|
|
|
);
|
|
|
|
$cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'Other', 'joinColumns' => $joinColumns));
|
2009-01-06 21:30:51 +03:00
|
|
|
// and an id generator type
|
2009-06-07 21:20:37 +04:00
|
|
|
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
// SUT
|
2009-07-16 00:18:40 +04:00
|
|
|
$cmf = new ClassMetadataFactoryTestSubject($entityManager);
|
2009-01-22 22:38:10 +03:00
|
|
|
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
// Prechecks
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertEquals(array(), $cm1->parentClasses);
|
2009-06-07 21:20:37 +04:00
|
|
|
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm1->inheritanceType);
|
2009-01-06 21:30:51 +03:00
|
|
|
$this->assertTrue($cm1->hasField('name'));
|
2009-07-20 16:05:19 +04:00
|
|
|
$this->assertEquals(2, count($cm1->associationMappings));
|
2009-06-07 21:20:37 +04:00
|
|
|
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType);
|
2009-01-06 21:30:51 +03:00
|
|
|
|
|
|
|
// Go
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm1 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1');
|
2009-01-06 21:30:51 +03:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertEquals(array(), $cm1->parentClasses);
|
2009-01-06 21:30:51 +03:00
|
|
|
$this->assertTrue($cm1->hasField('name'));
|
2009-06-07 21:20:37 +04:00
|
|
|
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType);
|
2009-01-06 20:22:23 +03:00
|
|
|
}
|
2009-11-04 22:59:34 +03:00
|
|
|
|
2009-11-05 02:06:38 +03:00
|
|
|
public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
|
2009-11-04 22:59:34 +03:00
|
|
|
{
|
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
|
|
|
|
|
|
|
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
|
|
|
|
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
|
|
|
|
$metadataDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
|
|
|
|
$metadataDriver->setClassDirectory(__DIR__."/../../Models/Global/");
|
|
|
|
|
|
|
|
$entityManager = $this->_createEntityManager($metadataDriver);
|
|
|
|
|
|
|
|
$mf = $entityManager->getMetadataFactory();
|
|
|
|
$m1 = $mf->getMetadataFor("DoctrineGlobal_Article");
|
2009-11-05 02:06:38 +03:00
|
|
|
$h1 = $mf->hasMetadataFor("DoctrineGlobal_Article");
|
2009-11-04 22:59:34 +03:00
|
|
|
$h2 = $mf->hasMetadataFor("\DoctrineGlobal_Article");
|
|
|
|
$m2 = $mf->getMetadataFor("\DoctrineGlobal_Article");
|
|
|
|
|
2009-11-05 02:06:38 +03:00
|
|
|
$this->assertNotSame($m1, $m2);
|
|
|
|
$this->assertFalse($h2);
|
|
|
|
$this->assertTrue($h1);
|
2009-11-04 22:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _createEntityManager($metadataDriver)
|
|
|
|
{
|
|
|
|
$driverMock = new DriverMock();
|
|
|
|
$config = new \Doctrine\ORM\Configuration();
|
|
|
|
$config->setProxyDir(__DIR__ . '/../../Proxies');
|
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies');
|
|
|
|
$eventManager = new EventManager();
|
|
|
|
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
|
|
|
|
$mockDriver = new MetadataDriverMock();
|
|
|
|
$config->setMetadataDriverImpl($metadataDriver);
|
|
|
|
|
|
|
|
return EntityManagerMock::create($conn, $config, $eventManager);
|
|
|
|
}
|
2009-01-06 20:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test subject class with overriden factory method for mocking purposes */
|
2009-02-18 10:59:11 +03:00
|
|
|
class ClassMetadataFactoryTestSubject extends \Doctrine\ORM\Mapping\ClassMetadataFactory
|
|
|
|
{
|
2009-01-06 20:22:23 +03:00
|
|
|
private $_mockMetadata = array();
|
|
|
|
private $_requestedClasses = array();
|
2009-02-18 10:59:11 +03:00
|
|
|
|
2009-01-06 20:22:23 +03:00
|
|
|
/** @override */
|
2009-02-18 10:59:11 +03:00
|
|
|
protected function _newClassMetadataInstance($className)
|
|
|
|
{
|
2009-01-06 20:22:23 +03:00
|
|
|
$this->_requestedClasses[] = $className;
|
|
|
|
if ( ! isset($this->_mockMetadata[$className])) {
|
|
|
|
throw new InvalidArgumentException("No mock metadata found for class $className.");
|
|
|
|
}
|
|
|
|
return $this->_mockMetadata[$className];
|
|
|
|
}
|
2009-02-18 10:59:11 +03:00
|
|
|
|
|
|
|
public function setMetadataForClass($className, $metadata)
|
|
|
|
{
|
2009-01-06 20:22:23 +03:00
|
|
|
$this->_mockMetadata[$className] = $metadata;
|
|
|
|
}
|
2009-02-18 10:59:11 +03:00
|
|
|
|
|
|
|
public function getRequestedClasses()
|
|
|
|
{
|
|
|
|
return $this->_requestedClasses;
|
|
|
|
}
|
2009-01-06 20:22:23 +03:00
|
|
|
}
|
2009-06-06 01:40:47 +04:00
|
|
|
|
|
|
|
class TestEntity1
|
|
|
|
{
|
|
|
|
private $id;
|
|
|
|
private $name;
|
|
|
|
private $other;
|
2009-07-20 16:05:19 +04:00
|
|
|
private $association;
|
|
|
|
}
|