MongoDBTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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], [], $hash));
  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], [], $hash));
  37. }
  38. public function testReadPreference()
  39. {
  40. $database = $this->getDatabase();
  41. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
  42. $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  43. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  44. // Only way to check whether options are passed down is through debugInfo
  45. $writeConcern = $database->getDb()->__debugInfo()['readPreference'];
  46. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  47. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  48. }
  49. public function testReadPreferenceIsInherited()
  50. {
  51. $client = $this->getClient();
  52. $client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  53. $database = $client->selectDB('test');
  54. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
  55. }
  56. public function testWriteConcern()
  57. {
  58. $database = $this->getDatabase();
  59. $this->assertSame(['w' => 1, 'wtimeout' => 0], $database->getWriteConcern());
  60. $this->assertSame(1, $database->w);
  61. $this->assertSame(0, $database->wtimeout);
  62. $this->assertTrue($database->setWriteConcern('majority', 100));
  63. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  64. $database->w = 2;
  65. $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
  66. $database->wtimeout = -1;
  67. $this->assertSame(['w' => 2, 'wtimeout' => 0], $database->getWriteConcern());
  68. // Only way to check whether options are passed down is through debugInfo
  69. $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
  70. $this->assertSame(2, $writeConcern->getW());
  71. $this->assertSame(0, $writeConcern->getWtimeout());
  72. }
  73. public function testWriteConcernIsInherited()
  74. {
  75. $client = $this->getClient();
  76. $client->setWriteConcern('majority', 100);
  77. $database = $client->selectDB('test');
  78. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
  79. }
  80. /**
  81. * @return \MongoDB
  82. */
  83. protected function getDatabase()
  84. {
  85. $client = $this->getClient();
  86. return $client->selectDB('mongo-php-adapter');
  87. }
  88. /**
  89. * @return \MongoClient
  90. */
  91. protected function getClient()
  92. {
  93. return new \MongoClient();
  94. }
  95. }