MongoClientTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. /**
  4. * @author alcaeus <alcaeus@alcaeus.org>
  5. * @covers MongoClient
  6. */
  7. class MongoClientTest extends TestCase
  8. {
  9. public function testConnectAndDisconnect()
  10. {
  11. $client = $this->getClient();
  12. $this->assertTrue($client->connected);
  13. $client->close();
  14. $this->assertFalse($client->connected);
  15. }
  16. public function testClientWithoutAutomaticConnect()
  17. {
  18. $client = $this->getClient([]);
  19. $this->assertFalse($client->connected);
  20. }
  21. public function testGetDb()
  22. {
  23. $client = $this->getClient();
  24. $db = $client->selectDB('mongo-php-adapter');
  25. $this->assertInstanceOf('\MongoDB', $db);
  26. $this->assertSame('mongo-php-adapter', (string) $db);
  27. }
  28. public function testGetDbProperty()
  29. {
  30. $client = $this->getClient();
  31. $db = $client->{'mongo-php-adapter'};
  32. $this->assertInstanceOf('\MongoDB', $db);
  33. $this->assertSame('mongo-php-adapter', (string) $db);
  34. }
  35. public function testGetCollection()
  36. {
  37. $client = $this->getClient();
  38. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  39. $this->assertInstanceOf('MongoCollection', $collection);
  40. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  41. }
  42. /**
  43. * @param array|null $options
  44. * @return \MongoClient
  45. */
  46. protected function getClient($options = null)
  47. {
  48. $args = ['mongodb://localhost'];
  49. if ($options !== null) {
  50. $args[] = $options;
  51. }
  52. $reflection = new \ReflectionClass('MongoClient');
  53. return $reflection->newInstanceArgs($args);
  54. }
  55. }