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

DatabaseProxyTest::testGetAndSetQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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 testGetAndSetConnectors()
38
    {
39
        $this->assertInstanceOf(DBConnector::class, $this->proxy->getConnector());
40
        $connector = new MySQLiConnector;
41
        $this->proxy->setConnector($connector);
42
        $this->assertSame($connector, $this->proxy->getConnector());
43
    }
44
45
    public function testGetAndSetDatabaseSchemaManager()
46
    {
47
        $this->assertInstanceOf(DBSchemaManager::class, $this->proxy->getSchemaManager());
48
        $manager = new MySQLSchemaManager;
49
        $this->proxy->setSchemaManager($manager);
50
        $this->assertSame($manager, $this->proxy->getSchemaManager());
51
    }
52
53
    public function testGetAndSetQueryBuilder()
54
    {
55
        $this->assertInstanceOf(DBQueryBuilder::class, $this->proxy->getQueryBuilder());
56
        $queryBuilder = new MySQLQueryBuilder;
57
        $this->proxy->setQueryBuilder($queryBuilder);
58
        $this->assertSame($queryBuilder, $this->proxy->getQueryBuilder());
59
    }
60
61
    /**
62
     * Test method to ensure that a set of methods are proxied through to the real connection. This test covers
63
     * all methods listed below:
64
     */
65
    public function testMethodsAreProxiedToRealConnection()
66
    {
67
        $proxyMethods = array(
68
            'addslashes',
69
            'alterTable',
70
            'comparisonClause',
71
            'createDatabase',
72
            'createField',
73
            'createTable',
74
            'datetimeDifferenceClause',
75
            'datetimeIntervalClause',
76
            'enumValuesForField',
77
            'fieldList',
78
            'formattedDatetimeClause',
79
            'getConnect',
80
            'getGeneratedID',
81
            'hasTable',
82
            'isActive',
83
            'renameField',
84
            'renameTable',
85
            'supportsTimezoneOverride',
86
            'supportsTransactions',
87
            'tableList',
88
            'transactionEnd',
89
            'transactionRollback',
90
            'transactionSavepoint',
91
            'transactionStart',
92
            'clearTable',
93
            'getDatabaseServer',
94
            'now',
95
            'random',
96
            'searchEngine',
97
            'supportsCollations',
98
        );
99
100
        $mockConnection = $this->getMockBuilder(MySQLDatabase::class)->setMethods($proxyMethods)->getMock();
101
        $proxy = new DatabaseProxy($mockConnection);
102
103
        foreach ($proxyMethods as $proxyMethod) {
104
            $mockConnection->expects($this->once())->method($proxyMethod);
105
            // Pass mock arguments - the large number is in searchEngine
106
            $proxy->$proxyMethod(null, null, null, null, null, null, null, null);
107
        }
108
    }
109
110
    /**
111
     * Test whether passing an array to the constructor still produces a DBConnector instance
112
     */
113
    public function testConstructorArguments()
114
    {
115
        global $databaseConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
116
        $newProxy = new DatabaseProxy($databaseConfig);
117
        $this->assertInstanceOf(DBConnector::class, $newProxy->getConnector());
118
    }
119
}
120