MongoClientTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoClientTest extends TestCase
  8. {
  9. public function testGetDb()
  10. {
  11. $client = $this->getClient();
  12. $db = $client->selectDB('mongo-php-adapter');
  13. $this->assertInstanceOf('\MongoDB', $db);
  14. $this->assertSame('mongo-php-adapter', (string) $db);
  15. }
  16. public function testSelectDBWithEmptyName()
  17. {
  18. $this->setExpectedException('Exception', 'Database name cannot be empty');
  19. $this->getClient()->selectDB('');
  20. }
  21. public function testSelectDBWithInvalidName()
  22. {
  23. $this->setExpectedException('Exception', 'Database name contains invalid characters');
  24. $this->getClient()->selectDB('/');
  25. }
  26. public function testGetDbProperty()
  27. {
  28. $client = $this->getClient();
  29. $db = $client->{'mongo-php-adapter'};
  30. $this->assertInstanceOf('\MongoDB', $db);
  31. $this->assertSame('mongo-php-adapter', (string) $db);
  32. }
  33. public function testGetCollection()
  34. {
  35. $client = $this->getClient();
  36. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  37. $this->assertInstanceOf('MongoCollection', $collection);
  38. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  39. }
  40. public function testGetHosts()
  41. {
  42. $client = $this->getClient();
  43. $hosts = $client->getHosts();
  44. $this->assertArraySubset(
  45. [
  46. 'localhost:27017;-;.;' . getmypid() => [
  47. 'host' => 'localhost',
  48. 'port' => 27017,
  49. 'health' => 1,
  50. 'state' => 0,
  51. ],
  52. ],
  53. $hosts
  54. );
  55. }
  56. public function testReadPreference()
  57. {
  58. $client = $this->getClient();
  59. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
  60. $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  61. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $client->getReadPreference());
  62. }
  63. public function testWriteConcern()
  64. {
  65. $client = $this->getClient();
  66. $this->assertTrue($client->setWriteConcern('majority', 100));
  67. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
  68. }
  69. public function testListDBs()
  70. {
  71. $document = ['foo' => 'bar'];
  72. $this->getCollection()->insert($document);
  73. $databases = $this->getClient()->listDBs();
  74. $this->assertSame(1.0, $databases['ok']);
  75. $this->assertArrayHasKey('totalSize', $databases);
  76. $this->assertArrayHasKey('databases', $databases);
  77. foreach ($databases['databases'] as $database) {
  78. $this->assertArrayHasKey('name', $database);
  79. $this->assertArrayHasKey('empty', $database);
  80. $this->assertArrayHasKey('sizeOnDisk', $database);
  81. if ($database['name'] == 'mongo-php-adapter') {
  82. $this->assertFalse($database['empty']);
  83. return;
  84. }
  85. }
  86. $this->fail('Could not find mongo-php-adapter database in list');
  87. }
  88. public function testNoPrefixUri()
  89. {
  90. $client = $this->getClient(null, 'localhost');
  91. $this->assertNotNull($client);
  92. }
  93. }