PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/doctrine_22/vendor/doctrine2/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php

http://github.com/eventhorizonpl/forked-php-orm-benchmark
PHP | 258 lines | 213 code | 37 blank | 8 comment | 9 complexity | 78d1637c1bf6bfdd795a79195e4092ac 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', 'addEntityGeneratorComment'), "EntityGeneratorBook::addEntityGeneratorComment() missing.");
  90. $this->assertEquals('published', $book->getStatus());
  91. $book->setName('Jonathan H. Wage');
  92. $this->assertEquals('Jonathan H. Wage', $book->getName());
  93. $author = new EntityGeneratorAuthor();
  94. $book->setAuthor($author);
  95. $this->assertEquals($author, $book->getAuthor());
  96. $comment = new EntityGeneratorComment();
  97. $book->addEntityGeneratorComment($comment);
  98. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  99. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  100. }
  101. public function testEntityUpdatingWorks()
  102. {
  103. $metadata = $this->generateBookEntityFixture();
  104. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  105. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  106. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
  107. $book = $this->newInstance($metadata);
  108. $reflClass = new \ReflectionClass($metadata->name);
  109. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  110. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  111. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  112. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  113. $this->assertTrue($reflClass->getProperty('test')->isPrivate(), "Check for private property test failed.");
  114. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  115. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  116. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  117. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  118. }
  119. public function testEntityExtendsStdClass()
  120. {
  121. $this->_generator->setClassToExtend('stdClass');
  122. $metadata = $this->generateBookEntityFixture();
  123. $book = $this->newInstance($metadata);
  124. $this->assertInstanceOf('stdClass', $book);
  125. }
  126. public function testLifecycleCallbacks()
  127. {
  128. $metadata = $this->generateBookEntityFixture();
  129. $book = $this->newInstance($metadata);
  130. $reflClass = new \ReflectionClass($metadata->name);
  131. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  132. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  133. }
  134. public function testLoadMetadata()
  135. {
  136. $metadata = $this->generateBookEntityFixture();
  137. $book = $this->newInstance($metadata);
  138. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  139. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  140. $driver = $this->createAnnotationDriver();
  141. $driver->loadMetadataForClass($cm->name, $cm);
  142. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  143. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  144. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  145. $this->assertEquals($cm->identifier, $metadata->identifier);
  146. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  147. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  148. }
  149. public function testLoadPrefixedMetadata()
  150. {
  151. $this->_generator->setAnnotationPrefix('ORM\\');
  152. $metadata = $this->generateBookEntityFixture();
  153. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  154. $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array());
  155. $book = $this->newInstance($metadata);
  156. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  157. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  158. $driver->loadMetadataForClass($cm->name, $cm);
  159. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  160. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  161. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  162. $this->assertEquals($cm->identifier, $metadata->identifier);
  163. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  164. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  165. }
  166. /**
  167. * @dataProvider getParseTokensInEntityFileData
  168. */
  169. public function testParseTokensInEntityFile($php, $classes)
  170. {
  171. $r = new \ReflectionObject($this->_generator);
  172. $m = $r->getMethod('_parseTokensInEntityFile');
  173. $m->setAccessible(true);
  174. $p = $r->getProperty('_staticReflection');
  175. $p->setAccessible(true);
  176. $ret = $m->invoke($this->_generator, $php);
  177. $this->assertEquals($classes, array_keys($p->getValue($this->_generator)));
  178. }
  179. public function getParseTokensInEntityFileData()
  180. {
  181. return array(
  182. array(
  183. '<?php namespace Foo\Bar; class Baz {}',
  184. array('Foo\Bar\Baz'),
  185. ),
  186. array(
  187. '<?php namespace Foo\Bar; use Foo; class Baz {}',
  188. array('Foo\Bar\Baz'),
  189. ),
  190. array(
  191. '<?php namespace /*Comment*/ Foo\Bar; /** Foo */class /* Comment */ Baz {}',
  192. array('Foo\Bar\Baz'),
  193. ),
  194. array(
  195. '
  196. <?php namespace
  197. /*Comment*/
  198. Foo\Bar
  199. ;
  200. /** Foo */
  201. class
  202. /* Comment */
  203. Baz {}
  204. ',
  205. array('Foo\Bar\Baz'),
  206. ),
  207. );
  208. }
  209. }
  210. class EntityGeneratorAuthor {}
  211. class EntityGeneratorComment {}