TestCase.php 5.2 KB

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