ClientFunctionalTest.php 3.56 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\Manager;
use MongoDB\Driver\Session;
9
use MongoDB\Model\DatabaseInfo;
10
use MongoDB\Model\DatabaseInfoIterator;
11
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
12 13 14 15
use function call_user_func;
use function is_callable;
use function sprintf;
use function version_compare;
16 17 18 19 20 21

/**
 * Functional tests for the Client class.
 */
class ClientFunctionalTest extends FunctionalTestCase
{
22 23
    use SetUpTearDownTrait;

24
    /** @var Client */
25 26
    private $client;

27
    private function doSetUp()
28 29 30
    {
        parent::setUp();

31
        $this->client = new Client(static::getUri());
32 33 34
        $this->client->dropDatabase($this->getDatabaseName());
    }

35 36
    public function testGetManager()
    {
37
        $this->assertInstanceOf(Manager::class, $this->client->getManager());
38 39
    }

40 41
    public function testDropDatabase()
    {
42 43 44 45
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

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

48
        $commandResult = $this->client->dropDatabase($this->getDatabaseName());
49 50 51
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
52 53 54

    public function testListDatabases()
    {
55 56 57 58
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

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

61 62
        $databases = $this->client->listDatabases();

63
        $this->assertInstanceOf(DatabaseInfoIterator::class, $databases);
64 65

        foreach ($databases as $database) {
66
            $this->assertInstanceOf(DatabaseInfo::class, $database);
67 68 69
        }

        $that = $this;
70
        $this->assertDatabaseExists($this->getDatabaseName(), function (DatabaseInfo $info) use ($that) {
71 72 73 74 75 76 77 78 79 80 81 82 83
            $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.
     *
84
     * @param string   $databaseName
85 86 87 88 89 90 91
     * @param callable $callback
     */
    private function assertDatabaseExists($databaseName, $callback = null)
    {
        if ($callback !== null && ! is_callable($callback)) {
            throw new InvalidArgumentException('$callback is not a callable');
        }
92

93
        $databases = $this->client->listDatabases();
94 95 96 97

        $foundDatabase = null;

        foreach ($databases as $database) {
98
            if ($database->getName() === $databaseName) {
99 100 101 102 103
                $foundDatabase = $database;
                break;
            }
        }

104
        $this->assertNotNull($foundDatabase, sprintf('Database %s does not exist on the server', $databaseName));
105 106 107 108

        if ($callback !== null) {
            call_user_func($callback, $foundDatabase);
        }
109
    }
110 111 112 113 114 115

    public function testStartSession()
    {
        if (version_compare($this->getFeatureCompatibilityVersion(), '3.6', '<')) {
            $this->markTestSkipped('startSession() is only supported on FCV 3.6 or higher');
        }
116
        $this->assertInstanceOf(Session::class, $this->client->startSession());
117
    }
118
}