PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/doctrine_22/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php

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