MongoDBTest.php 12 KB

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