MongoClientTest.php 3.5 KB

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