PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/CrAzE124/mongodb-odm
PHP | 197 lines | 155 code | 37 blank | 5 comment | 9 complexity | ad664ac729e3396a9ae3864385b37233 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. $this->namespace = uniqid("doctrine_");
  13. $this->tmpDir = \sys_get_temp_dir();
  14. \mkdir($this->tmpDir . \DIRECTORY_SEPARATOR . $this->namespace);
  15. $this->generator = new DocumentGenerator();
  16. $this->generator->setGenerateAnnotations(true);
  17. $this->generator->setGenerateStubMethods(true);
  18. $this->generator->setRegenerateDocumentIfExists(false);
  19. $this->generator->setUpdateDocumentIfExists(true);
  20. }
  21. public function tearDown()
  22. {
  23. $ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir . '/' . $this->namespace));
  24. foreach ($ri AS $file) {
  25. /* @var $file \SplFileInfo */
  26. if ($file->isFile()) {
  27. \unlink($file->getPathname());
  28. }
  29. }
  30. rmdir($this->tmpDir . '/' . $this->namespace);
  31. }
  32. public function generateBookDocumentFixture()
  33. {
  34. $metadata = new ClassMetadataInfo($this->namespace . '\DocumentGeneratorBook');
  35. $metadata->namespace = $this->namespace;
  36. $metadata->customRepositoryClassName = $this->namespace . '\DocumentGeneratorBookRepository';
  37. $metadata->collection = 'book';
  38. $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
  39. $metadata->mapField(array('fieldName' => 'status', 'type' => 'string'));
  40. $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  41. $metadata->mapOneReference(array('fieldName' => 'author', 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorAuthor'));
  42. $metadata->mapManyReference(array(
  43. 'fieldName' => 'comments',
  44. 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorComment'
  45. ));
  46. $metadata->mapManyReference(array(
  47. 'fieldName' => 'searches',
  48. 'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorSearch'
  49. ));
  50. $metadata->addLifecycleCallback('loading', 'postLoad');
  51. $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
  52. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_CUSTOM);
  53. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  54. return $metadata;
  55. }
  56. /**
  57. * @param ClassMetadataInfo $metadata
  58. * @return DocumentGeneratorBook
  59. */
  60. public function newInstance($metadata)
  61. {
  62. $path = $this->tmpDir . '/'. $this->namespace . '/DocumentGeneratorBook.php';
  63. $this->assertFileExists($path);
  64. require_once $path;
  65. return new $metadata->name;
  66. }
  67. public function testGeneratedDocumentClass()
  68. {
  69. $metadata = $this->generateBookDocumentFixture();
  70. $book = $this->newInstance($metadata);
  71. $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
  72. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', '__construct'), "DocumentGeneratorBook::__construct() missing.");
  73. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getId'), "DocumentGeneratorBook::getId() missing.");
  74. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setName'), "DocumentGeneratorBook::setName() missing.");
  75. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getName'), "DocumentGeneratorBook::getName() missing.");
  76. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setAuthor'), "DocumentGeneratorBook::setAuthor() missing.");
  77. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getAuthor'), "DocumentGeneratorBook::getAuthor() missing.");
  78. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getComments'), "DocumentGeneratorBook::getComments() missing.");
  79. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addComment'), "DocumentGeneratorBook::addComment() missing.");
  80. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeComment'), "DocumentGeneratorBook::removeComment() missing.");
  81. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getSearches'), "DocumentGeneratorBook::getSearches() missing.");
  82. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addSearch'), "DocumentGeneratorBook::addSearch() missing.");
  83. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeSearch'), "DocumentGeneratorBook::removeSearch() missing.");
  84. $book->setName('Jonathan H. Wage');
  85. $this->assertEquals('Jonathan H. Wage', $book->getName());
  86. $author = new DocumentGeneratorAuthor();
  87. $book->setAuthor($author);
  88. $this->assertEquals($author, $book->getAuthor());
  89. $comment = new DocumentGeneratorComment();
  90. $book->addComment($comment);
  91. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  92. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  93. $book->removeComment($comment);
  94. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
  95. }
  96. public function testDocumentUpdatingWorks()
  97. {
  98. $metadata = $this->generateBookDocumentFixture();
  99. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  100. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  101. $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DocumentGeneratorBook.php");
  102. $book = $this->newInstance($metadata);
  103. $reflClass = new \ReflectionClass($metadata->name);
  104. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  105. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  106. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  107. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  108. $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
  109. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  110. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  111. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  112. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  113. }
  114. public function testDocumentExtendsStdClass()
  115. {
  116. $this->generator->setClassToExtend('stdClass');
  117. $metadata = $this->generateBookDocumentFixture();
  118. $book = $this->newInstance($metadata);
  119. $this->assertInstanceOf('stdClass', $book);
  120. }
  121. public function testLifecycleCallbacks()
  122. {
  123. $metadata = $this->generateBookDocumentFixture();
  124. $book = $this->newInstance($metadata);
  125. $reflClass = new \ReflectionClass($metadata->name);
  126. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  127. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  128. }
  129. public function testLoadMetadata()
  130. {
  131. $metadata = $this->generateBookDocumentFixture();
  132. $book = $this->newInstance($metadata);
  133. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  134. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  135. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  136. $driver->loadMetadataForClass($cm->name, $cm);
  137. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  138. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  139. $this->assertEquals($cm->identifier, $metadata->identifier);
  140. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  141. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  142. }
  143. public function testLoadPrefixedMetadata()
  144. {
  145. $metadata = $this->generateBookDocumentFixture();
  146. $book = $this->newInstance($metadata);
  147. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  148. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  149. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  150. $driver->loadMetadataForClass($cm->name, $cm);
  151. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  152. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  153. $this->assertEquals($cm->identifier, $metadata->identifier);
  154. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  155. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  156. }
  157. }
  158. class DocumentGeneratorAuthor {}
  159. class DocumentGeneratorComment {}
  160. class DocumentGeneratorSearch {}