PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Doctrine/ODM/MongoDB/Tests/Tools/DocumentGeneratorTest.php

http://github.com/doctrine/mongodb-odm
PHP | 300 lines | 231 code | 51 blank | 18 comment | 9 complexity | b616a8dd2fd4b6d78a08d87cfbc5b6b2 MD5 | raw file
  1. <?php
  2. namespace Doctrine\ODM\MongoDB\Tests\Tools;
  3. use Doctrine\ODM\MongoDB\Tools\DocumentGenerator;
  4. use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
  5. class DocumentGeneratorTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
  6. {
  7. private $generator;
  8. private $tmpDir;
  9. private $namespace;
  10. public function setUp()
  11. {
  12. parent::setUp();
  13. $this->namespace = uniqid("doctrine_");
  14. $this->tmpDir = \sys_get_temp_dir();
  15. \mkdir($this->tmpDir . \DIRECTORY_SEPARATOR . $this->namespace);
  16. $this->generator = new DocumentGenerator();
  17. $this->generator->setGenerateAnnotations(true);
  18. $this->generator->setGenerateStubMethods(true);
  19. $this->generator->setRegenerateDocumentIfExists(false);
  20. $this->generator->setUpdateDocumentIfExists(true);
  21. }
  22. public function tearDown()
  23. {
  24. parent::tearDown();
  25. $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir . '/' . $this->namespace));
  26. foreach ($ri AS $file) {
  27. /* @var $file \SplFileInfo */
  28. if ($file->isFile()) {
  29. \unlink($file->getPathname());
  30. }
  31. }
  32. rmdir($this->tmpDir . '/' . $this->namespace);
  33. }
  34. public function generateBookDocumentFixture()
  35. {
  36. $metadata = new ClassMetadataInfo($this->namespace . '\DocumentGeneratorBook');
  37. $metadata->namespace = $this->namespace;
  38. $metadata->customRepositoryClassName = $this->namespace . '\DocumentGeneratorBookRepository';
  39. $metadata->collection = 'book';
  40. $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
  41. $metadata->mapField(array('fieldName' => 'status', 'type' => 'string'));
  42. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  43. $metadata->mapOneReference(array('fieldName' => 'author', 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorAuthor'));
  44. $metadata->mapManyReference(array(
  45. 'fieldName' => 'comments',
  46. 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorComment'
  47. ));
  48. $metadata->mapManyReference(array(
  49. 'fieldName' => 'searches',
  50. 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorSearch'
  51. ));
  52. $metadata->addLifecycleCallback('loading', 'postLoad');
  53. $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
  54. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_CUSTOM);
  55. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  56. return $metadata;
  57. }
  58. /**
  59. * @param ClassMetadataInfo $metadata
  60. * @return DocumentGeneratorBook
  61. */
  62. public function newInstance($metadata)
  63. {
  64. $path = $this->tmpDir . '/'. $this->namespace . '/DocumentGeneratorBook.php';
  65. $this->assertFileExists($path);
  66. require_once $path;
  67. return new $metadata->name;
  68. }
  69. public function testGeneratedDocumentClass()
  70. {
  71. $metadata = $this->generateBookDocumentFixture();
  72. $book = $this->newInstance($metadata);
  73. $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
  74. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', '__construct'), "DocumentGeneratorBook::__construct() missing.");
  75. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getId'), "DocumentGeneratorBook::getId() missing.");
  76. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setName'), "DocumentGeneratorBook::setName() missing.");
  77. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getName'), "DocumentGeneratorBook::getName() missing.");
  78. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setAuthor'), "DocumentGeneratorBook::setAuthor() missing.");
  79. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getAuthor'), "DocumentGeneratorBook::getAuthor() missing.");
  80. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getComments'), "DocumentGeneratorBook::getComments() missing.");
  81. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addComment'), "DocumentGeneratorBook::addComment() missing.");
  82. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeComment'), "DocumentGeneratorBook::removeComment() missing.");
  83. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getSearches'), "DocumentGeneratorBook::getSearches() missing.");
  84. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addSearch'), "DocumentGeneratorBook::addSearch() missing.");
  85. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeSearch'), "DocumentGeneratorBook::removeSearch() missing.");
  86. $book->setName('Jonathan H. Wage');
  87. $this->assertEquals('Jonathan H. Wage', $book->getName());
  88. $author = new DocumentGeneratorAuthor();
  89. $book->setAuthor($author);
  90. $this->assertEquals($author, $book->getAuthor());
  91. $comment = new DocumentGeneratorComment();
  92. $book->addComment($comment);
  93. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  94. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  95. $book->removeComment($comment);
  96. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
  97. }
  98. public function testDocumentUpdatingWorks()
  99. {
  100. $metadata = $this->generateBookDocumentFixture();
  101. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  102. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  103. $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DocumentGeneratorBook.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')->isProtected(), "Check for protected 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 testDocumentExtendsStdClass()
  117. {
  118. $this->generator->setClassToExtend('stdClass');
  119. $metadata = $this->generateBookDocumentFixture();
  120. $book = $this->newInstance($metadata);
  121. $this->assertInstanceOf('stdClass', $book);
  122. }
  123. public function testLifecycleCallbacks()
  124. {
  125. $metadata = $this->generateBookDocumentFixture();
  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->generateBookDocumentFixture();
  134. $book = $this->newInstance($metadata);
  135. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  136. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  137. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  138. $driver->loadMetadataForClass($cm->name, $cm);
  139. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  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. $metadata = $this->generateBookDocumentFixture();
  148. $book = $this->newInstance($metadata);
  149. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  150. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  151. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  152. $driver->loadMetadataForClass($cm->name, $cm);
  153. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  154. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  155. $this->assertEquals($cm->identifier, $metadata->identifier);
  156. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  157. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  158. }
  159. public function testTraitPropertiesAndMethodsAreNotDuplicated()
  160. {
  161. $cmf = $this->dm->getMetadataFactory();
  162. $user = new \Doctrine\ODM\MongoDB\Tests\Tools\GH297\User();
  163. $metadata = $cmf->getMetadataFor(get_class($user));
  164. $metadata->name = $this->namespace . "\User";
  165. $metadata->namespace = $this->namespace;
  166. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  167. $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/User.php");
  168. require $this->tmpDir . "/" . $this->namespace . "/User.php";
  169. $reflClass = new \ReflectionClass($metadata->name);
  170. $this->assertSame($reflClass->hasProperty('address'), false);
  171. $this->assertSame($reflClass->hasMethod('setAddress'), false);
  172. $this->assertSame($reflClass->hasMethod('getAddress'), false);
  173. }
  174. public function testTraitPropertiesAndMethodsAreNotDuplicatedInChildClasses()
  175. {
  176. $cmf = $this->dm->getMetadataFactory();
  177. $user = new \Doctrine\ODM\MongoDB\Tests\Tools\GH297\Admin();
  178. $metadata = $cmf->getMetadataFor(get_class($user));
  179. $metadata->name = $this->namespace . "\DDC2372Admin";
  180. $metadata->namespace = $this->namespace;
  181. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  182. $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DDC2372Admin.php");
  183. require $this->tmpDir . "/" . $this->namespace . "/DDC2372Admin.php";
  184. $reflClass = new \ReflectionClass($metadata->name);
  185. $this->assertSame($reflClass->hasProperty('address'), false);
  186. $this->assertSame($reflClass->hasMethod('setAddress'), false);
  187. $this->assertSame($reflClass->hasMethod('getAddress'), false);
  188. }
  189. /**
  190. * Tests that properties, getters and setters are not duplicated on children classes
  191. *
  192. * @see https://github.com/doctrine/mongodb-odm/issues/1299
  193. */
  194. public function testMethodsAndPropertiesAreNotDuplicatedInChildClasses()
  195. {
  196. $cmf = $this->dm->getMetadataFactory();
  197. $nsDir = $this->tmpDir.'/'.$this->namespace;
  198. // Copy GH1299User class to temp dir
  199. $content = str_replace(
  200. 'namespace Doctrine\ODM\MongoDB\Tests\Tools\GH1299',
  201. 'namespace '.$this->namespace,
  202. file_get_contents(__DIR__.'/GH1299/GH1299User.php')
  203. );
  204. $fname = $nsDir.'/GH1299User.php';
  205. file_put_contents($fname, $content);
  206. require $fname;
  207. // Generate document class
  208. $metadata = $cmf->getMetadataFor($this->namespace.'\GH1299User');
  209. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  210. // Make a copy of the generated class that does not extend the BaseUser class
  211. $source = file_get_contents($fname);
  212. // class _DDC1590User extends DDC1590Entity { ... }
  213. $source2 = str_replace('class GH1299User', 'class _GH1299User', $source);
  214. $fname2 = $nsDir.'/_DDC1590User.php';
  215. file_put_contents($fname2, $source2);
  216. require $fname2;
  217. $source3 = str_replace('class GH1299User extends BaseUser', 'class __GH1299User', $source);
  218. $fname3 = $nsDir.'/_GH1299User.php';
  219. file_put_contents($fname3, $source3);
  220. require $fname3;
  221. // The GH1299User class that extends BaseUser should have all properties, getters and setters
  222. // (some of them are inherited from BaseUser)
  223. $reflClass2 = new \ReflectionClass($this->namespace.'\_GH1299User');
  224. $this->assertTrue($reflClass2->hasProperty('id'));
  225. $this->assertTrue($reflClass2->hasProperty('name'));
  226. $this->assertTrue($reflClass2->hasProperty('lastname'));
  227. $this->assertTrue($reflClass2->hasMethod('getId'));
  228. $this->assertFalse($reflClass2->hasMethod('setId'));
  229. $this->assertTrue($reflClass2->hasMethod('getName'));
  230. $this->assertTrue($reflClass2->hasMethod('setName'));
  231. $this->assertTrue($reflClass2->hasMethod('getLastname'));
  232. $this->assertTrue($reflClass2->hasMethod('setLastname'));
  233. // The class that does not extend BaseUser should not have the properties and methods / setters
  234. // from the BaseUser class, but only its own specific ones
  235. $reflClass3 = new \ReflectionClass($this->namespace.'\__GH1299User');
  236. $this->assertFalse($reflClass3->hasProperty('id'));
  237. $this->assertFalse($reflClass3->hasProperty('name'));
  238. $this->assertTrue($reflClass3->hasProperty('lastname'));
  239. $this->assertFalse($reflClass3->hasMethod('getId'));
  240. $this->assertFalse($reflClass3->hasMethod('setId'));
  241. $this->assertFalse($reflClass3->hasMethod('getName'));
  242. $this->assertFalse($reflClass3->hasMethod('setName'));
  243. $this->assertTrue($reflClass3->hasMethod('getLastname'));
  244. $this->assertTrue($reflClass3->hasMethod('setLastname'));
  245. }
  246. }
  247. class DocumentGeneratorAuthor {}
  248. class DocumentGeneratorComment {}
  249. class DocumentGeneratorSearch {}