MongoDBTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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->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. /**
  87. * @return \MongoDB
  88. */
  89. protected function getDatabase()
  90. {
  91. $client = $this->getClient();
  92. return $client->selectDB('mongo-php-adapter');
  93. }
  94. /**
  95. * @return \MongoClient
  96. */
  97. protected function getClient()
  98. {
  99. return new \MongoClient();
  100. }
  101. }