BaseTestCaseMongoODM.php 5.2 KB

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