PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/taposh/mongodb-odm
PHP | 186 lines | 144 code | 37 blank | 5 comment | 9 complexity | 9fa1cd6a0ee19b80c1dbfbede946203f 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', 'addComments'), "DocumentGeneratorBook::addComments() missing.");
  76. $book->setName('Jonathan H. Wage');
  77. $this->assertEquals('Jonathan H. Wage', $book->getName());
  78. $author = new DocumentGeneratorAuthor();
  79. $book->setAuthor($author);
  80. $this->assertEquals($author, $book->getAuthor());
  81. $comment = new DocumentGeneratorComment();
  82. $book->addComments($comment);
  83. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
  84. $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
  85. }
  86. public function testDocumentUpdatingWorks()
  87. {
  88. $metadata = $this->generateBookDocumentFixture();
  89. $metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));
  90. $this->generator->writeDocumentClass($metadata, $this->tmpDir);
  91. $this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DocumentGeneratorBook.php");
  92. $book = $this->newInstance($metadata);
  93. $reflClass = new \ReflectionClass($metadata->name);
  94. $this->assertTrue($reflClass->hasProperty('name'), "Regenerating keeps property 'name'.");
  95. $this->assertTrue($reflClass->hasProperty('status'), "Regenerating keeps property 'status'.");
  96. $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'.");
  97. $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed.");
  98. $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed.");
  99. $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed.");
  100. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  101. $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");
  102. $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed.");
  103. }
  104. public function testDocumentExtendsStdClass()
  105. {
  106. $this->generator->setClassToExtend('stdClass');
  107. $metadata = $this->generateBookDocumentFixture();
  108. $book = $this->newInstance($metadata);
  109. $this->assertInstanceOf('stdClass', $book);
  110. }
  111. public function testLifecycleCallbacks()
  112. {
  113. $metadata = $this->generateBookDocumentFixture();
  114. $book = $this->newInstance($metadata);
  115. $reflClass = new \ReflectionClass($metadata->name);
  116. $this->assertTrue($reflClass->hasMethod('loading'), "Check for postLoad lifecycle callback.");
  117. $this->assertTrue($reflClass->hasMethod('willBeRemoved'), "Check for preRemove lifecycle callback.");
  118. }
  119. public function testLoadMetadata()
  120. {
  121. $metadata = $this->generateBookDocumentFixture();
  122. $book = $this->newInstance($metadata);
  123. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  124. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  125. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  126. $driver->loadMetadataForClass($cm->name, $cm);
  127. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  128. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  129. $this->assertEquals($cm->identifier, $metadata->identifier);
  130. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  131. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  132. }
  133. public function testLoadPrefixedMetadata()
  134. {
  135. $metadata = $this->generateBookDocumentFixture();
  136. $book = $this->newInstance($metadata);
  137. $cm = new \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo($metadata->name);
  138. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  139. $driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader);
  140. $driver->loadMetadataForClass($cm->name, $cm);
  141. $this->assertEquals($cm->getCollection(), $metadata->getCollection());
  142. $this->assertEquals($cm->lifecycleCallbacks, $metadata->lifecycleCallbacks);
  143. $this->assertEquals($cm->identifier, $metadata->identifier);
  144. $this->assertEquals($cm->idGenerator, $metadata->idGenerator);
  145. $this->assertEquals($cm->customRepositoryClassName, $metadata->customRepositoryClassName);
  146. }
  147. }
  148. class DocumentGeneratorAuthor {}
  149. class DocumentGeneratorComment {}