ConnectionTest.php 4.63 KB
Newer Older
Jens Segers's avatar
Jens Segers committed
1 2
<?php

3
class ConnectionTest extends TestCase {
Jens Segers's avatar
Jens Segers committed
4

Jens Segers's avatar
Jens Segers committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    public function testConnection()
    {
        $connection = DB::connection('mongodb');
        $this->assertInstanceOf('Jenssegers\Mongodb\Connection', $connection);
    }

    public function testReconnect()
    {
        $c1 = DB::connection('mongodb');
        $c2 = DB::connection('mongodb');
        $this->assertEquals(spl_object_hash($c1), spl_object_hash($c2));

        $c1 = DB::connection('mongodb');
        DB::purge('mongodb');
        $c2 = DB::connection('mongodb');
        $this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2));
    }

    public function testDb()
    {
        $connection = DB::connection('mongodb');
        $this->assertInstanceOf('MongoDB', $connection->getMongoDB());

        $connection = DB::connection('mongodb');
        $this->assertInstanceOf('MongoClient', $connection->getMongoClient());
    }

    public function testCollection()
    {
        $collection = DB::connection('mongodb')->getCollection('unittest');
        $this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);

        $collection = DB::connection('mongodb')->collection('unittests');
        $this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection);

        $collection = DB::connection('mongodb')->table('unittests');
        $this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection);
    }

    public function testDynamic()
    {
        $dbs = DB::connection('mongodb')->listCollections();
        $this->assertTrue(is_array($dbs));
    }

    /*public function testMultipleConnections()
    {
        global $app;

        # Add fake host
        $db = $app['config']['database.connections']['mongodb'];
        $db['host'] = array($db['host'], '1.2.3.4');

        $connection = new Connection($db);
        $mongoclient = $connection->getMongoClient();

        $hosts = $mongoclient->getHosts();
        $this->assertEquals(1, count($hosts));
    }*/

    public function testQueryLog()
    {
        DB::enableQueryLog();

        $this->assertEquals(0, count(DB::getQueryLog()));

        DB::collection('items')->get();
        $this->assertEquals(1, count(DB::getQueryLog()));

        DB::collection('items')->insert(['name' => 'test']);
        $this->assertEquals(2, count(DB::getQueryLog()));

        DB::collection('items')->count();
        $this->assertEquals(3, count(DB::getQueryLog()));

        DB::collection('items')->where('name', 'test')->update(['name' => 'test']);
        $this->assertEquals(4, count(DB::getQueryLog()));

        DB::collection('items')->where('name', 'test')->delete();
        $this->assertEquals(5, count(DB::getQueryLog()));
    }

    public function testSchemaBuilder()
    {
        $schema = DB::connection('mongodb')->getSchemaBuilder();
        $this->assertInstanceOf('Jenssegers\Mongodb\Schema\Builder', $schema);
    }

    public function testDriverName()
    {
        $driver = DB::connection('mongodb')->getDriverName();
        $this->assertEquals('mongodb', $driver);
    }

    public function testAuth()
    {
        Config::set('database.connections.mongodb.username', 'foo');
        Config::set('database.connections.mongodb.password', 'bar');
        $host = Config::get('database.connections.mongodb.host');
        $port = Config::get('database.connections.mongodb.port', 27017);
        $database = Config::get('database.connections.mongodb.database');

        $this->setExpectedExceptionRegExp('MongoConnectionException', "/Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fail/");
        $connection = DB::connection('mongodb');
    }

    public function testCustomPort()
    {
        $port = 27000;
        Config::set('database.connections.mongodb.port', $port);
        $host = Config::get('database.connections.mongodb.host');
        $database = Config::get('database.connections.mongodb.database');

        $this->setExpectedException('MongoConnectionException', "Failed to connect to: $host:$port: Connection refused");
        $connection = DB::connection('mongodb');
    }

    public function testHostWithPorts()
    {
        $hosts = ['localhost:27001', 'localhost:27002'];
        Config::set('database.connections.mongodb.port', 27000);
        Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']);
        $database = Config::get('database.connections.mongodb.database');

        $this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
        $connection = DB::connection('mongodb');
    }
132

133
}