MongoDBTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
  102. $this->assertArraySubset($expected, $db->command(['listDatabases' => 1]));
  103. }
  104. public function testCommandCursorTimeout()
  105. {
  106. $database = $this->getDatabase();
  107. $this->failMaxTimeMS();
  108. $result = $database->command([
  109. "count" => "test",
  110. "query" => array("a" => 1),
  111. "maxTimeMS" => 100,
  112. ]);
  113. $this->assertSame([
  114. 'ok' => 0.0,
  115. 'errmsg' => 'operation exceeded time limit',
  116. 'code' => 50,
  117. ], $result);
  118. }
  119. public function testReadPreference()
  120. {
  121. $database = $this->getDatabase();
  122. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  123. $this->assertFalse($database->getSlaveOkay());
  124. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  125. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $database->getReadPreference());
  126. $this->assertTrue($database->getSlaveOkay());
  127. $this->assertTrue($database->setSlaveOkay(true));
  128. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $database->getReadPreference());
  129. $this->assertTrue($database->setSlaveOkay(false));
  130. // Only test a subset since we don't keep tagsets around for RP_PRIMARY
  131. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  132. }
  133. public function testReadPreferenceIsSetInDriver()
  134. {
  135. $this->skipTestIf(extension_loaded('mongo'));
  136. $database = $this->getDatabase();
  137. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  138. // Only way to check whether options are passed down is through debugInfo
  139. $readPreference = $database->getDb()->__debugInfo()['readPreference'];
  140. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  141. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  142. }
  143. public function testReadPreferenceIsInherited()
  144. {
  145. $client = $this->getClient();
  146. $client->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  147. $database = $client->selectDB('test');
  148. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $database->getReadPreference());
  149. }
  150. public function testWriteConcern()
  151. {
  152. $database = $this->getDatabase();
  153. $this->assertTrue($database->setWriteConcern('majority', 100));
  154. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  155. }
  156. public function testWriteConcernIsSetInDriver()
  157. {
  158. $this->skipTestIf(extension_loaded('mongo'));
  159. $database = $this->getDatabase();
  160. $this->assertTrue($database->setWriteConcern(2, 100));
  161. // Only way to check whether options are passed down is through debugInfo
  162. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  163. $this->assertSame(2, $writeConcern->getW());
  164. $this->assertSame(100, $writeConcern->getWtimeout());
  165. }
  166. public function testWriteConcernIsInherited()
  167. {
  168. $client = $this->getClient();
  169. $client->setWriteConcern(2, 100);
  170. $database = $client->selectDB('test');
  171. $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
  172. }
  173. public function testProfilingLevel()
  174. {
  175. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->getProfilingLevel());
  176. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_SLOW));
  177. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->getProfilingLevel());
  178. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON));
  179. $this->assertSame(\MongoDB::PROFILING_ON, $this->getDatabase()->getProfilingLevel());
  180. }
  181. public function testForceError()
  182. {
  183. $result = $this->getDatabase()->forceError();
  184. $this->assertSame(0.0, $result['ok']);
  185. }
  186. public function testExecute()
  187. {
  188. $this->skipTestIf(version_compare($this->getServerVersion(), '4.2.0', '>='), 'Eval no longer works on MongoDB 4.2.0 and newer');
  189. $db = $this->getDatabase();
  190. $document = ['foo' => 'bar'];
  191. $this->getCollection()->insert($document);
  192. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  193. }
  194. public function testGetCollectionNames()
  195. {
  196. $document = ['foo' => 'bar'];
  197. $this->getCollection()->insert($document);
  198. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  199. }
  200. public function testGetCollectionNamesExecutionTimeoutException()
  201. {
  202. $document = ['foo' => 'bar'];
  203. $this->getCollection()->insert($document);
  204. $database = $this->getDatabase();
  205. $this->failMaxTimeMS();
  206. $this->expectException(\MongoExecutionTimeoutException::class);
  207. $database->getCollectionNames(['maxTimeMS' => 1]);
  208. }
  209. public function testGetCollectionInfo()
  210. {
  211. $document = ['foo' => 'bar'];
  212. $this->getCollection()->insert($document);
  213. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  214. if ($collectionInfo['name'] === 'test') {
  215. $expected = [
  216. 'name' => 'test',
  217. 'options' => []
  218. ];
  219. if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
  220. $expected += [
  221. 'type' => 'collection',
  222. 'info' => ['readOnly' => false],
  223. 'idIndex' => [
  224. 'v' => $this->getDefaultIndexVersion(),
  225. 'key' => ['_id' => 1],
  226. 'name' => '_id_',
  227. 'ns' => (string) $this->getCollection(),
  228. ],
  229. ];
  230. }
  231. $this->assertArraySubset($expected, $collectionInfo);
  232. return;
  233. }
  234. }
  235. $this->fail('The test collection was not found');
  236. }
  237. public function testGetCollectionInfoExecutionTimeoutException()
  238. {
  239. $document = ['foo' => 'bar'];
  240. $this->getCollection()->insert($document);
  241. $database = $this->getDatabase();
  242. $this->failMaxTimeMS();
  243. $this->expectException(\MongoExecutionTimeoutException::class);
  244. $database->getCollectionInfo(['maxTimeMS' => 1]);
  245. }
  246. public function testListCollections()
  247. {
  248. $document = ['foo' => 'bar'];
  249. $this->getCollection()->insert($document);
  250. foreach ($this->getDatabase()->listCollections() as $collection) {
  251. $this->assertInstanceOf('MongoCollection', $collection);
  252. if ($collection->getName() === 'test') {
  253. return;
  254. }
  255. }
  256. $this->fail('The test collection was not found');
  257. }
  258. public function testGetCollectionNamesDoesNotListSystemCollections()
  259. {
  260. // Enable profiling to ensure we have a system.profile collection
  261. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON);
  262. try {
  263. $document = ['foo' => 'bar'];
  264. $this->getCollection()->insert($document);
  265. $collectionNames = $this->getDatabase()->getCollectionNames();
  266. $this->assertNotContains('system.profile', $collectionNames);
  267. } finally {
  268. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_OFF);
  269. }
  270. }
  271. public function testGetCollectionNamesWithSystemCollections()
  272. {
  273. // Enable profiling to ensure we have a system.profile collection
  274. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON);
  275. try {
  276. $document = ['foo' => 'bar'];
  277. $this->getCollection()->insert($document);
  278. $collectionNames = $this->getDatabase()->getCollectionNames(['includeSystemCollections' => true]);
  279. $this->assertContains('system.profile', $collectionNames);
  280. } finally {
  281. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_OFF);
  282. }
  283. }
  284. public function testListCollectionsExecutionTimeoutException()
  285. {
  286. $this->failMaxTimeMS();
  287. $this->expectException(\MongoExecutionTimeoutException::class);
  288. $this->getDatabase()->listCollections(['maxTimeMS' => 1]);
  289. }
  290. public function testDrop()
  291. {
  292. $document = ['foo' => 'bar'];
  293. $this->getCollection()->insert($document);
  294. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  295. }
  296. public function testDropCollection()
  297. {
  298. $document = ['foo' => 'bar'];
  299. $this->getCollection()->insert($document);
  300. $expected = [
  301. 'ns' => (string) $this->getCollection(),
  302. 'nIndexesWas' => 1,
  303. 'ok' => 1.0
  304. ];
  305. $this->assertEquals($expected, $this->getDatabase()->dropCollection('test'));
  306. }
  307. public function testRepair()
  308. {
  309. $this->skipTestIf(version_compare($this->getServerVersion(), '4.2.0', '>='), 'The "repairDatabase" has been removed in MongoDB 4.2.0');
  310. $this->assertSame(['ok' => 1.0], $this->getDatabase()->repair());
  311. }
  312. }