MongoDBTest.php 12 KB

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