manager, $databaseName); } public function provideInvalidDatabaseNames() { return [ [null], [''], ]; } /** * @expectedException MongoDB\Exception\InvalidArgumentException * @dataProvider provideInvalidConstructorOptions */ public function testConstructorOptionTypeChecks(array $options) { new Database($this->manager, $this->getDatabaseName(), $options); } public function provideInvalidConstructorOptions() { $options = []; foreach ($this->getInvalidReadConcernValues() as $value) { $options[][] = ['readConcern' => $value]; } foreach ($this->getInvalidReadPreferenceValues() as $value) { $options[][] = ['readPreference' => $value]; } foreach ($this->getInvalidArrayValues() as $value) { $options[][] = ['typeMap' => $value]; } foreach ($this->getInvalidWriteConcernValues() as $value) { $options[][] = ['writeConcern' => $value]; } return $options; } public function testToString() { $this->assertEquals($this->getDatabaseName(), (string) $this->database); } public function getGetDatabaseName() { $this->assertEquals($this->getDatabaseName(), $this->database->getDatabaseName()); } public function testCommand() { $command = ['isMaster' => 1]; $options = [ 'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY), ]; $cursor = $this->database->command($command, $options); $this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor); $commandResult = current($cursor->toArray()); $this->assertCommandSucceeded($commandResult); $this->assertTrue(isset($commandResult->ismaster)); $this->assertTrue($commandResult->ismaster); } public function testCommandAppliesTypeMapToCursor() { $command = ['isMaster' => 1]; $options = [ 'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY), 'typeMap' => ['root' => 'array'], ]; $cursor = $this->database->command($command, $options); $this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor); $commandResult = current($cursor->toArray()); $this->assertCommandSucceeded($commandResult); $this->assertInternalType('array', $commandResult); $this->assertTrue(isset($commandResult['ismaster'])); $this->assertTrue($commandResult['ismaster']); } /** * @expectedException MongoDB\Exception\InvalidArgumentException * @dataProvider provideInvalidDocumentValues */ public function testCommandCommandArgumentTypeCheck($command) { $this->database->command($command); } public function testDrop() { $bulkWrite = new BulkWrite(); $bulkWrite->insert(['x' => 1]); $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $this->assertEquals(1, $writeResult->getInsertedCount()); $commandResult = $this->database->drop(); $this->assertCommandSucceeded($commandResult); $this->assertCollectionCount($this->getNamespace(), 0); } public function testGetSelectsCollectionAndInheritsOptions() { $databaseOptions = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]; $database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions); $collection = $database->{$this->getCollectionName()}; $debug = $collection->__debugInfo(); $this->assertSame($this->getCollectionName(), $debug['collectionName']); $this->assertSame($this->getDatabaseName(), $debug['databaseName']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); } public function testSelectCollectionInheritsOptions() { $databaseOptions = [ 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], '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\ReadConcern', $debug['readConcern']); $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertInternalType('array', $debug['typeMap']); $this->assertSame(['root' => 'array'], $debug['typeMap']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); } public function testSelectCollectionPassesOptions() { $collectionOptions = [ 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), ]; $collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions); $debug = $collection->__debugInfo(); $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']); $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertInternalType('array', $debug['typeMap']); $this->assertSame(['root' => 'array'], $debug['typeMap']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); } public function testWithOptionsInheritsOptions() { $databaseOptions = [ 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), ]; $database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions); $clone = $database->withOptions(); $debug = $clone->__debugInfo(); $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']); $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertInternalType('array', $debug['typeMap']); $this->assertSame(['root' => 'array'], $debug['typeMap']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); } public function testWithOptionsPassesOptions() { $databaseOptions = [ 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), ]; $clone = $this->database->withOptions($databaseOptions); $debug = $clone->__debugInfo(); $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']); $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel()); $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertInternalType('array', $debug['typeMap']); $this->assertSame(['root' => 'array'], $debug['typeMap']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); } }