BaseTestCaseMongoODM.php 5.5KB

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