TestCase.php 5.9 KB

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