MongoClientTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 testSelectDBWithEmptyName()
  28. {
  29. $this->setExpectedException('Exception', 'Database name cannot be empty');
  30. $this->getClient()->selectDB('');
  31. }
  32. public function testSelectDBWithInvalidName()
  33. {
  34. $this->setExpectedException('Exception', 'Database name contains invalid characters');
  35. $this->getClient()->selectDB('/');
  36. }
  37. public function testGetDbProperty()
  38. {
  39. $client = $this->getClient();
  40. $db = $client->{'mongo-php-adapter'};
  41. $this->assertInstanceOf('\MongoDB', $db);
  42. $this->assertSame('mongo-php-adapter', (string) $db);
  43. }
  44. public function testGetCollection()
  45. {
  46. $client = $this->getClient();
  47. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  48. $this->assertInstanceOf('MongoCollection', $collection);
  49. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  50. }
  51. public function testGetHosts()
  52. {
  53. $client = $this->getClient();
  54. $this->assertArraySubset(
  55. [
  56. 'localhost:27017' => [
  57. 'host' => 'localhost',
  58. 'port' => 27017,
  59. 'health' => 1,
  60. 'state' => 0,
  61. ],
  62. ],
  63. $client->getHosts()
  64. );
  65. }
  66. public function testReadPreference()
  67. {
  68. $client = $this->getClient();
  69. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
  70. $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  71. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $client->getReadPreference());
  72. }
  73. public function testWriteConcern()
  74. {
  75. $client = $this->getClient();
  76. $this->assertSame(['w' => 1, 'wtimeout' => 0], $client->getWriteConcern());
  77. $this->assertTrue($client->setWriteConcern('majority', 100));
  78. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
  79. }
  80. public function testListDBs()
  81. {
  82. $document = ['foo' => 'bar'];
  83. $this->getCollection()->insert($document);
  84. $databases = $this->getClient()->listDBs();
  85. $this->assertSame(1.0, $databases['ok']);
  86. $this->assertArrayHasKey('totalSize', $databases);
  87. $this->assertArrayHasKey('databases', $databases);
  88. $this->assertContains('mongo-php-adapter', $databases['databases']);
  89. }
  90. public function testNoPrefixUri()
  91. {
  92. $client = $this->getClient(null, 'localhost');
  93. $this->assertNotNull($client);
  94. }
  95. }