MongoClientTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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->assertSame(['w' => 1, 'wtimeout' => 0], $client->getWriteConcern());
  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. $this->assertContains('mongo-php-adapter', $databases['databases']);
  78. }
  79. public function testNoPrefixUri()
  80. {
  81. $client = $this->getClient(null, 'localhost');
  82. $this->assertNotNull($client);
  83. }
  84. }