| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace Alcaeus\MongoDbAdapter\Tests;
- /**
- * @author alcaeus <alcaeus@alcaeus.org>
- */
- class MongoClientTest extends TestCase
- {
- public function testConnectAndDisconnect()
- {
- $client = $this->getClient();
- $this->assertTrue($client->connected);
- $client->close();
- $this->assertFalse($client->connected);
- }
- public function testClientWithoutAutomaticConnect()
- {
- $client = $this->getClient([]);
- $this->assertFalse($client->connected);
- }
- public function testGetDb()
- {
- $client = $this->getClient();
- $db = $client->selectDB('mongo-php-adapter');
- $this->assertInstanceOf('\MongoDB', $db);
- $this->assertSame('mongo-php-adapter', (string) $db);
- }
- public function testGetDbProperty()
- {
- $client = $this->getClient();
- $db = $client->{'mongo-php-adapter'};
- $this->assertInstanceOf('\MongoDB', $db);
- $this->assertSame('mongo-php-adapter', (string) $db);
- }
- public function testGetCollection()
- {
- $client = $this->getClient();
- $collection = $client->selectCollection('mongo-php-adapter', 'test');
- $this->assertInstanceOf('MongoCollection', $collection);
- $this->assertSame('mongo-php-adapter.test', (string) $collection);
- }
- public function testGetHosts()
- {
- $client = $this->getClient();
- $this->assertArraySubset(
- [
- 'localhost:27017' => [
- 'host' => 'localhost',
- 'port' => 27017,
- 'health' => 1,
- 'state' => 1,
- ],
- ],
- $client->getHosts()
- );
- }
- public function testReadPreference()
- {
- $client = $this->getClient();
- $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
- $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
- $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $client->getReadPreference());
- }
- public function testWriteConcern()
- {
- $client = $this->getClient();
- $this->assertSame(['w' => 1, 'wtimeout' => 0], $client->getWriteConcern());
- $this->assertTrue($client->setWriteConcern('majority', 100));
- $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
- }
- /**
- * @param array|null $options
- * @return \MongoClient
- */
- protected function getClient($options = null)
- {
- $args = ['mongodb://localhost'];
- if ($options !== null) {
- $args[] = $options;
- }
- $reflection = new \ReflectionClass('MongoClient');
- return $reflection->newInstanceArgs($args);
- }
- }
|