MongoClientTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. /**
  4. * @author alcaeus <alcaeus@alcaeus.org>
  5. */
  6. class MongoClientTest extends TestCase
  7. {
  8. public function testConnectAndDisconnect()
  9. {
  10. $client = $this->getClient();
  11. $this->assertTrue($client->connected);
  12. $client->close();
  13. $this->assertFalse($client->connected);
  14. }
  15. public function testClientWithoutAutomaticConnect()
  16. {
  17. $client = $this->getClient([]);
  18. $this->assertFalse($client->connected);
  19. }
  20. public function testGetDb()
  21. {
  22. $client = $this->getClient();
  23. $db = $client->selectDB('mongo-php-adapter');
  24. $this->assertInstanceOf('\MongoDB', $db);
  25. $this->assertSame('mongo-php-adapter', (string) $db);
  26. }
  27. public function testGetDbProperty()
  28. {
  29. $client = $this->getClient();
  30. $db = $client->{'mongo-php-adapter'};
  31. $this->assertInstanceOf('\MongoDB', $db);
  32. $this->assertSame('mongo-php-adapter', (string) $db);
  33. }
  34. public function testGetCollection()
  35. {
  36. $client = $this->getClient();
  37. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  38. $this->assertInstanceOf('MongoCollection', $collection);
  39. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  40. }
  41. public function testReadPreference()
  42. {
  43. $client = $this->getClient();
  44. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
  45. $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  46. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $client->getReadPreference());
  47. }
  48. public function testWriteConcern()
  49. {
  50. $client = $this->getClient();
  51. $this->assertSame(['w' => 1, 'wtimeout' => 0], $client->getWriteConcern());
  52. $this->assertTrue($client->setWriteConcern('majority', 100));
  53. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
  54. }
  55. /**
  56. * @param array|null $options
  57. * @return \MongoClient
  58. */
  59. protected function getClient($options = null)
  60. {
  61. $args = ['mongodb://localhost'];
  62. if ($options !== null) {
  63. $args[] = $options;
  64. }
  65. $reflection = new \ReflectionClass('MongoClient');
  66. return $reflection->newInstanceArgs($args);
  67. }
  68. }