MongoDBTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 testGetCollection()
  10. {
  11. $db = $this->getDatabase();
  12. $collection = $db->selectCollection('test');
  13. $this->assertInstanceOf('MongoCollection', $collection);
  14. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  15. }
  16. public function testGetCollectionProperty()
  17. {
  18. $db = $this->getDatabase();
  19. $collection = $db->test;
  20. $this->assertInstanceOf('MongoCollection', $collection);
  21. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  22. }
  23. public function testCommand()
  24. {
  25. $db = $this->getDatabase();
  26. $this->assertEquals(['ok' => 1], $db->command(['ping' => 1]));
  27. }
  28. public function testCommandError()
  29. {
  30. $db = $this->getDatabase();
  31. $expected = [
  32. 'ok' => 0,
  33. 'errmsg' => 'listDatabases may only be run against the admin database.',
  34. 'code' => 13,
  35. ];
  36. $this->assertEquals($expected, $db->command(['listDatabases' => 1]));
  37. }
  38. public function testReadPreference()
  39. {
  40. $database = $this->getDatabase();
  41. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  42. $this->assertFalse($database->getSlaveOkay());
  43. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  44. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  45. $this->assertTrue($database->getSlaveOkay());
  46. // Only way to check whether options are passed down is through debugInfo
  47. $writeConcern = $database->getDb()->__debugInfo()['readPreference'];
  48. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  49. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  50. $this->assertTrue($database->setSlaveOkay(true));
  51. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  52. $this->assertTrue($database->setSlaveOkay(false));
  53. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  54. }
  55. public function testReadPreferenceIsInherited()
  56. {
  57. $client = $this->getClient();
  58. $client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  59. $database = $client->selectDB('test');
  60. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  61. }
  62. public function testWriteConcern()
  63. {
  64. $database = $this->getDatabase();
  65. $this->assertSame(['w' => 1, 'wtimeout' => 0], $database->getWriteConcern());
  66. $this->assertSame(1, $database->w);
  67. $this->assertSame(0, $database->wtimeout);
  68. $this->assertTrue($database->setWriteConcern('majority', 100));
  69. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  70. $database->w = 2;
  71. $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
  72. $database->wtimeout = -1;
  73. $this->assertSame(['w' => 2, 'wtimeout' => 0], $database->getWriteConcern());
  74. // Only way to check whether options are passed down is through debugInfo
  75. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  76. $this->assertSame(2, $writeConcern->getW());
  77. $this->assertSame(0, $writeConcern->getWtimeout());
  78. }
  79. public function testWriteConcernIsInherited()
  80. {
  81. $client = $this->getClient();
  82. $client->setWriteConcern('majority', 100);
  83. $database = $client->selectDB('test');
  84. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  85. }
  86. public function testProfilingLevel()
  87. {
  88. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->getProfilingLevel());
  89. $this->assertSame(\MongoDB::PROFILING_OFF, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_SLOW));
  90. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->getProfilingLevel());
  91. $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON));
  92. $this->assertSame(\MongoDB::PROFILING_ON, $this->getDatabase()->getProfilingLevel());
  93. }
  94. public function testForceError()
  95. {
  96. $result = $this->getDatabase()->forceError();
  97. $this->assertSame(0, $result['ok']);
  98. }
  99. public function testExecute()
  100. {
  101. $db = $this->getDatabase();
  102. $this->getCollection()->insert(['foo' => 'bar']);
  103. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  104. }
  105. public function testGetCollectionNames()
  106. {
  107. $this->getCollection()->insert(['foo' => 'bar']);
  108. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  109. }
  110. public function testGetCollectionInfo()
  111. {
  112. $this->getCollection()->insert(['foo' => 'bar']);
  113. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  114. if ($collectionInfo['name'] === 'test') {
  115. $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
  116. return;
  117. }
  118. }
  119. $this->fail('The test collection was not found');
  120. }
  121. public function testListCollections()
  122. {
  123. $this->getCollection()->insert(['foo' => 'bar']);
  124. foreach ($this->getDatabase()->listCollections() as $collection) {
  125. $this->assertInstanceOf('MongoCollection', $collection);
  126. if ($collection->getName() === 'test') {
  127. return;
  128. }
  129. }
  130. $this->fail('The test collection was not found');
  131. }
  132. public function testDrop()
  133. {
  134. $this->getCollection()->insert(['foo' => 'bar']);
  135. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  136. }
  137. public function testDropCollection()
  138. {
  139. $this->getCollection()->insert(['foo' => 'bar']);
  140. $expected = [
  141. 'ns' => (string) $this->getCollection(),
  142. 'nIndexesWas' => 1,
  143. 'ok' => 1.0
  144. ];
  145. $this->assertSame($expected, $this->getDatabase()->dropCollection('test'));
  146. }
  147. }