TestCase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Client;
  4. use PHPUnit\Framework\TestCase as BaseTestCase;
  5. use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
  6. abstract class TestCase extends BaseTestCase
  7. {
  8. use SetUpTearDownTrait;
  9. const INDEX_VERSION_1 = 1;
  10. const INDEX_VERSION_2 = 2;
  11. private function doTearDown()
  12. {
  13. $this->getCheckDatabase()->drop();
  14. parent::tearDown();
  15. }
  16. /**
  17. * @return \MongoDB\Client
  18. */
  19. protected function getCheckClient()
  20. {
  21. return new Client('mongodb://localhost', ['connect' => true]);
  22. }
  23. /**
  24. * @return \MongoDB\Database
  25. */
  26. protected function getCheckDatabase()
  27. {
  28. $client = $this->getCheckClient();
  29. return $client->selectDatabase('mongo-php-adapter');
  30. }
  31. /**
  32. * @param array|null $options
  33. * @return \MongoClient
  34. */
  35. protected function getClient($options = null, $uri = 'mongodb://localhost')
  36. {
  37. $args = [$uri];
  38. if ($options !== null) {
  39. $args[] = $options;
  40. }
  41. $reflection = new \ReflectionClass('MongoClient');
  42. return $reflection->newInstanceArgs($args);
  43. }
  44. /**
  45. * @param \MongoClient|null $client
  46. * @return \MongoDB
  47. */
  48. protected function getDatabase(\MongoClient $client = null)
  49. {
  50. if ($client === null) {
  51. $client = $this->getClient();
  52. }
  53. return $client->selectDB('mongo-php-adapter');
  54. }
  55. /**
  56. * @param string $name
  57. * @param \MongoDB|null $database
  58. * @return \MongoCollection
  59. */
  60. protected function getCollection($name = 'test', \MongoDB $database = null)
  61. {
  62. if ($database === null) {
  63. $database = $this->getDatabase();
  64. }
  65. return $database->selectCollection($name);
  66. }
  67. /**
  68. * @param string $prefix
  69. * @param \MongoDB|null $database
  70. * @return \MongoGridFS
  71. */
  72. protected function getGridFS($prefix = 'fs', \MongoDB $database = null)
  73. {
  74. if ($database === null) {
  75. $database = $this->getDatabase();
  76. }
  77. return $database->getGridFS($prefix);
  78. }
  79. /**
  80. * @return \MongoCollection
  81. */
  82. protected function prepareData()
  83. {
  84. $collection = $this->getCollection();
  85. $document = ['foo' => 'bar'];
  86. $collection->insert($document);
  87. unset($document['_id']);
  88. $collection->insert($document);
  89. $document = ['foo' => 'foo'];
  90. $collection->insert($document);
  91. return $collection;
  92. }
  93. protected function configureFailPoint($failPoint, $mode, $data = [])
  94. {
  95. $this->checkFailPoint();
  96. $doc = array(
  97. "configureFailPoint" => $failPoint,
  98. "mode" => $mode,
  99. );
  100. if ($data) {
  101. $doc["data"] = $data;
  102. }
  103. $adminDb = $this->getCheckClient()->selectDatabase('admin');
  104. $result = $adminDb->command($doc);
  105. $arr = current($result->toArray());
  106. if (empty($arr->ok)) {
  107. throw new \RuntimeException("Failpoint failed");
  108. }
  109. return true;
  110. }
  111. protected function checkFailPoint()
  112. {
  113. $database = $this->getCheckClient()->selectDatabase('test');
  114. try {
  115. $database->command(['configureFailPoint' => 1]);
  116. } catch (\MongoDB\Driver\Exception\Exception $e) {
  117. /* command not found */
  118. if ($e->getCode() == 59) {
  119. $this->markTestSkipped(
  120. 'This test require the mongo daemon to be started with the test flag: --setParameter enableTestCommands=1'
  121. );
  122. }
  123. }
  124. }
  125. protected function failMaxTimeMS()
  126. {
  127. return $this->configureFailPoint("maxTimeAlwaysTimeOut", array("times" => 1));
  128. }
  129. /**
  130. * @param bool $condition
  131. */
  132. protected function skipTestUnless($condition, $message = null)
  133. {
  134. $this->skipTestIf(! $condition, $message);
  135. }
  136. /**
  137. * @param bool $condition
  138. * @param string|null $message
  139. */
  140. protected function skipTestIf($condition, $message = null)
  141. {
  142. if ($condition) {
  143. $this->markTestSkipped($message !== null ? $message : 'Test only applies when running against mongo-php-adapter');
  144. }
  145. }
  146. /**
  147. * @return string
  148. */
  149. protected function getServerVersion()
  150. {
  151. $serverInfo = $this->getDatabase()->command(['buildinfo' => true]);
  152. return $serverInfo['version'];
  153. }
  154. /**
  155. * @return string
  156. */
  157. protected function getFeatureCompatibilityVersion()
  158. {
  159. $featureCompatibilityVersion = $this->getClient()->selectDB('admin')->command(['getParameter' => true, 'featureCompatibilityVersion' => true]);
  160. if (! isset($featureCompatibilityVersion['featureCompatibilityVersion'])) {
  161. return '3.2';
  162. }
  163. return isset($featureCompatibilityVersion['featureCompatibilityVersion']['version']) ? $featureCompatibilityVersion['featureCompatibilityVersion']['version'] : $featureCompatibilityVersion['featureCompatibilityVersion'];
  164. }
  165. /**
  166. * Indexes created in MongoDB 3.4 default to v: 2.
  167. * @return int
  168. * @see https://docs.mongodb.com/manual/release-notes/3.4-compatibility/#backwards-incompatible-features
  169. */
  170. protected function getDefaultIndexVersion()
  171. {
  172. if (version_compare($this->getServerVersion(), '3.4.0', '<')) {
  173. self::INDEX_VERSION_1;
  174. }
  175. // Check featureCompatibilityFlag
  176. $compatibilityVersion = $this->getFeatureCompatibilityVersion();
  177. return version_compare($compatibilityVersion, '3.4', '>=') ? self::INDEX_VERSION_2 : self::INDEX_VERSION_1;
  178. }
  179. }