MongoDBTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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->assertFalse($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. $this->setExpectedException('MongoCursorTimeoutException');
  85. $database->command([
  86. "count" => "test",
  87. "query" => array("a" => 1),
  88. "maxTimeMS" => 100,
  89. ]);
  90. }
  91. public function testReadPreference()
  92. {
  93. $database = $this->getDatabase();
  94. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  95. $this->assertFalse($database->getSlaveOkay());
  96. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  97. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $database->getReadPreference());
  98. $this->assertTrue($database->getSlaveOkay());
  99. $this->assertTrue($database->setSlaveOkay(true));
  100. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $database->getReadPreference());
  101. $this->assertTrue($database->setSlaveOkay(false));
  102. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  103. }
  104. public function testReadPreferenceIsSetInDriver()
  105. {
  106. $this->skipTestIf(extension_loaded('mongo'));
  107. $database = $this->getDatabase();
  108. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  109. // Only way to check whether options are passed down is through debugInfo
  110. $readPreference = $database->getDb()->__debugInfo()['readPreference'];
  111. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  112. $this->assertSame(['a' => 'b'], $readPreference->getTagSets());
  113. }
  114. public function testReadPreferenceIsInherited()
  115. {
  116. $client = $this->getClient();
  117. $client->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  118. $database = $client->selectDB('test');
  119. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $database->getReadPreference());
  120. }
  121. public function testWriteConcern()
  122. {
  123. $database = $this->getDatabase();
  124. $this->assertSame(['w' => 1, 'wtimeout' => 0], $database->getWriteConcern());
  125. $this->assertSame(1, $database->w);
  126. $this->assertSame(0, $database->wtimeout);
  127. $this->assertTrue($database->setWriteConcern('majority', 100));
  128. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  129. $database->w = 2;
  130. $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
  131. $database->wtimeout = -1;
  132. $this->assertSame(['w' => 2, 'wtimeout' => 0], $database->getWriteConcern());
  133. // Only way to check whether options are passed down is through debugInfo
  134. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  135. $this->assertSame(2, $writeConcern->getW());
  136. $this->assertSame(0, $writeConcern->getWtimeout());
  137. }
  138. public function testWriteConcernIsSetInDriver()
  139. {
  140. $this->skipTestIf(extension_loaded('mongo'));
  141. $database = $this->getDatabase();
  142. $this->assertTrue($database->setWriteConcern(2, 100));
  143. // Only way to check whether options are passed down is through debugInfo
  144. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  145. $this->assertSame(2, $writeConcern->getW());
  146. $this->assertSame(100, $writeConcern->getWtimeout());
  147. }
  148. public function testWriteConcernIsInherited()
  149. {
  150. $client = $this->getClient();
  151. $client->setWriteConcern('majority', 100);
  152. $database = $client->selectDB('test');
  153. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  154. }
  155. public function testProfilingLevel()
  156. {
  157. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->getProfilingLevel());
  158. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_SLOW));
  159. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->getProfilingLevel());
  160. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON));
  161. $this->assertSame(\MongoDB::PROFILING_ON, $this->getDatabase()->getProfilingLevel());
  162. }
  163. public function testForceError()
  164. {
  165. $result = $this->getDatabase()->forceError();
  166. $this->assertSame(0, $result['ok']);
  167. }
  168. public function testExecute()
  169. {
  170. $db = $this->getDatabase();
  171. $document = ['foo' => 'bar'];
  172. $this->getCollection()->insert($document);
  173. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  174. }
  175. public function testGetCollectionNames()
  176. {
  177. $document = ['foo' => 'bar'];
  178. $this->getCollection()->insert($document);
  179. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  180. }
  181. public function testGetCollectionNamesExecutionTimeoutException()
  182. {
  183. $document = ['foo' => 'bar'];
  184. $this->getCollection()->insert($document);
  185. $database = $this->getDatabase();
  186. $this->failMaxTimeMS();
  187. $this->setExpectedException('MongoExecutionTimeoutException');
  188. $database->getCollectionNames(['maxTimeMS' => 1]);
  189. }
  190. public function testGetCollectionInfo()
  191. {
  192. $document = ['foo' => 'bar'];
  193. $this->getCollection()->insert($document);
  194. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  195. if ($collectionInfo['name'] === 'test') {
  196. $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
  197. return;
  198. }
  199. }
  200. $this->fail('The test collection was not found');
  201. }
  202. public function testGetCollectionInfoExecutionTimeoutException()
  203. {
  204. $document = ['foo' => 'bar'];
  205. $this->getCollection()->insert($document);
  206. $database = $this->getDatabase();
  207. $this->failMaxTimeMS();
  208. $this->setExpectedException('MongoExecutionTimeoutException');
  209. $database->getCollectionInfo(['maxTimeMS' => 1]);
  210. }
  211. public function testListCollections()
  212. {
  213. $document = ['foo' => 'bar'];
  214. $this->getCollection()->insert($document);
  215. foreach ($this->getDatabase()->listCollections() as $collection) {
  216. $this->assertInstanceOf('MongoCollection', $collection);
  217. if ($collection->getName() === 'test') {
  218. return;
  219. }
  220. }
  221. $this->fail('The test collection was not found');
  222. }
  223. public function testListCollectionsExecutionTimeoutException()
  224. {
  225. $this->failMaxTimeMS();
  226. $this->setExpectedException('MongoExecutionTimeoutException');
  227. $this->getDatabase()->listCollections(['maxTimeMS' => 1]);
  228. }
  229. public function testDrop()
  230. {
  231. $document = ['foo' => 'bar'];
  232. $this->getCollection()->insert($document);
  233. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  234. }
  235. public function testDropCollection()
  236. {
  237. $document = ['foo' => 'bar'];
  238. $this->getCollection()->insert($document);
  239. $expected = [
  240. 'ns' => (string) $this->getCollection(),
  241. 'nIndexesWas' => 1,
  242. 'ok' => 1.0
  243. ];
  244. $this->assertSame($expected, $this->getDatabase()->dropCollection('test'));
  245. }
  246. }