PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ingsol/music_sonata
PHP | 250 lines | 198 code | 44 blank | 8 comment | 9 complexity | 6c666ea9ee15be94cb450288751cb1d8 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  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->setGenerateAnnotations(true);
  20. $this->_generator->setGenerateStubMethods(true);
  21. $this->_generator->setRegenerateEntityIfExists(false);
  22. $this->_generator->setUpdateEntityIfExists(true);
  23. }
  24. public function tearDown()
  25. {
  26. $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
  27. foreach ($ri AS $file) {
  28. /* @var $file \SplFileInfo */
  29. if ($file->isFile()) {
  30. \unlink($file->getPathname());
  31. }
  32. }
  33. rmdir($this->_tmpDir . '/' . $this->_namespace);
  34. }
  35. public function generateBookEntityFixture()
  36. {
  37. $metadata = new ClassMetadataInfo($this->_namespace . '\EntityGeneratorBook');
  38. $metadata->namespace = $this->_namespace;
  39. $metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository';
  40. $metadata->table['name'] = 'book';
  41. $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
  42. $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
  43. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  44. $metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
  45. $joinColumns = array(
  46. array('name' => 'author_id', 'referencedColumnName' => 'id')
  47. );
  48. $metadata->mapManyToMany(array(
  49. 'fieldName' => 'comments',
  50. 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
  51. 'joinTable' => array(
  52. 'name' => 'book_comment',
  53. 'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
  54. 'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
  55. ),
  56. ));
  57. $metadata->addLifecycleCallback('loading', 'postLoad');
  58. $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
  59. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  60. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  61. return $metadata;
  62. }
  63. /**
  64. * @param ClassMetadataInfo $metadata
  65. * @return EntityGeneratorBook
  66. */
  67. public function newInstance($metadata)
  68. {
  69. $path = $this->_tmpDir . '/'. $this->_namespace . '/EntityGeneratorBook.php';
  70. $this->assertFileExists($path);
  71. require_once $path;
  72. return new $metadata->name;
  73. }
  74. public function testGeneratedEntityClass()
  75. {
  76. $metadata = $this->generateBookEntityFixture();
  77. $book = $this->newInstance($metadata);
  78. $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
  79. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing.");
  80. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
  81. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
  82. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
  83. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing.");
  84. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing.");
  85. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing.");
  86. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addEntityGeneratorComment'), "EntityGeneratorBook::addEntityGeneratorComment() missing.");
  87. $this->assertEquals('published', $book->getStatus());
  88. $book->setName('Jonathan H. Wage');
  89. $this->assertEquals('Jonathan H. Wage', $book->getName());
  90. $author = new EntityGeneratorAuthor();
  91. $book->setAuthor($author);
  92. $this->assertEquals($author, $book->getAuthor());
  93. $comment = new EntityGeneratorComment();
  94. $book->addEntityGeneratorComment($comment);
  95. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  96. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  97. }
  98. public function testEntityUpdatingWorks()
  99. {
  100. $metadata = $this->generateBookEntityFixture();
  101. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  102. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  103. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
  104. $book = $this->newInstance($metadata);
  105. $reflClass = new \ReflectionClass($metadata->name);
  106. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  107. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  108. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  109. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  110. $this->assertTrue($reflClass->getProperty('test')->isPrivate(), "Check for private property test failed.");
  111. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  112. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  113. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  114. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  115. }
  116. public function testEntityExtendsStdClass()
  117. {
  118. $this->_generator->setClassToExtend('stdClass');
  119. $metadata = $this->generateBookEntityFixture();
  120. $book = $this->newInstance($metadata);
  121. $this->assertInstanceOf('stdClass', $book);
  122. }
  123. public function testLifecycleCallbacks()
  124. {
  125. $metadata = $this->generateBookEntityFixture();
  126. $book = $this->newInstance($metadata);
  127. $reflClass = new \ReflectionClass($metadata->name);
  128. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  129. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  130. }
  131. public function testLoadMetadata()
  132. {
  133. $metadata = $this->generateBookEntityFixture();
  134. $book = $this->newInstance($metadata);
  135. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  136. $driver = $this->createAnnotationDriver();
  137. $driver->loadMetadataForClass($cm->name, $cm);
  138. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  139. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  140. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  141. $this->assertEquals($cm->identifier, $metadata->identifier);
  142. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  143. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  144. }
  145. public function testLoadPrefixedMetadata()
  146. {
  147. $this->_generator->setAnnotationPrefix('orm:');
  148. $metadata = $this->generateBookEntityFixture();
  149. $book = $this->newInstance($metadata);
  150. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  151. $driver = $this->createAnnotationDriver(array(), 'orm');
  152. $driver->loadMetadataForClass($cm->name, $cm);
  153. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  154. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  155. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  156. $this->assertEquals($cm->identifier, $metadata->identifier);
  157. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  158. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  159. }
  160. /**
  161. * @dataProvider getParseTokensInEntityFileData
  162. */
  163. public function testParseTokensInEntityFile($php, $classes)
  164. {
  165. $r = new \ReflectionObject($this->_generator);
  166. $m = $r->getMethod('_parseTokensInEntityFile');
  167. $m->setAccessible(true);
  168. $p = $r->getProperty('_staticReflection');
  169. $p->setAccessible(true);
  170. $ret = $m->invoke($this->_generator, $php);
  171. $this->assertEquals($classes, array_keys($p->getValue($this->_generator)));
  172. }
  173. public function getParseTokensInEntityFileData()
  174. {
  175. return array(
  176. array(
  177. '<?php namespace Foo\Bar; class Baz {}',
  178. array('Foo\Bar\Baz'),
  179. ),
  180. array(
  181. '<?php namespace Foo\Bar; use Foo; class Baz {}',
  182. array('Foo\Bar\Baz'),
  183. ),
  184. array(
  185. '<?php namespace /*Comment*/ Foo\Bar; /** Foo */class /* Comment */ Baz {}',
  186. array('Foo\Bar\Baz'),
  187. ),
  188. array(
  189. '
  190. <?php namespace
  191. /*Comment*/
  192. Foo\Bar
  193. ;
  194. /** Foo */
  195. class
  196. /* Comment */
  197. Baz {}
  198. ',
  199. array('Foo\Bar\Baz'),
  200. ),
  201. );
  202. }
  203. }
  204. class EntityGeneratorAuthor {}
  205. class EntityGeneratorComment {}