MongoClientTest.php 1.7 KB

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