ClientFunctionalTest.php 2.87 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
    public function testDropDatabase()
    {
27 28 29 30
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

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

33
        $commandResult = $this->client->dropDatabase($this->getDatabaseName());
34 35 36
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
37 38 39

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

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

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        $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.
     *
69
     * @param string $databaseName
70 71 72 73 74 75 76
     * @param callable $callback
     */
    private function assertDatabaseExists($databaseName, $callback = null)
    {
        if ($callback !== null && ! is_callable($callback)) {
            throw new InvalidArgumentException('$callback is not a callable');
        }
77

78
        $databases = $this->client->listDatabases();
79 80 81 82

        $foundDatabase = null;

        foreach ($databases as $database) {
83
            if ($database->getName() === $databaseName) {
84 85 86 87 88
                $foundDatabase = $database;
                break;
            }
        }

89
        $this->assertNotNull($foundDatabase, sprintf('Database %s does not exist on the server', $databaseName));
90 91 92 93

        if ($callback !== null) {
            call_user_func($callback, $foundDatabase);
        }
94
    }
95
}