MongoDBTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. $db = $this->getDatabase();
  189. $document = ['foo' => 'bar'];
  190. $this->getCollection()->insert($document);
  191. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  192. }
  193. public function testGetCollectionNames()
  194. {
  195. $document = ['foo' => 'bar'];
  196. $this->getCollection()->insert($document);
  197. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  198. }
  199. public function testGetCollectionNamesExecutionTimeoutException()
  200. {
  201. $document = ['foo' => 'bar'];
  202. $this->getCollection()->insert($document);
  203. $database = $this->getDatabase();
  204. $this->failMaxTimeMS();
  205. $this->expectException(\MongoExecutionTimeoutException::class);
  206. $database->getCollectionNames(['maxTimeMS' => 1]);
  207. }
  208. public function testGetCollectionInfo()
  209. {
  210. $document = ['foo' => 'bar'];
  211. $this->getCollection()->insert($document);
  212. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  213. if ($collectionInfo['name'] === 'test') {
  214. $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
  215. return;
  216. }
  217. }
  218. $this->fail('The test collection was not found');
  219. }
  220. public function testGetCollectionInfoExecutionTimeoutException()
  221. {
  222. $document = ['foo' => 'bar'];
  223. $this->getCollection()->insert($document);
  224. $database = $this->getDatabase();
  225. $this->failMaxTimeMS();
  226. $this->expectException(\MongoExecutionTimeoutException::class);
  227. $database->getCollectionInfo(['maxTimeMS' => 1]);
  228. }
  229. public function testListCollections()
  230. {
  231. $document = ['foo' => 'bar'];
  232. $this->getCollection()->insert($document);
  233. foreach ($this->getDatabase()->listCollections() as $collection) {
  234. $this->assertInstanceOf('MongoCollection', $collection);
  235. if ($collection->getName() === 'test') {
  236. return;
  237. }
  238. }
  239. $this->fail('The test collection was not found');
  240. }
  241. public function testGetCollectionNamesDoesNotListSystemCollections()
  242. {
  243. // Enable profiling to ensure we have a system.profile collection
  244. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON);
  245. try {
  246. $document = ['foo' => 'bar'];
  247. $this->getCollection()->insert($document);
  248. $collectionNames = $this->getDatabase()->getCollectionNames();
  249. $this->assertNotContains('system.profile', $collectionNames);
  250. } finally {
  251. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_OFF);
  252. }
  253. }
  254. public function testGetCollectionNamesWithSystemCollections()
  255. {
  256. // Enable profiling to ensure we have a system.profile collection
  257. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON);
  258. try {
  259. $document = ['foo' => 'bar'];
  260. $this->getCollection()->insert($document);
  261. $collectionNames = $this->getDatabase()->getCollectionNames(['includeSystemCollections' => true]);
  262. $this->assertContains('system.profile', $collectionNames);
  263. } finally {
  264. $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_OFF);
  265. }
  266. }
  267. public function testListCollectionsExecutionTimeoutException()
  268. {
  269. $this->failMaxTimeMS();
  270. $this->expectException(\MongoExecutionTimeoutException::class);
  271. $this->getDatabase()->listCollections(['maxTimeMS' => 1]);
  272. }
  273. public function testDrop()
  274. {
  275. $document = ['foo' => 'bar'];
  276. $this->getCollection()->insert($document);
  277. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  278. }
  279. public function testDropCollection()
  280. {
  281. $document = ['foo' => 'bar'];
  282. $this->getCollection()->insert($document);
  283. $expected = [
  284. 'ns' => (string) $this->getCollection(),
  285. 'nIndexesWas' => 1,
  286. 'ok' => 1.0
  287. ];
  288. $this->assertSame($expected, $this->getDatabase()->dropCollection('test'));
  289. }
  290. public function testRepair()
  291. {
  292. $this->assertSame(['ok' => 1.0], $this->getDatabase()->repair());
  293. }
  294. }