MongoDBTest.php 10 KB

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