PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php

https://github.com/gedrox/doctrine2
PHP | 292 lines | 247 code | 37 blank | 8 comment | 9 complexity | b1736e9634dc5bc98617deac32588734 MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\ORM\Tools;
  3. use Doctrine\ORM\Tools\SchemaTool,
  4. Doctrine\ORM\Tools\EntityGenerator,
  5. Doctrine\ORM\Tools\Export\ClassMetadataExporter,
  6. Doctrine\ORM\Mapping\ClassMetadataInfo;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
  9. {
  10. private $_generator;
  11. private $_tmpDir;
  12. private $_namespace;
  13. public function setUp()
  14. {
  15. $this->_namespace = uniqid("doctrine_");
  16. $this->_tmpDir = \sys_get_temp_dir();
  17. \mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace);
  18. $this->_generator = new EntityGenerator();
  19. $this->_generator->setAnnotationPrefix("");
  20. $this->_generator->setGenerateAnnotations(true);
  21. $this->_generator->setGenerateStubMethods(true);
  22. $this->_generator->setRegenerateEntityIfExists(false);
  23. $this->_generator->setUpdateEntityIfExists(true);
  24. $this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED);
  25. }
  26. public function tearDown()
  27. {
  28. $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
  29. foreach ($ri AS $file) {
  30. /* @var $file \SplFileInfo */
  31. if ($file->isFile()) {
  32. \unlink($file->getPathname());
  33. }
  34. }
  35. rmdir($this->_tmpDir . '/' . $this->_namespace);
  36. }
  37. public function generateBookEntityFixture()
  38. {
  39. $metadata = new ClassMetadataInfo($this->_namespace . '\EntityGeneratorBook');
  40. $metadata->namespace = $this->_namespace;
  41. $metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository';
  42. $metadata->table['name'] = 'book';
  43. $metadata->table['uniqueConstraints']['name_uniq'] = array('columns' => array('name'));
  44. $metadata->table['indexes']['status_idx'] = array('columns' => array('status'));
  45. $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
  46. $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
  47. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  48. $metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
  49. $joinColumns = array(
  50. array('name' => 'author_id', 'referencedColumnName' => 'id')
  51. );
  52. $metadata->mapManyToMany(array(
  53. 'fieldName' => 'comments',
  54. 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
  55. 'joinTable' => array(
  56. 'name' => 'book_comment',
  57. 'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
  58. 'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
  59. ),
  60. ));
  61. $metadata->addLifecycleCallback('loading', 'postLoad');
  62. $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
  63. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  64. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  65. return $metadata;
  66. }
  67. /**
  68. * @param ClassMetadataInfo $metadata
  69. * @return EntityGeneratorBook
  70. */
  71. public function newInstance($metadata)
  72. {
  73. $path = $this->_tmpDir . '/'. $this->_namespace . '/EntityGeneratorBook.php';
  74. $this->assertFileExists($path);
  75. require_once $path;
  76. return new $metadata->name;
  77. }
  78. public function testGeneratedEntityClass()
  79. {
  80. $metadata = $this->generateBookEntityFixture();
  81. $book = $this->newInstance($metadata);
  82. $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
  83. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing.");
  84. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
  85. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
  86. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
  87. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing.");
  88. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing.");
  89. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing.");
  90. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addEntityGeneratorComment'), "EntityGeneratorBook::addEntityGeneratorComment() missing.");
  91. $this->assertEquals('published', $book->getStatus());
  92. $book->setName('Jonathan H. Wage');
  93. $this->assertEquals('Jonathan H. Wage', $book->getName());
  94. $author = new EntityGeneratorAuthor();
  95. $book->setAuthor($author);
  96. $this->assertEquals($author, $book->getAuthor());
  97. $comment = new EntityGeneratorComment();
  98. $book->addEntityGeneratorComment($comment);
  99. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  100. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  101. }
  102. public function testEntityUpdatingWorks()
  103. {
  104. $metadata = $this->generateBookEntityFixture();
  105. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  106. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  107. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
  108. $book = $this->newInstance($metadata);
  109. $reflClass = new \ReflectionClass($metadata->name);
  110. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  111. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  112. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  113. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  114. $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
  115. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  116. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  117. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  118. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  119. }
  120. public function testEntityExtendsStdClass()
  121. {
  122. $this->_generator->setClassToExtend('stdClass');
  123. $metadata = $this->generateBookEntityFixture();
  124. $book = $this->newInstance($metadata);
  125. $this->assertInstanceOf('stdClass', $book);
  126. }
  127. public function testLifecycleCallbacks()
  128. {
  129. $metadata = $this->generateBookEntityFixture();
  130. $book = $this->newInstance($metadata);
  131. $reflClass = new \ReflectionClass($metadata->name);
  132. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  133. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  134. }
  135. public function testLoadMetadata()
  136. {
  137. $metadata = $this->generateBookEntityFixture();
  138. $book = $this->newInstance($metadata);
  139. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  140. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  141. $driver = $this->createAnnotationDriver();
  142. $driver->loadMetadataForClass($cm->name, $cm);
  143. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  144. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  145. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  146. $this->assertEquals($cm->identifier, $metadata->identifier);
  147. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  148. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  149. }
  150. public function testLoadPrefixedMetadata()
  151. {
  152. $this->_generator->setAnnotationPrefix('ORM\\');
  153. $metadata = $this->generateBookEntityFixture();
  154. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  155. $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array());
  156. $book = $this->newInstance($metadata);
  157. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  158. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  159. $driver->loadMetadataForClass($cm->name, $cm);
  160. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  161. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  162. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  163. $this->assertEquals($cm->identifier, $metadata->identifier);
  164. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  165. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  166. }
  167. /**
  168. * @dataProvider getParseTokensInEntityFileData
  169. */
  170. public function testParseTokensInEntityFile($php, $classes)
  171. {
  172. $r = new \ReflectionObject($this->_generator);
  173. $m = $r->getMethod('_parseTokensInEntityFile');
  174. $m->setAccessible(true);
  175. $p = $r->getProperty('_staticReflection');
  176. $p->setAccessible(true);
  177. $ret = $m->invoke($this->_generator, $php);
  178. $this->assertEquals($classes, array_keys($p->getValue($this->_generator)));
  179. }
  180. /**
  181. * @group DDC-1784
  182. */
  183. public function testGenerateEntityWithSequenceGenerator()
  184. {
  185. $metadata = new ClassMetadataInfo($this->_namespace . '\DDC1784Entity');
  186. $metadata->namespace = $this->_namespace;
  187. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  188. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
  189. $metadata->setSequenceGeneratorDefinition(array(
  190. 'sequenceName' => 'DDC1784_ID_SEQ',
  191. 'allocationSize' => 1,
  192. 'initialValue' => 2
  193. ));
  194. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  195. $filename = $this->_tmpDir . DIRECTORY_SEPARATOR
  196. . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php';
  197. $this->assertFileExists($filename);
  198. require_once $filename;
  199. $reflection = new \ReflectionProperty($metadata->name, 'id');
  200. $docComment = $reflection->getDocComment();
  201. $this->assertContains('@Id', $docComment);
  202. $this->assertContains('@Column(name="id", type="integer")', $docComment);
  203. $this->assertContains('@GeneratedValue(strategy="SEQUENCE")', $docComment);
  204. $this->assertContains('@SequenceGenerator(sequenceName="DDC1784_ID_SEQ", allocationSize=1, initialValue=2)', $docComment);
  205. }
  206. public function getParseTokensInEntityFileData()
  207. {
  208. return array(
  209. array(
  210. '<?php namespace Foo\Bar; class Baz {}',
  211. array('Foo\Bar\Baz'),
  212. ),
  213. array(
  214. '<?php namespace Foo\Bar; use Foo; class Baz {}',
  215. array('Foo\Bar\Baz'),
  216. ),
  217. array(
  218. '<?php namespace /*Comment*/ Foo\Bar; /** Foo */class /* Comment */ Baz {}',
  219. array('Foo\Bar\Baz'),
  220. ),
  221. array(
  222. '
  223. <?php namespace
  224. /*Comment*/
  225. Foo\Bar
  226. ;
  227. /** Foo */
  228. class
  229. /* Comment */
  230. Baz {}
  231. ',
  232. array('Foo\Bar\Baz'),
  233. ),
  234. );
  235. }
  236. }
  237. class EntityGeneratorAuthor {}
  238. class EntityGeneratorComment {}