public function setUp() { $annotationDriver = $this->createAnnotationDriver(); $this->em = $this->_getTestEntityManager(); $this->em->getConfiguration()->setMetadataDriverImpl($annotationDriver); $this->factory = new ClassMetadataFactory(); $this->factory->setEntityManager($this->em); $this->listener = new ResolveTargetEntityListener(); }
public function setUp() { $this->listener = new AttachEntityListenersListener(); $driver = $this->createAnnotationDriver(); $this->em = $this->_getTestEntityManager(); $evm = $this->em->getEventManager(); $this->factory = new ClassMetadataFactory(); $evm->addEventListener(Events::loadClassMetadata, $this->listener); $this->em->getConfiguration()->setMetadataDriverImpl($driver); $this->factory->setEntityManager($this->em); }
public function testGetMetadataForSingleClass() { $mockDriver = new MetadataDriverMock(); $entityManager = $this->_createEntityManager($mockDriver); $conn = $entityManager->getConnection(); $mockPlatform = $conn->getDatabasePlatform(); $mockPlatform->setPrefersSequences(true); $mockPlatform->setPrefersIdentityColumns(false); $cm1 = $this->_createValidClassMetadata(); // SUT $cmf = new \Doctrine\ORM\Mapping\ClassMetadataFactory(); $cmf->setEntityManager($entityManager); $cmf->setMetadataFor($cm1->name, $cm1); // Prechecks $this->assertEquals(array(), $cm1->parentClasses); $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm1->inheritanceType); $this->assertTrue($cm1->hasField('name')); $this->assertEquals(2, count($cm1->associationMappings)); $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType); $this->assertEquals('group', $cm1->table['name']); // Go $cmMap1 = $cmf->getMetadataFor($cm1->name); $this->assertSame($cm1, $cmMap1); $this->assertEquals('group', $cmMap1->table['name']); $this->assertTrue($cmMap1->table['quoted']); $this->assertEquals(array(), $cmMap1->parentClasses); $this->assertTrue($cmMap1->hasField('name')); }
/** * @group DDC-3293 * @group DDC-3477 * @group 1238 */ public function testEmbeddedMappingsWithFalseUseColumnPrefix() { $factory = new ClassMetadataFactory(); $em = $this->_getTestEntityManager(); $em->getConfiguration()->setMetadataDriverImpl($this->_loadDriver()); $factory->setEntityManager($em); $this->assertFalse($factory->getMetadataFor('Doctrine\\Tests\\Models\\DDC3293\\DDC3293User')->embeddedClasses['address']['columnPrefix']); }
/** * Creates a new EntityManager that operates on the given database connection * and uses the given Configuration and EventManager implementations. * * @param \Doctrine\DBAL\Connection $conn * @param \Doctrine\ORM\Configuration $config * @param \Doctrine\Common\EventManager $eventManager */ protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) { $this->conn = $conn; $this->config = $config; $this->eventManager = $eventManager; $metadataFactoryClassName = $config->getClassMetadataFactoryName(); $this->metadataFactory = new $metadataFactoryClassName(); $this->metadataFactory->setEntityManager($this); $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); $this->unitOfWork = new UnitOfWork($this); $this->proxyFactory = new ProxyFactory($this, $config->getProxyDir(), $config->getProxyNamespace(), $config->getAutoGenerateProxyClasses()); }
/** * Creates a new EntityManager that operates on the given database connection * and uses the given Configuration and EventManager implementations. * * @param \Doctrine\DBAL\Connection $conn * @param \Doctrine\ORM\Configuration $config * @param \Doctrine\Common\EventManager $eventManager */ protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) { $this->conn = $conn; $this->config = $config; $this->eventManager = $eventManager; $metadataFactoryClassName = $config->getClassMetadataFactoryName(); $this->metadataFactory = new $metadataFactoryClassName(); $this->metadataFactory->setEntityManager($this); $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); $this->repositoryFactory = $config->getRepositoryFactory(); $this->unitOfWork = new UnitOfWork($this); $this->proxyFactory = new ProxyFactory($this, $config->getProxyDir(), $config->getProxyNamespace(), $config->getAutoGenerateProxyClasses()); if ($config->isSecondLevelCacheEnabled()) { $cacheClass = $config->getSecondLevelCacheConfiguration()->getCacheClassName(); $this->cache = new $cacheClass($this); } }
/** * @param mixed $classes * @return void */ public function setUp() { $this->doctrine = Zend_Registry::get('container')->getService('doctrine'); $this->em = $this->doctrine->getEntityManager(); $this->em->clear(); $tool = new SchemaTool($this->em); $tool->dropDatabase(); $classes = func_get_args(); if (!empty($classes)) { $metadataFactory = new ClassMetadataFactory(); $metadataFactory->setEntityManager($this->em); $metadataFactory->setCacheDriver(new ArrayCache()); $metadata = array(); foreach ((array) $classes as $class) { $metadata[] = $metadataFactory->getMetadataFor($class); } $tool->createSchema($metadata); } }
public function testGetMetadataForSingleClass() { $mockDriver = new MetadataDriverMock(); $entityManager = $this->_createEntityManager($mockDriver); $conn = $entityManager->getConnection(); $mockPlatform = $conn->getDatabasePlatform(); $mockPlatform->setPrefersSequences(true); $mockPlatform->setPrefersIdentityColumns(false); // Self-made metadata $cm1 = new ClassMetadata('Doctrine\\Tests\\ORM\\Mapping\\TestEntity1'); $cm1->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService()); $cm1->setPrimaryTable(array('name' => '`group`')); // Add a mapped field $cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar')); // Add a mapped field $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); // and a mapped association $cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'TestEntity1', 'mappedBy' => 'this')); // and an association on the owning side $joinColumns = array(array('name' => 'other_id', 'referencedColumnName' => 'id')); $cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'TestEntity1', 'joinColumns' => $joinColumns)); // and an id generator type $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); // SUT $cmf = new \Doctrine\ORM\Mapping\ClassMetadataFactory(); $cmf->setEntityManager($entityManager); $cmf->setMetadataFor('Doctrine\\Tests\\ORM\\Mapping\\TestEntity1', $cm1); // Prechecks $this->assertEquals(array(), $cm1->parentClasses); $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm1->inheritanceType); $this->assertTrue($cm1->hasField('name')); $this->assertEquals(2, count($cm1->associationMappings)); $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType); $this->assertEquals('group', $cm1->table['name']); // Go $cmMap1 = $cmf->getMetadataFor('Doctrine\\Tests\\ORM\\Mapping\\TestEntity1'); $this->assertSame($cm1, $cmMap1); $this->assertEquals('group', $cmMap1->table['name']); $this->assertTrue($cmMap1->table['quoted']); $this->assertEquals(array(), $cmMap1->parentClasses); $this->assertTrue($cmMap1->hasField('name')); }
/** * Set up entity manager * * @return Doctrine\ORM\EntityManager */ protected function setUpOrm() { global $application; $doctrine = $application->getBootstrap()->getResource('container')->getService('doctrine'); $orm = $doctrine->getEntityManager(); $orm->clear(); $tool = new SchemaTool($orm); $tool->dropDatabase(); $classes = func_get_args(); if (!empty($classes)) { $metadataFactory = new ClassMetadataFactory(); $metadataFactory->setEntityManager($orm); $metadataFactory->setCacheDriver(new Cache()); $metadata = array(); foreach ((array) $classes as $class) { $metadata[] = $metadataFactory->getMetadataFor($class); } $tool->createSchema($metadata); } return $orm; }
/** * @param EntityManager $em */ public function setEntityManager(EntityManager $em) { $this->em = $em; $this->config = $em->getConfiguration(); parent::setEntityManager($em); }
/** * {@inheritDoc} */ protected function setUp() { $this->cmf = new ClassMetadataFactory(); $this->cmf->setEntityManager($this->_getTestEntityManager()); }
/** * {@inheritdoc} */ public function setEntityManager(EntityManagerInterface $em) { parent::setEntityManager($em); $this->entityManager = $em; }
public function testAddDefaultDiscriminatorMap() { $cmf = new ClassMetadataFactory(); $driver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/JoinedInheritanceType/')); $em = $this->_createEntityManager($driver); $cmf->setEntityManager($em); $rootMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\JoinedInheritanceType\\RootClass'); $childMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\JoinedInheritanceType\\ChildClass'); $anotherChildMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\JoinedInheritanceType\\AnotherChildClass'); $rootDiscriminatorMap = $rootMetadata->discriminatorMap; $childDiscriminatorMap = $childMetadata->discriminatorMap; $anotherChildDiscriminatorMap = $anotherChildMetadata->discriminatorMap; $rootClass = 'Doctrine\\Tests\\Models\\JoinedInheritanceType\\RootClass'; $childClass = 'Doctrine\\Tests\\Models\\JoinedInheritanceType\\ChildClass'; $anotherChildClass = 'Doctrine\\Tests\\Models\\JoinedInheritanceType\\AnotherChildClass'; $rootClassKey = array_search($rootClass, $rootDiscriminatorMap); $childClassKey = array_search($childClass, $rootDiscriminatorMap); $anotherChildClassKey = array_search($anotherChildClass, $rootDiscriminatorMap); $this->assertEquals('rootclass', $rootClassKey); $this->assertEquals('childclass', $childClassKey); $this->assertEquals('anotherchildclass', $anotherChildClassKey); $this->assertEquals($childDiscriminatorMap, $rootDiscriminatorMap); $this->assertEquals($anotherChildDiscriminatorMap, $rootDiscriminatorMap); // ClassMetadataFactory::addDefaultDiscriminatorMap shouldn't be called again, because the // discriminator map is already cached $cmf = $this->getMock('Doctrine\\ORM\\Mapping\\ClassMetadataFactory', array('addDefaultDiscriminatorMap')); $cmf->setEntityManager($em); $cmf->expects($this->never())->method('addDefaultDiscriminatorMap'); $rootMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\JoinedInheritanceType\\RootClass'); }
/** * @group DDC-1845 */ public function testQuoteMetadata() { $cmf = new ClassMetadataFactory(); $driver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/Quote/')); $em = $this->_createEntityManager($driver); $cmf->setEntityManager($em); $userMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\Quote\\User'); $phoneMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\Quote\\Phone'); $groupMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\Quote\\Group'); $addressMetadata = $cmf->getMetadataFor('Doctrine\\Tests\\Models\\Quote\\Address'); // Phone Class Metadata $this->assertTrue($phoneMetadata->fieldMappings['number']['quoted']); $this->assertEquals('phone-number', $phoneMetadata->fieldMappings['number']['columnName']); $user = $phoneMetadata->associationMappings['user']; $this->assertTrue($user['joinColumns'][0]['quoted']); $this->assertEquals('user-id', $user['joinColumns'][0]['name']); $this->assertEquals('user-id', $user['joinColumns'][0]['referencedColumnName']); // User Group Metadata $this->assertTrue($groupMetadata->fieldMappings['id']['quoted']); $this->assertTrue($groupMetadata->fieldMappings['name']['quoted']); $this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']); $this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']); $user = $groupMetadata->associationMappings['parent']; $this->assertTrue($user['joinColumns'][0]['quoted']); $this->assertEquals('parent-id', $user['joinColumns'][0]['name']); $this->assertEquals('group-id', $user['joinColumns'][0]['referencedColumnName']); // Address Class Metadata $this->assertTrue($addressMetadata->fieldMappings['id']['quoted']); $this->assertTrue($addressMetadata->fieldMappings['zip']['quoted']); $this->assertEquals('address-id', $addressMetadata->fieldMappings['id']['columnName']); $this->assertEquals('address-zip', $addressMetadata->fieldMappings['zip']['columnName']); $user = $addressMetadata->associationMappings['user']; $this->assertTrue($user['joinColumns'][0]['quoted']); $this->assertEquals('user-id', $user['joinColumns'][0]['name']); $this->assertEquals('user-id', $user['joinColumns'][0]['referencedColumnName']); // User Class Metadata $this->assertTrue($userMetadata->fieldMappings['id']['quoted']); $this->assertTrue($userMetadata->fieldMappings['name']['quoted']); $this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']); $this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']); $address = $userMetadata->associationMappings['address']; $this->assertTrue($address['joinColumns'][0]['quoted']); $this->assertEquals('address-id', $address['joinColumns'][0]['name']); $this->assertEquals('address-id', $address['joinColumns'][0]['referencedColumnName']); $groups = $userMetadata->associationMappings['groups']; $this->assertTrue($groups['joinTable']['quoted']); $this->assertTrue($groups['joinTable']['joinColumns'][0]['quoted']); $this->assertEquals('quote-users-groups', $groups['joinTable']['name']); $this->assertEquals('user-id', $groups['joinTable']['joinColumns'][0]['name']); $this->assertEquals('user-id', $groups['joinTable']['joinColumns'][0]['referencedColumnName']); $this->assertTrue($groups['joinTable']['inverseJoinColumns'][0]['quoted']); $this->assertEquals('group-id', $groups['joinTable']['inverseJoinColumns'][0]['name']); $this->assertEquals('group-id', $groups['joinTable']['inverseJoinColumns'][0]['referencedColumnName']); }
/** * @param string $className * @param string $newClassName * @return string */ private function writeEntityClass($className, $newClassName) { $cmf = new ClassMetadataFactory(); $em = $this->_getTestEntityManager(); $cmf->setEntityManager($em); $metadata = $cmf->getMetadataFor($className); $metadata->namespace = $this->_namespace; $metadata->name = $newClassName; $metadata->customRepositoryClassName = $newClassName . "Repository"; $this->_generator->writeEntityClass($metadata, $this->_tmpDir); require $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $newClassName) . ".php"; }
/** * @group DDC-1590 */ public function testMethodsAndPropertiesAreNotDuplicatedInChildClasses() { $cmf = new ClassMetadataFactory(); $em = $this->_getTestEntityManager(); $cmf->setEntityManager($em); $ns = $this->_namespace; $nsdir = $this->_tmpDir . '/' . $ns; $content = str_replace('namespace Doctrine\\Tests\\Models\\DDC1590', 'namespace ' . $ns, file_get_contents(__DIR__ . '/../../Models/DDC1590/DDC1590User.php')); $fname = $nsdir . "/DDC1590User.php"; file_put_contents($fname, $content); require $fname; $metadata = $cmf->getMetadataFor($ns . '\\DDC1590User'); $this->_generator->writeEntityClass($metadata, $this->_tmpDir); // class DDC1590User extends DDC1590Entity { ... } $source = file_get_contents($fname); // class _DDC1590User extends DDC1590Entity { ... } $source2 = str_replace('class DDC1590User', 'class _DDC1590User', $source); $fname2 = $nsdir . "/_DDC1590User.php"; file_put_contents($fname2, $source2); require $fname2; // class __DDC1590User { ... } $source3 = str_replace('class DDC1590User extends DDC1590Entity', 'class __DDC1590User', $source); $fname3 = $nsdir . "/__DDC1590User.php"; file_put_contents($fname3, $source3); require $fname3; // class _DDC1590User extends DDC1590Entity { ... } $rc2 = new \ReflectionClass($ns . '\\_DDC1590User'); $this->assertTrue($rc2->hasProperty('name')); $this->assertTrue($rc2->hasProperty('id')); $this->assertTrue($rc2->hasProperty('created_at')); $this->assertTrue($rc2->hasMethod('getName')); $this->assertTrue($rc2->hasMethod('setName')); $this->assertTrue($rc2->hasMethod('getId')); $this->assertFalse($rc2->hasMethod('setId')); $this->assertTrue($rc2->hasMethod('getCreatedAt')); $this->assertTrue($rc2->hasMethod('setCreatedAt')); // class __DDC1590User { ... } $rc3 = new \ReflectionClass($ns . '\\__DDC1590User'); $this->assertTrue($rc3->hasProperty('name')); $this->assertFalse($rc3->hasProperty('id')); $this->assertFalse($rc3->hasProperty('created_at')); $this->assertTrue($rc3->hasMethod('getName')); $this->assertTrue($rc3->hasMethod('setName')); $this->assertFalse($rc3->hasMethod('getId')); $this->assertFalse($rc3->hasMethod('setId')); $this->assertFalse($rc3->hasMethod('getCreatedAt')); $this->assertFalse($rc3->hasMethod('setCreatedAt')); }
/** * @param Doctrine\ORM\EntityManagerInterface $em */ public function setEntityManager(Doctrine\ORM\EntityManagerInterface $em) { $this->em = $em; $this->config = $em->getConfiguration(); parent::setEntityManager($em); }
/** * @group DDC-3427 */ public function testAcceptsEntityManagerInterfaceInstances() { $classMetadataFactory = new ClassMetadataFactory(); /* @var $entityManager EntityManager */ $entityManager = $this->getMock('Doctrine\\ORM\\EntityManagerInterface'); $classMetadataFactory->setEntityManager($entityManager); // not really the cleanest way to check it, but we won't add a getter to the CMF just for the sake of testing. $this->assertAttributeSame($entityManager, 'em', $classMetadataFactory); }