/**
  * @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";
 }
예제 #2
0
 public function testRegenerateEntityClass()
 {
     $metadata = $this->generateBookEntityFixture();
     $this->loadEntityClass($metadata);
     $className = basename(str_replace('\\', '/', $metadata->name));
     $path = $this->_tmpDir . '/' . $this->_namespace . '/' . $className . '.php';
     $classTest = file_get_contents($path);
     $this->_generator->setRegenerateEntityIfExists(true);
     $this->_generator->setBackupExisting(false);
     $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
     $classNew = file_get_contents($path);
     $this->assertSame($classTest, $classNew);
 }
예제 #3
0
 /**
  * @group DDC-1784
  */
 public function testGenerateEntityWithSequenceGenerator()
 {
     $metadata = new ClassMetadataInfo($this->_namespace . '\\DDC1784Entity');
     $metadata->namespace = $this->_namespace;
     $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
     $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
     $metadata->setSequenceGeneratorDefinition(array('sequenceName' => 'DDC1784_ID_SEQ', 'allocationSize' => 1, 'initialValue' => 2));
     $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
     $filename = $this->_tmpDir . DIRECTORY_SEPARATOR . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php';
     $this->assertFileExists($filename);
     require_once $filename;
     $reflection = new \ReflectionProperty($metadata->name, 'id');
     $docComment = $reflection->getDocComment();
     $this->assertContains('@Id', $docComment);
     $this->assertContains('@Column(name="id", type="integer")', $docComment);
     $this->assertContains('@GeneratedValue(strategy="SEQUENCE")', $docComment);
     $this->assertContains('@SequenceGenerator(sequenceName="DDC1784_ID_SEQ", allocationSize=1, initialValue=2)', $docComment);
 }
예제 #4
0
 /**
  * @group DDC-2079
  */
 public function testGenerateEntityWithMultipleInverseJoinColumns()
 {
     $metadata = new ClassMetadataInfo($this->_namespace . '\\DDC2079Entity');
     $metadata->namespace = $this->_namespace;
     $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
     $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
     $metadata->mapManyToMany(array('fieldName' => 'centroCustos', 'targetEntity' => 'DDC2079CentroCusto', 'joinTable' => array('name' => 'unidade_centro_custo', 'joinColumns' => array(array('name' => 'idorcamento', 'referencedColumnName' => 'idorcamento'), array('name' => 'idunidade', 'referencedColumnName' => 'idunidade')), 'inverseJoinColumns' => array(array('name' => 'idcentrocusto', 'referencedColumnName' => 'idcentrocusto'), array('name' => 'idpais', 'referencedColumnName' => 'idpais')))));
     $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
     $filename = $this->_tmpDir . DIRECTORY_SEPARATOR . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC2079Entity.php';
     $this->assertFileExists($filename);
     require_once $filename;
     $property = new \ReflectionProperty($metadata->name, 'centroCustos');
     $docComment = $property->getDocComment();
     //joinColumns
     $this->assertContains('@JoinColumn(name="idorcamento", referencedColumnName="idorcamento"),', $docComment);
     $this->assertContains('@JoinColumn(name="idunidade", referencedColumnName="idunidade")', $docComment);
     //inverseJoinColumns
     $this->assertContains('@JoinColumn(name="idcentrocusto", referencedColumnName="idcentrocusto"),', $docComment);
     $this->assertContains('@JoinColumn(name="idpais", referencedColumnName="idpais")', $docComment);
 }
 public function generate($parameters)
 {
     if (!$this->isInitialized) {
         throw new \RuntimeException('Entity generator needs to be initialized first in order to generate');
     }
     $moduleName = $this->moduleName;
     $parentClassName = $this->parentClassName;
     $this->classNames = $parameters->classNames;
     $classNames = $this->classNames;
     $outputList = array();
     $metadatas = array();
     foreach ($classNames as $className) {
         if (in_array($className, $this->driver->getAllClassNames())) {
             $classNameWithNamespace = $this->getNamespacedClassName($className);
             $this->namespacedClassNames[$className] = $classNameWithNamespace;
             $realClassNameWithNamespace = $this->composeFullClassName($classNameWithNamespace);
             $realClassName = $this->getClassNameFromNamespacedClassName($classNameWithNamespace);
             $namespace = $this->getNamespaceFromNamespacedClassName($realClassNameWithNamespace, $realClassName);
             $metadata = new \Doctrine\ORM\Mapping\ClassMetadataInfo($className);
             $this->driver->loadMetadataForClass($className, $metadata);
             $metadata = $this->fixMetadata($metadata);
             $outputList[] = array('outputDirectory' => $this->generatedEntityOutputDirectory, 'entityNamespace' => $this->entityNamespace, 'entityName' => $className, 'className' => $realClassName);
             $metadata->name = $realClassNameWithNamespace;
             $metadata->namespace = $namespace;
             $metadatas[] = $metadata;
             echo $namespace . '\\' . $realClassName . '<br>';
         }
     }
     if (count($metadatas) > 0) {
         $this->initGenerator();
         if ($this->parentClassName != null) {
             if (!file_exists($this->generatedEntityOutputDirectory . '/' . $this->parentClassName . '.php')) {
                 $parentMetadata = new \Doctrine\ORM\Mapping\ClassMetadataInfo($this->parentClassName);
                 $parentMetadata->name = $this->composeFullClassName($this->parentClassName);
                 $this->generator->writeEntityClass($parentMetadata, $this->generatedEntityOutputDirectory);
             }
         }
         $this->generator->generate($metadatas, $this->generatedEntityOutputDirectory);
     }
     return $outputList;
 }