MongoClientTest.php 3.3 KB

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