ClientFunctionalTest.php 3.32 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests;

use MongoDB\Client;
6
use MongoDB\Driver\BulkWrite;
7 8
use MongoDB\Driver\Command;
use MongoDB\Model\DatabaseInfo;
9 10 11 12 13 14

/**
 * Functional tests for the Client class.
 */
class ClientFunctionalTest extends FunctionalTestCase
{
15 16 17 18 19 20 21 22 23 24
    private $client;

    public function setUp()
    {
        parent::setUp();

        $this->client = new Client($this->getUri());
        $this->client->dropDatabase($this->getDatabaseName());
    }

25 26 27 28 29
    public function testGetManager()
    {
        $this->assertInstanceOf('MongoDB\Driver\Manager', $this->client->getManager());
    }

30 31
    public function testDropDatabase()
    {
32 33 34 35
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

        $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
36 37
        $this->assertEquals(1, $writeResult->getInsertedCount());

38
        $commandResult = $this->client->dropDatabase($this->getDatabaseName());
39 40 41
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
42 43 44

    public function testListDatabases()
    {
45 46 47 48
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

        $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
49 50
        $this->assertEquals(1, $writeResult->getInsertedCount());

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        $databases = $this->client->listDatabases();

        $this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);

        foreach ($databases as $database) {
            $this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
        }

        $that = $this;
        $this->assertDatabaseExists($this->getDatabaseName(), function(DatabaseInfo $info) use ($that) {
            $that->assertFalse($info->isEmpty());
            $that->assertGreaterThan(0, $info->getSizeOnDisk());
        });
    }

    /**
     * Asserts that a database with the given name exists on the server.
     *
     * An optional $callback may be provided, which should take a DatabaseInfo
     * argument as its first and only parameter. If a DatabaseInfo matching
     * the given name is found, it will be passed to the callback, which may
     * perform additional assertions.
     *
74
     * @param string $databaseName
75 76 77 78 79 80 81
     * @param callable $callback
     */
    private function assertDatabaseExists($databaseName, $callback = null)
    {
        if ($callback !== null && ! is_callable($callback)) {
            throw new InvalidArgumentException('$callback is not a callable');
        }
82

83
        $databases = $this->client->listDatabases();
84 85 86 87

        $foundDatabase = null;

        foreach ($databases as $database) {
88
            if ($database->getName() === $databaseName) {
89 90 91 92 93
                $foundDatabase = $database;
                break;
            }
        }

94
        $this->assertNotNull($foundDatabase, sprintf('Database %s does not exist on the server', $databaseName));
95 96 97 98

        if ($callback !== null) {
            call_user_func($callback, $foundDatabase);
        }
99
    }
100 101 102 103 104 105 106 107

    public function testStartSession()
    {
        if (version_compare($this->getFeatureCompatibilityVersion(), '3.6', '<')) {
            $this->markTestSkipped('startSession() is only supported on FCV 3.6 or higher');
        }
        $this->assertInstanceOf('MongoDB\Driver\Session', $this->client->startSession());
    }
108
}