BaseTestCaseMongoODM.php 4.5 KB

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