Completed
Push — master ( ef5eaa...6d70d2 )
by Lars
03:21 queued 30s
created

MultipleConnectionTest::testFindOneOverDifferentConnections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
use idiorm\orm\ORM;
4
5
class MultipleConnectionTest extends PHPUnit_Framework_TestCase {
6
7
    const ALTERNATE = 'alternate'; // Used as name of alternate connection
8
9
    public function setUp() {
10
        // Set up the dummy database connections
11
        ORM::set_db(new MockPDO('sqlite::memory:'));
12
        ORM::set_db(new MockDifferentPDO('sqlite::memory:'), self::ALTERNATE);
13
14
        // Enable logging
15
        ORM::configure('logging', true);
16
        ORM::configure('logging', true, self::ALTERNATE);
17
    }
18
19
    public function tearDown() {
20
        ORM::reset_config();
21
        ORM::reset_db();
22
    }
23
24
    public function testMultiplePdoConnections() {
25
        $this->assertInstanceOf('MockPDO', ORM::get_db());
26
        $this->assertInstanceOf('MockPDO', ORM::get_db(ORM::DEFAULT_CONNECTION));
27
        $this->assertInstanceOf('MockDifferentPDO', ORM::get_db(self::ALTERNATE));
28
    }
29
30
    public function testRawExecuteOverAlternateConnection() {
31
        $expected = "SELECT * FROM `foo`";
32
        ORM::raw_execute("SELECT * FROM `foo`", array(), self::ALTERNATE);
33
34
        $this->assertEquals($expected, ORM::get_last_query(self::ALTERNATE));
35
    }
36
37
    public function testFindOneOverDifferentConnections() {
38
        ORM::for_table('widget')->find_one();
39
        $statementOne = ORM::get_last_statement();
40
        $this->assertInstanceOf('MockPDOStatement', $statementOne);
41
42
        ORM::for_table('person', self::ALTERNATE)->find_one();
43
        $statementOne = ORM::get_last_statement(); // get_statement is *not* per connection
44
        $this->assertInstanceOf('MockDifferentPDOStatement', $statementOne);
45
46
        $expected = "SELECT * FROM `widget` LIMIT 1";
47
        $this->assertNotEquals($expected, ORM::get_last_query()); // Because get_last_query() is across *all* connections
48
        $this->assertEquals($expected, ORM::get_last_query(ORM::DEFAULT_CONNECTION));
49
50
        $expectedToo = "SELECT * FROM `person` LIMIT 1";
51
        $this->assertEquals($expectedToo, ORM::get_last_query(self::ALTERNATE));
52
    }
53
54
}