PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/eymengunay/mongodb-odm
PHP | 189 lines | 147 code | 37 blank | 5 comment | 9 complexity | e84476099744b8a38fb3647e6d19407f 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->addLifecycleCallback('loading', 'postLoad');
  47. $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
  48. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  49. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  50. return $metadata;
  51. }
  52. /**
  53. * @param ClassMetadataInfo $metadata
  54. * @return DocumentGeneratorBook
  55. */
  56. public function newInstance($metadata)
  57. {
  58. $path = $this->tmpDir . '/'. $this->namespace . '/DocumentGeneratorBook.php';
  59. $this->assertFileExists($path);
  60. require_once $path;
  61. return new $metadata->name;
  62. }
  63. public function testGeneratedDocumentClass()
  64. {
  65. $metadata = $this->generateBookDocumentFixture();
  66. $book = $this->newInstance($metadata);
  67. $this->assertTrue(class_exists($metadata->name), "Class does not exist.");
  68. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', '__construct'), "DocumentGeneratorBook::__construct() missing.");
  69. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getId'), "DocumentGeneratorBook::getId() missing.");
  70. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setName'), "DocumentGeneratorBook::setName() missing.");
  71. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getName'), "DocumentGeneratorBook::getName() missing.");
  72. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'setAuthor'), "DocumentGeneratorBook::setAuthor() missing.");
  73. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getAuthor'), "DocumentGeneratorBook::getAuthor() missing.");
  74. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getComments'), "DocumentGeneratorBook::getComments() missing.");
  75. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addComment'), "DocumentGeneratorBook::addComment() missing.");
  76. $this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeComment'), "EntityGeneratorBook::removeComment() missing.");
  77. $book->setName('Jonathan H. Wage');
  78. $this->assertEquals('Jonathan H. Wage', $book->getName());
  79. $author = new DocumentGeneratorAuthor();
  80. $book->setAuthor($author);
  81. $this->assertEquals($author, $book->getAuthor());
  82. $comment = new DocumentGeneratorComment();
  83. $book->addComment($comment);
  84. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  85. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  86. $book->removeComment($comment);
  87. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments());
  88. }
  89. public function testDocumentUpdatingWorks()
  90. {
  91. $metadata = $this->generateBookDocumentFixture();
  92. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  93. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  94. $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DocumentGeneratorBook.php");
  95. $book = $this->newInstance($metadata);
  96. $reflClass = new \ReflectionClass($metadata->name);
  97. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  98. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  99. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  100. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  101. $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
  102. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  103. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  104. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  105. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  106. }
  107. public function testDocumentExtendsStdClass()
  108. {
  109. $this->generator->setClassToExtend('stdClass');
  110. $metadata = $this->generateBookDocumentFixture();
  111. $book = $this->newInstance($metadata);
  112. $this->assertInstanceOf('stdClass', $book);
  113. }
  114. public function testLifecycleCallbacks()
  115. {
  116. $metadata = $this->generateBookDocumentFixture();
  117. $book = $this->newInstance($metadata);
  118. $reflClass = new \ReflectionClass($metadata->name);
  119. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  120. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  121. }
  122. public function testLoadMetadata()
  123. {
  124. $metadata = $this->generateBookDocumentFixture();
  125. $book = $this->newInstance($metadata);
  126. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  127. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  128. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  129. $driver->loadMetadataForClass($cm->name, $cm);
  130. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  131. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  132. $this->assertEquals($cm->identifier, $metadata->identifier);
  133. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  134. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  135. }
  136. public function testLoadPrefixedMetadata()
  137. {
  138. $metadata = $this->generateBookDocumentFixture();
  139. $book = $this->newInstance($metadata);
  140. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  141. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  142. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  143. $driver->loadMetadataForClass($cm->name, $cm);
  144. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  145. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  146. $this->assertEquals($cm->identifier, $metadata->identifier);
  147. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  148. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  149. }
  150. }
  151. class DocumentGeneratorAuthor {}
  152. class DocumentGeneratorComment {}