MongoDBTest.php 11 KB

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