MongoDBTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. $document = ['foo' => 'bar'];
  103. $this->getCollection()->insert($document);
  104. $this->assertEquals(['ok' => 1, 'retval' => 1], $db->execute("return db.test.count();"));
  105. }
  106. public function testGetCollectionNames()
  107. {
  108. $document = ['foo' => 'bar'];
  109. $this->getCollection()->insert($document);
  110. $this->assertContains('test', $this->getDatabase()->getCollectionNames());
  111. }
  112. public function testGetCollectionInfo()
  113. {
  114. $document = ['foo' => 'bar'];
  115. $this->getCollection()->insert($document);
  116. foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
  117. if ($collectionInfo['name'] === 'test') {
  118. $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
  119. return;
  120. }
  121. }
  122. $this->fail('The test collection was not found');
  123. }
  124. public function testListCollections()
  125. {
  126. $document = ['foo' => 'bar'];
  127. $this->getCollection()->insert($document);
  128. foreach ($this->getDatabase()->listCollections() as $collection) {
  129. $this->assertInstanceOf('MongoCollection', $collection);
  130. if ($collection->getName() === 'test') {
  131. return;
  132. }
  133. }
  134. $this->fail('The test collection was not found');
  135. }
  136. public function testDrop()
  137. {
  138. $document = ['foo' => 'bar'];
  139. $this->getCollection()->insert($document);
  140. $this->assertSame(['dropped' => 'mongo-php-adapter', 'ok' => 1.0], $this->getDatabase()->drop());
  141. }
  142. public function testDropCollection()
  143. {
  144. $document = ['foo' => 'bar'];
  145. $this->getCollection()->insert($document);
  146. $expected = [
  147. 'ns' => (string) $this->getCollection(),
  148. 'nIndexesWas' => 1,
  149. 'ok' => 1.0
  150. ];
  151. $this->assertSame($expected, $this->getDatabase()->dropCollection('test'));
  152. }
  153. }