MongoDBTest.php 10 KB

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