BaseTestCaseMongoODM.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Tool;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
  5. use Doctrine\ODM\MongoDB\DocumentManager;
  6. use Doctrine\Common\EventManager;
  7. use Doctrine\MongoDB\Connection;
  8. use Gedmo\Translatable\TranslatableListener;
  9. use Gedmo\Sluggable\SluggableListener;
  10. use Gedmo\Timestampable\TimestampableListener;
  11. use Gedmo\SoftDeleteable\SoftDeleteableListener;
  12. use Gedmo\Loggable\LoggableListener;
  13. /**
  14. * Base test case contains common mock objects
  15. * and functionality among all extensions using
  16. * ORM object manager
  17. *
  18. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  19. * @link http://www.gediminasm.org
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. abstract class BaseTestCaseMongoODM extends \PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var DocumentManager
  26. */
  27. protected $dm;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function setUp()
  32. {
  33. if (!class_exists('Mongo')) {
  34. $this->markTestSkipped('Missing Mongo extension.');
  35. }
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function tearDown()
  41. {
  42. if ($this->dm) {
  43. foreach ($this->dm->getDocumentDatabases() as $db) {
  44. foreach ($db->listCollections() as $collection) {
  45. $collection->drop();
  46. }
  47. }
  48. $this->dm->getConnection()->close();
  49. $this->dm = null;
  50. }
  51. }
  52. /**
  53. * DocumentManager mock object together with
  54. * annotation mapping driver and database
  55. *
  56. * @param EventManager $evm
  57. * @return DocumentManager
  58. */
  59. protected function getMockDocumentManager(EventManager $evm = null, $config = null)
  60. {
  61. $conn = new Connection();
  62. $config = $config ? $config : $this->getMockAnnotatedConfig();
  63. try {
  64. $this->dm = DocumentManager::create($conn, $config, $evm ?: $this->getEventManager());
  65. $this->dm->getConnection()->connect();
  66. } catch (\MongoException $e) {
  67. $this->markTestSkipped('Doctrine MongoDB ODM failed to connect');
  68. }
  69. return $this->dm;
  70. }
  71. /**
  72. * DocumentManager mock object with
  73. * annotation mapping driver
  74. *
  75. * @param EventManager $evm
  76. * @return DocumentManager
  77. */
  78. protected function getMockMappedDocumentManager(EventManager $evm = null, $config = null)
  79. {
  80. $conn = $this->getMock('Doctrine\\MongoDB\\Connection');
  81. $config = $config ? $config : $this->getMockAnnotatedConfig();
  82. $this->dm = DocumentManager::create($conn, $config, $evm ?: $this->getEventManager());
  83. return $this->dm;
  84. }
  85. /**
  86. * Creates default mapping driver
  87. *
  88. * @return \Doctrine\ORM\Mapping\Driver\Driver
  89. */
  90. protected function getMetadataDriverImplementation()
  91. {
  92. return new AnnotationDriver($_ENV['annotation_reader']);
  93. }
  94. /**
  95. * Build event manager
  96. *
  97. * @return EventManager
  98. */
  99. private function getEventManager()
  100. {
  101. $evm = new EventManager;
  102. $evm->addEventSubscriber(new SluggableListener);
  103. $evm->addEventSubscriber(new LoggableListener);
  104. $evm->addEventSubscriber(new TranslatableListener);
  105. $evm->addEventSubscriber(new TimestampableListener);
  106. $evm->addEventSubscriber(new SoftDeleteableListener());
  107. return $evm;
  108. }
  109. /**
  110. * Get annotation mapping configuration
  111. *
  112. * @return Doctrine\ORM\Configuration
  113. */
  114. protected function getMockAnnotatedConfig()
  115. {
  116. $config = $this->getMock('Doctrine\\ODM\\MongoDB\\Configuration');
  117. $config->expects($this->any())
  118. ->method('getFilterClassName')
  119. ->will($this->returnValue('Gedmo\\SoftDeleteable\\Filter\\ODM\\SoftDeleteableFilter'));
  120. $config->expects($this->once())
  121. ->method('getProxyDir')
  122. ->will($this->returnValue(__DIR__.'/../../temp'));
  123. $config->expects($this->once())
  124. ->method('getProxyNamespace')
  125. ->will($this->returnValue('Proxy'));
  126. $config->expects($this->once())
  127. ->method('getHydratorDir')
  128. ->will($this->returnValue(__DIR__.'/../../temp'));
  129. $config->expects($this->once())
  130. ->method('getHydratorNamespace')
  131. ->will($this->returnValue('Hydrator'));
  132. $config->expects($this->any())
  133. ->method('getDefaultDB')
  134. ->will($this->returnValue('gedmo_extensions_test'));
  135. $config->expects($this->once())
  136. ->method('getAutoGenerateProxyClasses')
  137. ->will($this->returnValue(true));
  138. $config->expects($this->once())
  139. ->method('getAutoGenerateHydratorClasses')
  140. ->will($this->returnValue(true));
  141. $config->expects($this->once())
  142. ->method('getClassMetadataFactoryName')
  143. ->will($this->returnValue('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadataFactory'));
  144. $config
  145. ->expects($this->any())
  146. ->method('getMongoCmd')
  147. ->will($this->returnValue('$'))
  148. ;
  149. $config
  150. ->expects($this->any())
  151. ->method('getDefaultCommitOptions')
  152. ->will($this->returnValue(array('safe' => true)))
  153. ;
  154. $mappingDriver = $this->getMetadataDriverImplementation();
  155. $config->expects($this->any())
  156. ->method('getMetadataDriverImpl')
  157. ->will($this->returnValue($mappingDriver));
  158. return $config;
  159. }
  160. }