Commit d13e1109 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-145: __debugInfo for Client, Database, and Collection

This allows the selectDatabase() and selectCollection() to be tested (PHPLIB-144).
parent 68acf014
...@@ -34,6 +34,20 @@ class Client ...@@ -34,6 +34,20 @@ class Client
$this->uri = (string) $uri; $this->uri = (string) $uri;
} }
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public function __debugInfo()
{
return [
'manager' => $this->manager,
'uri' => $this->uri,
];
}
/** /**
* Return the connection string (i.e. URI). * Return the connection string (i.e. URI).
* *
......
...@@ -88,6 +88,23 @@ class Collection ...@@ -88,6 +88,23 @@ class Collection
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern(); $this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
} }
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public function __debugInfo()
{
return [
'collectionName' => $this->collectionName,
'databaseName' => $this->databaseName,
'manager' => $this->manager,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
}
/** /**
* Return the collection namespace (e.g. "db.collection"). * Return the collection namespace (e.g. "db.collection").
* *
......
...@@ -66,6 +66,22 @@ class Database ...@@ -66,6 +66,22 @@ class Database
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern(); $this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
} }
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public function __debugInfo()
{
return [
'databaseName' => $this->databaseName,
'manager' => $this->manager,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
}
/** /**
* Return the database name. * Return the database name.
* *
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace MongoDB\Tests; namespace MongoDB\Tests;
use MongoDB\Client; use MongoDB\Client;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
/** /**
* Unit tests for the Client class. * Unit tests for the Client class.
...@@ -22,4 +24,72 @@ class ClientTest extends TestCase ...@@ -22,4 +24,72 @@ class ClientTest extends TestCase
$this->assertSame($this->getUri(), (string) $client); $this->assertSame($this->getUri(), (string) $client);
} }
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
{
$clientOptions = [
'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY,
];
$client = new Client($this->getUri(), $clientOptions);
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
{
$collectionOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$client = new Client($this->getUri());
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectDatabaseInheritsReadPreferenceAndWriteConcern()
{
$clientOptions = [
'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY,
];
$client = new Client($this->getUri(), $clientOptions);
$database = $client->selectDatabase($this->getDatabaseName());
$debug = $database->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectDatabasePassesReadPreferenceAndWriteConcern()
{
$databaseOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$client = new Client($this->getUri());
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
$debug = $database->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
} }
...@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Database; ...@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Database;
use MongoDB\Database; use MongoDB\Database;
use MongoDB\Driver\BulkWrite; use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
/** /**
* Functional tests for the Database class. * Functional tests for the Database class.
...@@ -74,4 +76,37 @@ class DatabaseFunctionalTest extends FunctionalTestCase ...@@ -74,4 +76,37 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertCommandSucceeded($commandResult); $this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0); $this->assertCollectionCount($this->getNamespace(), 0);
} }
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
{
$databaseOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
$collection = $database->selectCollection($this->getCollectionName());
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
{
$collectionOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment