Completed
Pull Request — master (#47)
by Robbie
01:29
created

DatabaseProxyTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 86
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGetAndSetShowQueries() 0 7 1
A testGetAndSetDatabaseSchemaManager() 0 7 1
A testGetAndSetQueryBuilder() 0 7 1
B testMethodsAreProxiedToRealConnection() 0 44 2
1
<?php
2
3
namespace LeKoala\DebugBar\Test\Proxy;
4
5
use LeKoala\DebugBar\Proxy\DatabaseProxy;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\ORM\Connect\DBConnector;
8
use SilverStripe\ORM\Connect\DBSchemaManager;
9
use SilverStripe\ORM\Connect\DBQueryBuilder;
10
use SilverStripe\ORM\Connect\MySQLDatabase;
11
use SilverStripe\ORM\Connect\MySQLiConnector;
12
use SilverStripe\ORM\Connect\MySQLSchemaManager;
13
use SilverStripe\ORM\Connect\MySQLQueryBuilder;
14
use SilverStripe\ORM\DB;
15
16
class DatabaseProxyTest extends SapphireTest
17
{
18
    /**
19
     * @var DebugBarDatabaseProxy
20
     */
21
    protected $proxy;
22
23
    public function setUp()
24
    {
25
        parent::setUp();
26
        $this->proxy = new DatabaseProxy(DB::get_conn());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \LeKoala\DebugBar\Pr...ipe\ORM\DB::get_conn()) of type object<LeKoala\DebugBar\Proxy\DatabaseProxy> is incompatible with the declared type object<LeKoala\DebugBar\...\DebugBarDatabaseProxy> of property $proxy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
    }
28
29
    public function testGetAndSetShowQueries()
30
    {
31
        $this->proxy->setShowQueries(true);
32
        $this->assertTrue($this->proxy->getShowQueries());
33
        $this->proxy->setShowQueries(false);
34
        $this->assertFalse($this->proxy->getShowQueries());
35
    }
36
37
    public function testGetAndSetDatabaseSchemaManager()
38
    {
39
        $this->assertInstanceOf(DBSchemaManager::class, $this->proxy->getSchemaManager());
40
        $manager = new MySQLSchemaManager;
41
        $this->proxy->setSchemaManager($manager);
42
        $this->assertSame($manager, $this->proxy->getSchemaManager());
43
    }
44
45
    public function testGetAndSetQueryBuilder()
46
    {
47
        $this->assertInstanceOf(DBQueryBuilder::class, $this->proxy->getQueryBuilder());
48
        $queryBuilder = new MySQLQueryBuilder;
49
        $this->proxy->setQueryBuilder($queryBuilder);
50
        $this->assertSame($queryBuilder, $this->proxy->getQueryBuilder());
51
    }
52
53
    /**
54
     * Test method to ensure that a set of methods are proxied through to the real connection. This test covers
55
     * all methods listed below:
56
     */
57
    public function testMethodsAreProxiedToRealConnection()
58
    {
59
        $proxyMethods = array(
60
            'addslashes',
61
            'alterTable',
62
            'comparisonClause',
63
            'createDatabase',
64
            'createField',
65
            'createTable',
66
            'datetimeDifferenceClause',
67
            'datetimeIntervalClause',
68
            'enumValuesForField',
69
            'fieldList',
70
            'formattedDatetimeClause',
71
            'getConnect',
72
            'getGeneratedID',
73
            'hasTable',
74
            'isActive',
75
            'renameField',
76
            'renameTable',
77
            'supportsTimezoneOverride',
78
            'supportsTransactions',
79
            'tableList',
80
            'transactionEnd',
81
            'transactionRollback',
82
            'transactionSavepoint',
83
            'transactionStart',
84
            'clearTable',
85
            'getDatabaseServer',
86
            'now',
87
            'random',
88
            'searchEngine',
89
            'supportsCollations',
90
        );
91
92
        $mockConnection = $this->getMockBuilder(MySQLDatabase::class)->setMethods($proxyMethods)->getMock();
93
        $proxy = new DatabaseProxy($mockConnection);
94
95
        foreach ($proxyMethods as $proxyMethod) {
96
            $mockConnection->expects($this->once())->method($proxyMethod);
97
            // Pass mock arguments - the large number is in searchEngine
98
            $proxy->$proxyMethod(null, null, null, null, null, null, null, null);
99
        }
100
    }
101
}
102