MongoDBTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\ReadPreference;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoDBTest extends TestCase
  8. {
  9. public function testGetCollection()
  10. {
  11. $db = $this->getDatabase();
  12. $collection = $db->selectCollection('test');
  13. $this->assertInstanceOf('MongoCollection', $collection);
  14. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  15. }
  16. public function testSelectCollectionEmptyName()
  17. {
  18. $database = $this->getDatabase();
  19. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  20. $database->selectCollection('');
  21. }
  22. public function testSelectCollectionWithNullBytes()
  23. {
  24. $database = $this->getDatabase();
  25. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  26. $database->selectCollection('foo' . chr(0));
  27. }
  28. public function testCreateCollection()
  29. {
  30. $database = $this->getDatabase();
  31. $collection = $database->createCollection('test', ['capped' => true, 'size' => 100]);
  32. $this->assertInstanceOf('MongoCollection', $collection);
  33. $document = ['foo' => 'bar'];
  34. $collection->insert($document);
  35. $checkDatabase = $this->getCheckDatabase();
  36. foreach ($checkDatabase->listCollections() as $collectionInfo) {
  37. if ($collectionInfo->getName() === 'test') {
  38. $this->assertTrue($collectionInfo->isCapped());
  39. return;
  40. }
  41. }
  42. }
  43. public function testCreateCollectionInvalidParameters()
  44. {
  45. $database = $this->getDatabase();
  46. $this->assertFalse($database->createCollection('test', ['capped' => 2, 'size' => 100]));
  47. }
  48. public function testGetCollectionProperty()
  49. {
  50. $db = $this->getDatabase();
  51. $collection = $db->test;
  52. $this->assertInstanceOf('MongoCollection', $collection);
  53. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  54. }
  55. public function testCommand()
  56. {
  57. $db = $this->getDatabase();
  58. $this->assertEquals(['ok' => 1], $db->command(['ping' => 1]));
  59. }
  60. public function testCommandError()
  61. {
  62. $db = $this->getDatabase();
  63. $expected = [
  64. 'ok' => 0,
  65. 'errmsg' => 'listDatabases may only be run against the admin database.',
  66. 'code' => 13,
  67. ];
  68. $this->assertEquals($expected, $db->command(['listDatabases' => 1]));
  69. }
  70. public function testCommandCursorTimeout()
  71. {
  72. $database = $this->getDatabase();
  73. $this->failMaxTimeMS();
  74. $this->setExpectedException('MongoCursorTimeoutException');
  75. $database->command([
  76. "count" => "test",
  77. "query" => array("a" => 1),
  78. "maxTimeMS" => 100,
  79. ]);
  80. }
  81. public function testReadPreference()
  82. {
  83. $database = $this->getDatabase();
  84. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  85. $this->assertFalse($database->getSlaveOkay());
  86. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  87. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  88. $this->assertTrue($database->getSlaveOkay());
  89. // Only way to check whether options are passed down is through debugInfo
  90. $writeConcern = $database->getDb()->__debugInfo()['readPreference'];
  91. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  92. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  93. $this->assertTrue($database->setSlaveOkay(true));
  94. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  95. $this->assertTrue($database->setSlaveOkay(false));
  96. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  97. }
  98. public function testReadPreferenceIsInherited()
  99. {
  100. $client = $this->getClient();
  101. $client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  102. $database = $client->selectDB('test');
  103. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  104. }
  105. public function testWriteConcern()
  106. {
  107. $database = $this->getDatabase();
  108. $this->assertSame(['w' => 1, 'wtimeout' => 0], $database->getWriteConcern());
  109. $this->assertSame(1, $database->w);
  110. $this->assertSame(0, $database->wtimeout);
  111. $this->assertTrue($database->setWriteConcern('majority', 100));
  112. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  113. $database->w = 2;
  114. $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
  115. $database->wtimeout = -1;
  116. $this->assertSame(['w' => 2, 'wtimeout' => 0], $database->getWriteConcern());
  117. // Only way to check whether options are passed down is through debugInfo
  118. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  119. $this->assertSame(2, $writeConcern->getW());
  120. $this->assertSame(0, $writeConcern->getWtimeout());
  121. }
  122. public function testWriteConcernIsInherited()
  123. {
  124. $client = $this->getClient();
  125. $client->setWriteConcern('majority', 100);
  126. $database = $client->selectDB('test');
  127. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  128. }
  129. public function testProfilingLevel()
  130. {
  131. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->getProfilingLevel());
  132. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_SLOW));
  133. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->getProfilingLevel());
  134. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON));
  135. $this->assertSame(\MongoDB::PROFILING_ON, $this->getDatabase()->getProfilingLevel());
  136. }
  137. public function testForceError()
  138. {
  139. $result = $this->getDatabase()->forceError();
  140. $this->assertSame(0, $result['ok']);
  141. }
  142. public function testExecute()
  143. {
  144. $db = $this->getDatabase();
  145. $document = ['foo' => 'bar'];
  146. $this->getCollection()->insert($document);
  147. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  148. }
  149. public function testGetCollectionNames()
  150. {
  151. $document = ['foo' => 'bar'];
  152. $this->getCollection()->insert($document);
  153. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  154. }
  155. public function testGetCollectionNamesExecutionTimeoutException()
  156. {
  157. $document = ['foo' => 'bar'];
  158. $this->getCollection()->insert($document);
  159. $database = $this->getDatabase();
  160. $this->failMaxTimeMS();
  161. $this->setExpectedException('MongoExecutionTimeoutException');
  162. $database->getCollectionNames(['maxTimeMS' => 1]);
  163. }
  164. public function testGetCollectionInfo()
  165. {
  166. $document = ['foo' => 'bar'];
  167. $this->getCollection()->insert($document);
  168. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  169. if ($collectionInfo['name'] === 'test') {
  170. $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
  171. return;
  172. }
  173. }
  174. $this->fail('The test collection was not found');
  175. }
  176. public function testGetCollectionInfoExecutionTimeoutException()
  177. {
  178. $document = ['foo' => 'bar'];
  179. $this->getCollection()->insert($document);
  180. $database = $this->getDatabase();
  181. $this->failMaxTimeMS();
  182. $this->setExpectedException('MongoExecutionTimeoutException');
  183. $database->getCollectionInfo(['maxTimeMS' => 1]);
  184. }
  185. public function testListCollections()
  186. {
  187. $document = ['foo' => 'bar'];
  188. $this->getCollection()->insert($document);
  189. foreach ($this->getDatabase()->listCollections() as $collection) {
  190. $this->assertInstanceOf('MongoCollection', $collection);
  191. if ($collection->getName() === 'test') {
  192. return;
  193. }
  194. }
  195. $this->fail('The test collection was not found');
  196. }
  197. public function testListCollectionsExecutionTimeoutException()
  198. {
  199. $this->failMaxTimeMS();
  200. $this->setExpectedException('MongoExecutionTimeoutException');
  201. $this->getDatabase()->listCollections(['maxTimeMS' => 1]);
  202. }
  203. public function testDrop()
  204. {
  205. $document = ['foo' => 'bar'];
  206. $this->getCollection()->insert($document);
  207. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  208. }
  209. public function testDropCollection()
  210. {
  211. $document = ['foo' => 'bar'];
  212. $this->getCollection()->insert($document);
  213. $expected = [
  214. 'ns' => (string) $this->getCollection(),
  215. 'nIndexesWas' => 1,
  216. 'ok' => 1.0
  217. ];
  218. $this->assertSame($expected, $this->getDatabase()->dropCollection('test'));
  219. }
  220. }