PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/dbruenig/doctrine2
PHP | 675 lines | 605 code | 51 blank | 19 comment | 9 complexity | 366c3a3a3bd44a31975fda14c950c2a7 MD5 | raw file
Possible License(s): Unlicense
  1. <?php
  2. namespace Doctrine\Tests\ORM\Tools;
  3. use Doctrine\ORM\Tools\SchemaTool;
  4. use Doctrine\ORM\Tools\EntityGenerator;
  5. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  6. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  7. use Doctrine\ORM\Mapping\ClassMetadataFactory;
  8. use Doctrine\Tests\Models\DDC2372\DDC2372User;
  9. use Doctrine\Tests\Models\DDC2372\DDC2372Admin;
  10. class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
  11. {
  12. /**
  13. * @var EntityGenerator
  14. */
  15. private $_generator;
  16. private $_tmpDir;
  17. private $_namespace;
  18. public function setUp()
  19. {
  20. $this->_namespace = uniqid("doctrine_");
  21. $this->_tmpDir = \sys_get_temp_dir();
  22. \mkdir($this->_tmpDir . \DIRECTORY_SEPARATOR . $this->_namespace);
  23. $this->_generator = new EntityGenerator();
  24. $this->_generator->setAnnotationPrefix("");
  25. $this->_generator->setGenerateAnnotations(true);
  26. $this->_generator->setGenerateStubMethods(true);
  27. $this->_generator->setRegenerateEntityIfExists(false);
  28. $this->_generator->setUpdateEntityIfExists(true);
  29. $this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED);
  30. }
  31. public function tearDown()
  32. {
  33. $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir . '/' . $this->_namespace));
  34. foreach ($ri AS $file) {
  35. /* @var $file \SplFileInfo */
  36. if ($file->isFile()) {
  37. \unlink($file->getPathname());
  38. }
  39. }
  40. rmdir($this->_tmpDir . '/' . $this->_namespace);
  41. }
  42. public function generateBookEntityFixture()
  43. {
  44. $metadata = new ClassMetadataInfo($this->_namespace . '\EntityGeneratorBook');
  45. $metadata->namespace = $this->_namespace;
  46. $metadata->customRepositoryClassName = $this->_namespace . '\EntityGeneratorBookRepository';
  47. $metadata->table['name'] = 'book';
  48. $metadata->table['uniqueConstraints']['name_uniq'] = array('columns' => array('name'));
  49. $metadata->table['indexes']['status_idx'] = array('columns' => array('status'));
  50. $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
  51. $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'default' => 'published'));
  52. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  53. $metadata->mapOneToOne(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', 'mappedBy' => 'book'));
  54. $joinColumns = array(
  55. array('name' => 'author_id', 'referencedColumnName' => 'id')
  56. );
  57. $metadata->mapManyToMany(array(
  58. 'fieldName' => 'comments',
  59. 'targetEntity' => 'Doctrine\Tests\ORM\Tools\EntityGeneratorComment',
  60. 'fetch' => ClassMetadataInfo::FETCH_EXTRA_LAZY,
  61. 'joinTable' => array(
  62. 'name' => 'book_comment',
  63. 'joinColumns' => array(array('name' => 'book_id', 'referencedColumnName' => 'id')),
  64. 'inverseJoinColumns' => array(array('name' => 'comment_id', 'referencedColumnName' => 'id')),
  65. ),
  66. ));
  67. $metadata->addLifecycleCallback('loading', 'postLoad');
  68. $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
  69. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  70. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  71. return $metadata;
  72. }
  73. private function generateEntityTypeFixture(array $field)
  74. {
  75. $metadata = new ClassMetadataInfo($this->_namespace . '\EntityType');
  76. $metadata->namespace = $this->_namespace;
  77. $metadata->table['name'] = 'entity_type';
  78. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  79. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  80. $name = $field['fieldName'];
  81. $type = $field['dbType'];
  82. $metadata->mapField(array('fieldName' => $name, 'type' => $type));
  83. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  84. return $metadata;
  85. }
  86. /**
  87. * @param ClassMetadataInfo $metadata
  88. * @return EntityGeneratorBook
  89. */
  90. public function newInstance($metadata)
  91. {
  92. $path = $this->_tmpDir . '/'. $this->_namespace . '/EntityGeneratorBook.php';
  93. $this->assertFileExists($path);
  94. require_once $path;
  95. return new $metadata->name;
  96. }
  97. public function testGeneratedEntityClass()
  98. {
  99. $metadata = $this->generateBookEntityFixture();
  100. $book = $this->newInstance($metadata);
  101. $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
  102. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing.");
  103. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
  104. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
  105. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
  106. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing.");
  107. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing.");
  108. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing.");
  109. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addComment'), "EntityGeneratorBook::addComment() missing.");
  110. $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'removeComment'), "EntityGeneratorBook::removeComment() missing.");
  111. $this->assertEquals('published', $book->getStatus());
  112. $book->setName('Jonathan H. Wage');
  113. $this->assertEquals('Jonathan H. Wage', $book->getName());
  114. $author = new EntityGeneratorAuthor();
  115. $book->setAuthor($author);
  116. $this->assertEquals($author, $book->getAuthor());
  117. $comment = new EntityGeneratorComment();
  118. $book->addComment($comment);
  119. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  120. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  121. $book->removeComment($comment);
  122. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
  123. }
  124. public function testEntityUpdatingWorks()
  125. {
  126. $metadata = $this->generateBookEntityFixture();
  127. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  128. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  129. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
  130. $book = $this->newInstance($metadata);
  131. $reflClass = new \ReflectionClass($metadata->name);
  132. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  133. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  134. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  135. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  136. $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
  137. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  138. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  139. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  140. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  141. }
  142. /**
  143. * @group DDC-3152
  144. */
  145. public function testDoesNotRegenerateExistingMethodsWithDifferentCase()
  146. {
  147. $metadata = $this->generateBookEntityFixture();
  148. // Workaround to change existing fields case (just to simulate the use case)
  149. $metadata->fieldMappings['status']['fieldName'] = 'STATUS';
  150. // Should not throw a PHP fatal error
  151. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  152. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/EntityGeneratorBook.php~");
  153. $this->newInstance($metadata);
  154. $reflClass = new \ReflectionClass($metadata->name);
  155. $this->assertTrue($reflClass->hasProperty('status'));
  156. $this->assertTrue($reflClass->hasProperty('STATUS'));
  157. $this->assertTrue($reflClass->hasMethod('getStatus'));
  158. $this->assertTrue($reflClass->hasMethod('setStatus'));
  159. }
  160. /**
  161. * @group DDC-2121
  162. */
  163. public function testMethodDocBlockShouldStartWithBackSlash()
  164. {
  165. $metadata = $this->generateBookEntityFixture();
  166. $book = $this->newInstance($metadata);
  167. $this->assertPhpDocVarType('\Doctrine\Common\Collections\Collection', new \ReflectionProperty($book, 'comments'));
  168. $this->assertPhpDocReturnType('\Doctrine\Common\Collections\Collection', new \ReflectionMethod($book, 'getComments'));
  169. $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorComment', new \ReflectionMethod($book, 'addComment'));
  170. $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorComment', new \ReflectionMethod($book, 'removeComment'));
  171. $this->assertPhpDocVarType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionProperty($book, 'author'));
  172. $this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'getAuthor'));
  173. $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'setAuthor'));
  174. }
  175. public function testEntityExtendsStdClass()
  176. {
  177. $this->_generator->setClassToExtend('stdClass');
  178. $metadata = $this->generateBookEntityFixture();
  179. $book = $this->newInstance($metadata);
  180. $this->assertInstanceOf('stdClass', $book);
  181. }
  182. public function testLifecycleCallbacks()
  183. {
  184. $metadata = $this->generateBookEntityFixture();
  185. $book = $this->newInstance($metadata);
  186. $reflClass = new \ReflectionClass($metadata->name);
  187. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  188. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  189. }
  190. public function testLoadMetadata()
  191. {
  192. $metadata = $this->generateBookEntityFixture();
  193. $book = $this->newInstance($metadata);
  194. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  195. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  196. $driver = $this->createAnnotationDriver();
  197. $driver->loadMetadataForClass($cm->name, $cm);
  198. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  199. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  200. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  201. $this->assertEquals($cm->identifier, $metadata->identifier);
  202. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  203. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  204. $this->assertEquals(ClassMetadataInfo::FETCH_EXTRA_LAZY, $cm->associationMappings['comments']['fetch']);
  205. }
  206. public function testLoadPrefixedMetadata()
  207. {
  208. $this->_generator->setAnnotationPrefix('ORM\\');
  209. $metadata = $this->generateBookEntityFixture();
  210. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  211. $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, array());
  212. $book = $this->newInstance($metadata);
  213. $cm = new \Doctrine\ORM\Mapping\ClassMetadata($metadata->name);
  214. $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  215. $driver->loadMetadataForClass($cm->name, $cm);
  216. $this->assertEquals($cm->columnNames, $metadata->columnNames);
  217. $this->assertEquals($cm->getTableName(), $metadata->getTableName());
  218. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  219. $this->assertEquals($cm->identifier, $metadata->identifier);
  220. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  221. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  222. }
  223. /**
  224. * @dataProvider getParseTokensInEntityFileData
  225. */
  226. public function testParseTokensInEntityFile($php, $classes)
  227. {
  228. $r = new \ReflectionObject($this->_generator);
  229. $m = $r->getMethod('parseTokensInEntityFile');
  230. $m->setAccessible(true);
  231. $p = $r->getProperty('staticReflection');
  232. $p->setAccessible(true);
  233. $ret = $m->invoke($this->_generator, $php);
  234. $this->assertEquals($classes, array_keys($p->getValue($this->_generator)));
  235. }
  236. /**
  237. * @group DDC-1784
  238. */
  239. public function testGenerateEntityWithSequenceGenerator()
  240. {
  241. $metadata = new ClassMetadataInfo($this->_namespace . '\DDC1784Entity');
  242. $metadata->namespace = $this->_namespace;
  243. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  244. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
  245. $metadata->setSequenceGeneratorDefinition(array(
  246. 'sequenceName' => 'DDC1784_ID_SEQ',
  247. 'allocationSize' => 1,
  248. 'initialValue' => 2
  249. ));
  250. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  251. $filename = $this->_tmpDir . DIRECTORY_SEPARATOR
  252. . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php';
  253. $this->assertFileExists($filename);
  254. require_once $filename;
  255. $reflection = new \ReflectionProperty($metadata->name, 'id');
  256. $docComment = $reflection->getDocComment();
  257. $this->assertContains('@Id', $docComment);
  258. $this->assertContains('@Column(name="id", type="integer")', $docComment);
  259. $this->assertContains('@GeneratedValue(strategy="SEQUENCE")', $docComment);
  260. $this->assertContains('@SequenceGenerator(sequenceName="DDC1784_ID_SEQ", allocationSize=1, initialValue=2)', $docComment);
  261. }
  262. /**
  263. * @group DDC-2079
  264. */
  265. public function testGenerateEntityWithMultipleInverseJoinColumns()
  266. {
  267. $metadata = new ClassMetadataInfo($this->_namespace . '\DDC2079Entity');
  268. $metadata->namespace = $this->_namespace;
  269. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  270. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
  271. $metadata->mapManyToMany(array(
  272. 'fieldName' => 'centroCustos',
  273. 'targetEntity' => 'DDC2079CentroCusto',
  274. 'joinTable' => array(
  275. 'name' => 'unidade_centro_custo',
  276. 'joinColumns' => array(
  277. array('name' => 'idorcamento', 'referencedColumnName' => 'idorcamento'),
  278. array('name' => 'idunidade', 'referencedColumnName' => 'idunidade')
  279. ),
  280. 'inverseJoinColumns' => array(
  281. array('name' => 'idcentrocusto', 'referencedColumnName' => 'idcentrocusto'),
  282. array('name' => 'idpais', 'referencedColumnName' => 'idpais'),
  283. ),
  284. ),
  285. ));
  286. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  287. $filename = $this->_tmpDir . DIRECTORY_SEPARATOR
  288. . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC2079Entity.php';
  289. $this->assertFileExists($filename);
  290. require_once $filename;
  291. $property = new \ReflectionProperty($metadata->name, 'centroCustos');
  292. $docComment = $property->getDocComment();
  293. //joinColumns
  294. $this->assertContains('@JoinColumn(name="idorcamento", referencedColumnName="idorcamento"),', $docComment);
  295. $this->assertContains('@JoinColumn(name="idunidade", referencedColumnName="idunidade")', $docComment);
  296. //inverseJoinColumns
  297. $this->assertContains('@JoinColumn(name="idcentrocusto", referencedColumnName="idcentrocusto"),', $docComment);
  298. $this->assertContains('@JoinColumn(name="idpais", referencedColumnName="idpais")', $docComment);
  299. }
  300. /**
  301. * @group DDC-2172
  302. */
  303. public function testGetInheritanceTypeString()
  304. {
  305. $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
  306. $method = new \ReflectionMethod($this->_generator, 'getInheritanceTypeString');
  307. $constants = $reflection->getConstants();
  308. $pattern = '/^INHERITANCE_TYPE_/';
  309. $method->setAccessible(true);
  310. foreach ($constants as $name => $value) {
  311. if( ! preg_match($pattern, $name)) {
  312. continue;
  313. }
  314. $expected = preg_replace($pattern, '', $name);
  315. $actual = $method->invoke($this->_generator, $value);
  316. $this->assertEquals($expected, $actual);
  317. }
  318. $this->setExpectedException('\InvalidArgumentException', 'Invalid provided InheritanceType: INVALID');
  319. $method->invoke($this->_generator, 'INVALID');
  320. }
  321. /**
  322. * @group DDC-2172
  323. */
  324. public function testGetChangeTrackingPolicyString()
  325. {
  326. $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
  327. $method = new \ReflectionMethod($this->_generator, 'getChangeTrackingPolicyString');
  328. $constants = $reflection->getConstants();
  329. $pattern = '/^CHANGETRACKING_/';
  330. $method->setAccessible(true);
  331. foreach ($constants as $name => $value) {
  332. if( ! preg_match($pattern, $name)) {
  333. continue;
  334. }
  335. $expected = preg_replace($pattern, '', $name);
  336. $actual = $method->invoke($this->_generator, $value);
  337. $this->assertEquals($expected, $actual);
  338. }
  339. $this->setExpectedException('\InvalidArgumentException', 'Invalid provided ChangeTrackingPolicy: INVALID');
  340. $method->invoke($this->_generator, 'INVALID');
  341. }
  342. /**
  343. * @group DDC-2172
  344. */
  345. public function testGetIdGeneratorTypeString()
  346. {
  347. $reflection = new \ReflectionClass('\Doctrine\ORM\Mapping\ClassMetadata');
  348. $method = new \ReflectionMethod($this->_generator, 'getIdGeneratorTypeString');
  349. $constants = $reflection->getConstants();
  350. $pattern = '/^GENERATOR_TYPE_/';
  351. $method->setAccessible(true);
  352. foreach ($constants as $name => $value) {
  353. if( ! preg_match($pattern, $name)) {
  354. continue;
  355. }
  356. $expected = preg_replace($pattern, '', $name);
  357. $actual = $method->invoke($this->_generator, $value);
  358. $this->assertEquals($expected, $actual);
  359. }
  360. $this->setExpectedException('\InvalidArgumentException', 'Invalid provided IdGeneratorType: INVALID');
  361. $method->invoke($this->_generator, 'INVALID');
  362. }
  363. /**
  364. * @dataProvider getEntityTypeAliasDataProvider
  365. *
  366. * @group DDC-1694
  367. */
  368. public function testEntityTypeAlias(array $field)
  369. {
  370. $metadata = $this->generateEntityTypeFixture($field);
  371. $path = $this->_tmpDir . '/'. $this->_namespace . '/EntityType.php';
  372. $this->assertFileExists($path);
  373. require_once $path;
  374. $entity = new $metadata->name;
  375. $reflClass = new \ReflectionClass($metadata->name);
  376. $type = $field['phpType'];
  377. $name = $field['fieldName'];
  378. $value = $field['value'];
  379. $getter = "get" . ucfirst($name);
  380. $setter = "set" . ucfirst($name);
  381. $this->assertPhpDocVarType($type, $reflClass->getProperty($name));
  382. $this->assertPhpDocParamType($type, $reflClass->getMethod($setter));
  383. $this->assertPhpDocReturnType($type, $reflClass->getMethod($getter));
  384. $this->assertSame($entity, $entity->{$setter}($value));
  385. $this->assertEquals($value, $entity->{$getter}());
  386. }
  387. /**
  388. * @group DDC-2372
  389. */
  390. public function testTraitPropertiesAndMethodsAreNotDuplicated()
  391. {
  392. if (PHP_VERSION_ID < 50400) {
  393. $this->markTestSkipped('Traits are not available before php 5.4.');
  394. }
  395. $cmf = new ClassMetadataFactory();
  396. $em = $this->_getTestEntityManager();
  397. $cmf->setEntityManager($em);
  398. $user = new DDC2372User();
  399. $metadata = $cmf->getMetadataFor(get_class($user));
  400. $metadata->name = $this->_namespace . "\DDC2372User";
  401. $metadata->namespace = $this->_namespace;
  402. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  403. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/DDC2372User.php");
  404. require $this->_tmpDir . "/" . $this->_namespace . "/DDC2372User.php";
  405. $reflClass = new \ReflectionClass($metadata->name);
  406. $this->assertSame($reflClass->hasProperty('address'), false);
  407. $this->assertSame($reflClass->hasMethod('setAddress'), false);
  408. $this->assertSame($reflClass->hasMethod('getAddress'), false);
  409. }
  410. /**
  411. * @group DDC-2372
  412. */
  413. public function testTraitPropertiesAndMethodsAreNotDuplicatedInChildClasses()
  414. {
  415. if (PHP_VERSION_ID < 50400) {
  416. $this->markTestSkipped('Traits are not available before php 5.4.');
  417. }
  418. $cmf = new ClassMetadataFactory();
  419. $em = $this->_getTestEntityManager();
  420. $cmf->setEntityManager($em);
  421. $user = new DDC2372Admin();
  422. $metadata = $cmf->getMetadataFor(get_class($user));
  423. $metadata->name = $this->_namespace . "\DDC2372Admin";
  424. $metadata->namespace = $this->_namespace;
  425. $this->_generator->writeEntityClass($metadata, $this->_tmpDir);
  426. $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/DDC2372Admin.php");
  427. require $this->_tmpDir . "/" . $this->_namespace . "/DDC2372Admin.php";
  428. $reflClass = new \ReflectionClass($metadata->name);
  429. $this->assertSame($reflClass->hasProperty('address'), false);
  430. $this->assertSame($reflClass->hasMethod('setAddress'), false);
  431. $this->assertSame($reflClass->hasMethod('getAddress'), false);
  432. }
  433. /**
  434. * @return array
  435. */
  436. public function getEntityTypeAliasDataProvider()
  437. {
  438. return array(
  439. array(array(
  440. 'fieldName' => 'datetimetz',
  441. 'phpType' => '\\DateTime',
  442. 'dbType' => 'datetimetz',
  443. 'value' => new \DateTime
  444. )),
  445. array(array(
  446. 'fieldName' => 'datetime',
  447. 'phpType' => '\\DateTime',
  448. 'dbType' => 'datetime',
  449. 'value' => new \DateTime
  450. )),
  451. array(array(
  452. 'fieldName' => 'date',
  453. 'phpType' => '\\DateTime',
  454. 'dbType' => 'date',
  455. 'value' => new \DateTime
  456. )),
  457. array(array(
  458. 'fieldName' => 'time',
  459. 'phpType' => '\DateTime',
  460. 'dbType' => 'time',
  461. 'value' => new \DateTime
  462. )),
  463. array(array(
  464. 'fieldName' => 'object',
  465. 'phpType' => '\stdClass',
  466. 'dbType' => 'object',
  467. 'value' => new \stdClass()
  468. )),
  469. array(array(
  470. 'fieldName' => 'bigint',
  471. 'phpType' => 'integer',
  472. 'dbType' => 'bigint',
  473. 'value' => 11
  474. )),
  475. array(array(
  476. 'fieldName' => 'smallint',
  477. 'phpType' => 'integer',
  478. 'dbType' => 'smallint',
  479. 'value' => 22
  480. )),
  481. array(array(
  482. 'fieldName' => 'text',
  483. 'phpType' => 'string',
  484. 'dbType' => 'text',
  485. 'value' => 'text'
  486. )),
  487. array(array(
  488. 'fieldName' => 'blob',
  489. 'phpType' => 'string',
  490. 'dbType' => 'blob',
  491. 'value' => 'blob'
  492. )),
  493. array(array(
  494. 'fieldName' => 'decimal',
  495. 'phpType' => 'string',
  496. 'dbType' => 'decimal',
  497. 'value' => '12.34'
  498. ),
  499. ));
  500. }
  501. public function getParseTokensInEntityFileData()
  502. {
  503. return array(
  504. array(
  505. '<?php namespace Foo\Bar; class Baz {}',
  506. array('Foo\Bar\Baz'),
  507. ),
  508. array(
  509. '<?php namespace Foo\Bar; use Foo; class Baz {}',
  510. array('Foo\Bar\Baz'),
  511. ),
  512. array(
  513. '<?php namespace /*Comment*/ Foo\Bar; /** Foo */class /* Comment */ Baz {}',
  514. array('Foo\Bar\Baz'),
  515. ),
  516. array(
  517. '
  518. <?php namespace
  519. /*Comment*/
  520. Foo\Bar
  521. ;
  522. /** Foo */
  523. class
  524. /* Comment */
  525. Baz {}
  526. ',
  527. array('Foo\Bar\Baz'),
  528. ),
  529. );
  530. }
  531. /**
  532. * @param string $type
  533. * @param \ReflectionProperty $property
  534. */
  535. private function assertPhpDocVarType($type, \ReflectionProperty $property)
  536. {
  537. $this->assertEquals(1, preg_match('/@var\s+([^\s]+)/',$property->getDocComment(), $matches));
  538. $this->assertEquals($type, $matches[1]);
  539. }
  540. /**
  541. * @param string $type
  542. * @param \ReflectionProperty $method
  543. */
  544. private function assertPhpDocReturnType($type, \ReflectionMethod $method)
  545. {
  546. $this->assertEquals(1, preg_match('/@return\s+([^\s]+)/', $method->getDocComment(), $matches));
  547. $this->assertEquals($type, $matches[1]);
  548. }
  549. /**
  550. * @param string $type
  551. * @param \ReflectionProperty $method
  552. */
  553. private function assertPhpDocParamType($type, \ReflectionMethod $method)
  554. {
  555. $this->assertEquals(1, preg_match('/@param\s+([^\s]+)/', $method->getDocComment(), $matches));
  556. $this->assertEquals($type, $matches[1]);
  557. }
  558. }
  559. class EntityGeneratorAuthor {}
  560. class EntityGeneratorComment {}