TestCase.php 4.5 KB

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