MongoDBTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\ReadPreference;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoDBTest extends TestCase
  8. {
  9. public function testEmptyDatabaseName()
  10. {
  11. $this->setExpectedException('Exception', 'Database name cannot be empty');
  12. new \MongoDB($this->getClient(), '');
  13. }
  14. public function testInvalidDatabaseName()
  15. {
  16. $this->setExpectedException('Exception', 'Database name contains invalid characters');
  17. new \MongoDB($this->getClient(), '/');
  18. }
  19. public function testGetCollection()
  20. {
  21. $db = $this->getDatabase();
  22. $collection = $db->selectCollection('test');
  23. $this->assertInstanceOf('MongoCollection', $collection);
  24. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  25. }
  26. public function testSelectCollectionEmptyName()
  27. {
  28. $database = $this->getDatabase();
  29. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  30. $database->selectCollection('');
  31. }
  32. public function testSelectCollectionWithNullBytes()
  33. {
  34. $database = $this->getDatabase();
  35. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  36. $database->selectCollection('foo' . chr(0));
  37. }
  38. public function testCreateCollection()
  39. {
  40. $database = $this->getDatabase();
  41. $collection = $database->createCollection('test', ['capped' => true, 'size' => 100]);
  42. $this->assertInstanceOf('MongoCollection', $collection);
  43. $document = ['foo' => 'bar'];
  44. $collection->insert($document);
  45. $checkDatabase = $this->getCheckDatabase();
  46. foreach ($checkDatabase->listCollections() as $collectionInfo) {
  47. if ($collectionInfo->getName() === 'test') {
  48. $this->assertTrue($collectionInfo->isCapped());
  49. return;
  50. }
  51. }
  52. }
  53. public function testCreateCollectionInvalidParameters()
  54. {
  55. $database = $this->getDatabase();
  56. $this->assertFalse($database->createCollection('test', ['capped' => 2, 'size' => 100]));
  57. }
  58. public function testGetCollectionProperty()
  59. {
  60. $db = $this->getDatabase();
  61. $collection = $db->test;
  62. $this->assertInstanceOf('MongoCollection', $collection);
  63. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  64. }
  65. public function testCommand()
  66. {
  67. $db = $this->getDatabase();
  68. $this->assertEquals(['ok' => 1], $db->command(['ping' => 1]));
  69. }
  70. public function testCommandError()
  71. {
  72. $db = $this->getDatabase();
  73. $expected = [
  74. 'ok' => 0,
  75. 'errmsg' => 'listDatabases may only be run against the admin database.',
  76. 'code' => 13,
  77. ];
  78. $this->assertEquals($expected, $db->command(['listDatabases' => 1]));
  79. }
  80. public function testCommandCursorTimeout()
  81. {
  82. $database = $this->getDatabase();
  83. $this->failMaxTimeMS();
  84. $this->setExpectedException('MongoCursorTimeoutException');
  85. $database->command([
  86. "count" => "test",
  87. "query" => array("a" => 1),
  88. "maxTimeMS" => 100,
  89. ]);
  90. }
  91. public function testReadPreference()
  92. {
  93. $database = $this->getDatabase();
  94. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  95. $this->assertFalse($database->getSlaveOkay());
  96. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  97. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  98. $this->assertTrue($database->getSlaveOkay());
  99. // Only way to check whether options are passed down is through debugInfo
  100. $writeConcern = $database->getDb()->__debugInfo()['readPreference'];
  101. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  102. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  103. $this->assertTrue($database->setSlaveOkay(true));
  104. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  105. $this->assertTrue($database->setSlaveOkay(false));
  106. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  107. }
  108. public function testReadPreferenceIsInherited()
  109. {
  110. $client = $this->getClient();
  111. $client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  112. $database = $client->selectDB('test');
  113. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  114. }
  115. public function testWriteConcern()
  116. {
  117. $database = $this->getDatabase();
  118. $this->assertSame(['w' => 1, 'wtimeout' => 0], $database->getWriteConcern());
  119. $this->assertSame(1, $database->w);
  120. $this->assertSame(0, $database->wtimeout);
  121. $this->assertTrue($database->setWriteConcern('majority', 100));
  122. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  123. $database->w = 2;
  124. $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
  125. $database->wtimeout = -1;
  126. $this->assertSame(['w' => 2, 'wtimeout' => 0], $database->getWriteConcern());
  127. // Only way to check whether options are passed down is through debugInfo
  128. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  129. $this->assertSame(2, $writeConcern->getW());
  130. $this->assertSame(0, $writeConcern->getWtimeout());
  131. }
  132. public function testWriteConcernIsInherited()
  133. {
  134. $client = $this->getClient();
  135. $client->setWriteConcern('majority', 100);
  136. $database = $client->selectDB('test');
  137. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  138. }
  139. public function testProfilingLevel()
  140. {
  141. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->getProfilingLevel());
  142. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_SLOW));
  143. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->getProfilingLevel());
  144. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON));
  145. $this->assertSame(\MongoDB::PROFILING_ON, $this->getDatabase()->getProfilingLevel());
  146. }
  147. public function testForceError()
  148. {
  149. $result = $this->getDatabase()->forceError();
  150. $this->assertSame(0, $result['ok']);
  151. }
  152. public function testExecute()
  153. {
  154. $db = $this->getDatabase();
  155. $document = ['foo' => 'bar'];
  156. $this->getCollection()->insert($document);
  157. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  158. }
  159. public function testGetCollectionNames()
  160. {
  161. $document = ['foo' => 'bar'];
  162. $this->getCollection()->insert($document);
  163. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  164. }
  165. public function testGetCollectionNamesExecutionTimeoutException()
  166. {
  167. $document = ['foo' => 'bar'];
  168. $this->getCollection()->insert($document);
  169. $database = $this->getDatabase();
  170. $this->failMaxTimeMS();
  171. $this->setExpectedException('MongoExecutionTimeoutException');
  172. $database->getCollectionNames(['maxTimeMS' => 1]);
  173. }
  174. public function testGetCollectionInfo()
  175. {
  176. $document = ['foo' => 'bar'];
  177. $this->getCollection()->insert($document);
  178. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  179. if ($collectionInfo['name'] === 'test') {
  180. $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
  181. return;
  182. }
  183. }
  184. $this->fail('The test collection was not found');
  185. }
  186. public function testGetCollectionInfoExecutionTimeoutException()
  187. {
  188. $document = ['foo' => 'bar'];
  189. $this->getCollection()->insert($document);
  190. $database = $this->getDatabase();
  191. $this->failMaxTimeMS();
  192. $this->setExpectedException('MongoExecutionTimeoutException');
  193. $database->getCollectionInfo(['maxTimeMS' => 1]);
  194. }
  195. public function testListCollections()
  196. {
  197. $document = ['foo' => 'bar'];
  198. $this->getCollection()->insert($document);
  199. foreach ($this->getDatabase()->listCollections() as $collection) {
  200. $this->assertInstanceOf('MongoCollection', $collection);
  201. if ($collection->getName() === 'test') {
  202. return;
  203. }
  204. }
  205. $this->fail('The test collection was not found');
  206. }
  207. public function testListCollectionsExecutionTimeoutException()
  208. {
  209. $this->failMaxTimeMS();
  210. $this->setExpectedException('MongoExecutionTimeoutException');
  211. $this->getDatabase()->listCollections(['maxTimeMS' => 1]);
  212. }
  213. public function testDrop()
  214. {
  215. $document = ['foo' => 'bar'];
  216. $this->getCollection()->insert($document);
  217. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  218. }
  219. public function testDropCollection()
  220. {
  221. $document = ['foo' => 'bar'];
  222. $this->getCollection()->insert($document);
  223. $expected = [
  224. 'ns' => (string) $this->getCollection(),
  225. 'nIndexesWas' => 1,
  226. 'ok' => 1.0
  227. ];
  228. $this->assertSame($expected, $this->getDatabase()->dropCollection('test'));
  229. }
  230. }