TestCase.php 3.9 KB

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