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

Jens Segers's avatar
Jens Segers committed
3 4
class ConnectionTest extends TestCase
{
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
    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');
Jens Segers's avatar
Jens Segers committed
26
        $this->assertInstanceOf('MongoDB\Database', $connection->getMongoDB());
Jens Segers's avatar
Jens Segers committed
27 28

        $connection = DB::connection('mongodb');
Jens Segers's avatar
Jens Segers committed
29
        $this->assertInstanceOf('MongoDB\Client', $connection->getMongoClient());
Jens Segers's avatar
Jens Segers committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
    }

    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);
    }

Jens Segers's avatar
Jens Segers committed
44 45 46 47 48
    // public function testDynamic()
    // {
    //     $dbs = DB::connection('mongodb')->listCollections();
    //     $this->assertTrue(is_array($dbs));
    // }
Jens Segers's avatar
Jens Segers committed
49

Jens Segers's avatar
Jens Segers committed
50 51 52
    // public function testMultipleConnections()
    // {
    //     global $app;
Jens Segers's avatar
Jens Segers committed
53

Jens Segers's avatar
Jens Segers committed
54 55 56
    //     # Add fake host
    //     $db = $app['config']['database.connections']['mongodb'];
    //     $db['host'] = array($db['host'], '1.2.3.4');
Jens Segers's avatar
Jens Segers committed
57

Jens Segers's avatar
Jens Segers committed
58 59
    //     $connection = new Connection($db);
    //     $mongoclient = $connection->getMongoClient();
Jens Segers's avatar
Jens Segers committed
60

Jens Segers's avatar
Jens Segers committed
61 62 63
    //     $hosts = $mongoclient->getHosts();
    //     $this->assertEquals(1, count($hosts));
    // }
Jens Segers's avatar
Jens Segers committed
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

    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');

Jens Segers's avatar
Jens Segers committed
107
        // $this->setExpectedExceptionRegExp('MongoConnectionException', "/Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fail/");
Jens Segers's avatar
Jens Segers committed
108 109 110 111 112 113 114 115 116 117
        $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');

Jens Segers's avatar
Jens Segers committed
118
        // $this->setExpectedException('MongoConnectionException', "Failed to connect to: $host:$port: Connection refused");
Jens Segers's avatar
Jens Segers committed
119 120 121 122 123 124 125 126 127 128
        $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');

Jens Segers's avatar
Jens Segers committed
129
        // $this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
Jens Segers's avatar
Jens Segers committed
130 131
        $connection = DB::connection('mongodb');
    }
132
}