Completed
Pull Request — master (#11)
by Elisha-Wigwe Chijioke
03:32
created

PotatoConnectorTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 20
Bugs 7 Features 8
Metric Value
wmc 18
c 20
b 7
f 8
lcom 1
cbo 2
dl 0
loc 159
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 1
A testGetAdaptar() 0 5 1
A testGetHost() 0 5 1
A testSqliteConnect() 0 5 1
A testSqliteConnectForNoPath() 0 5 1
A testConnectDriverForSQLite() 0 5 1
A testConnectDriverForMySql() 0 12 1
A testConnectDriverforException() 0 4 1
A testGetUsername() 0 5 1
A testGetDBName() 0 5 1
A testGetPassWord() 0 5 1
A testGetConfigurationsIfGivenFilePath() 0 19 2
A testGetConFilePath() 0 5 1
A testSetConnectionFunction() 0 5 1
A testSetConnectionFunctionThrowsException() 0 4 1
A testSetConnection() 0 5 1
A tearDown() 0 4 1
1
<?php
2
3
use Elchroy\PotatoORM\PotatoConnector;
4
use Mockery as m;
5
use org\bovigo\vfs\vfsStream;
6
7
class PotatoConnectorTest extends PHPUnit_Framework_TestCase
8
{
9
    private $root;
10
    private $connector;
11
    private $mockConnection;
0 ignored issues
show
Unused Code introduced by
The property $mockConnection is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
    private $configFile;
13
    private $expectedconfig;
14
    private $adaptar;
15
    private $host;
16
    private $dbname;
17
    private $username;
18
    private $password;
19
20
    public function setUp()
21
    {
22
        $this->expectedconfig = [
23
                'host'        => 'myhost',
24
                'username'    => 'myusername',
25
                'password'    => '',
26
                'dbname'      => 'mydb',
27
                'adaptar'     => 'sqlite',
28
                'sqlite_file' => 'sample.db',
29
            ];
30
        $this->adaptar = $this->expectedconfig['adaptar'];
31
        $this->host = $this->expectedconfig['host'];
32
        $this->dbname = $this->expectedconfig['dbname'];
33
        $this->username = $this->expectedconfig['username'];
34
        $this->password = $this->expectedconfig['password'];
35
36
        $this->root = vfsStream::setup('home');
37
        $this->configFile = vfsStream::url('home/config.ini');
38
39
        $this->connector = new PotatoConnector($this->expectedconfig);
40
    }
41
42
    public function testGetAdaptar()
43
    {
44
        $adaptar = $this->connector->getAdaptar();
45
        $this->assertEquals('sqlite', $adaptar);
46
    }
47
48
    public function testGetHost()
49
    {
50
        $host = $this->connector->getHost();
51
        $this->assertEquals('myhost', $host);
52
    }
53
54
    public function testSqliteConnect()
55
    {
56
        $result = $this->connector->sqliteConnect('sqlite', 'sample.db');
57
        $this->assertInstanceOf('PDO', $result);
58
    }
59
60
    public function testSqliteConnectForNoPath()
61
    {
62
        $result = $this->connector->sqliteConnect('sqlite');
63
        $this->assertInstanceOf('PDO', $result);
64
    }
65
66
    public function testConnectDriverForSQLite()
67
    {
68
        $result = $this->connector->connectDriver('sqlite');
69
        $this->assertInstanceOf('PDO', $result);
70
    }
71
72
    public function testConnectDriverForMySql()
73
    {
74
        $conn = mysqli_connect('127.0.0.1', 'root', '');
75
        mysqli_query($conn, 'CREATE DATABASE IF NOT EXISTS elchroy');
76
        $this->connector->configuration['host'] = '127.0.0.1';
77
        $this->connector->configuration['dbname'] = 'elchroy';
78
        $this->connector->configuration['username'] = 'root';
79
        $this->connector->configuration['password'] = '';
80
        $result = $this->connector->connectDriver('mysql');
81
        $this->assertInstanceOf('PDO', $result);
82
        mysqli_query($conn, 'DROP DATABASE elchroy'); //Destroy the database;
83
    }
84
85
    /**
86
     * @expectedException Elchroy\PotatoORMExceptions\InvalidAdaptarException
87
     *
88
     * @expectedExceptionMessage Invalid Adapter wrongAdaptar : Please provide a driver for the connection to the database.
89
     */
90
    public function testConnectDriverforException()
91
    {
92
        $result = $this->connector->connectDriver('wrongAdaptar');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
    }
94
95
    public function testGetUsername()
96
    {
97
        $username = $this->connector->getUsername();
98
        $this->assertEquals('myusername', $username);
99
    }
100
101
    public function testGetDBName()
102
    {
103
        $dbname = $this->connector->getDBName();
104
        $this->assertEquals('mydb', $dbname);
105
    }
106
107
    public function testGetPassWord()
108
    {
109
        $password = $this->connector->getPassword();
110
        $this->assertEquals('', $password);
111
    }
112
113
    public function testGetConfigurationsIfGivenFilePath()
114
    {
115
        $file = fopen($this->configFile, 'a');
116
        $configData = [
117
                    '[database]',
118
                    'host = myhost',
119
                    'username = myusername',
120
                    'password = ',
121
                    'dbname = mydb',
122
                    'adaptar = sqlite',
123
                    'sqlite_file = sample.db',
124
            ];
125
        foreach ($configData as $cfg) {
126
            fwrite($file, $cfg."\n");
127
        }
128
        fclose($file);
129
        $result = $this->connector->getConfigurations($this->configFile);
130
        $this->assertEquals($this->expectedconfig, $result);
131
    }
132
133
    public function testGetConFilePath()
134
    {
135
        $path = $this->connector->getConfigFilePath($this->configFile);
136
        $this->assertEquals('vfs://home/config.ini', $path);
137
    }
138
139
    public function testSetConnectionFunction()
140
    {
141
        $connection = $this->connector->connect($this->adaptar, $this->host, $this->dbname, $this->username, $this->password);
0 ignored issues
show
Unused Code introduced by
The call to PotatoConnector::connect() has too many arguments starting with $this->host.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
142
        $this->assertInstanceOf('PDO', $connection);
143
    }
144
145
    /**
146
     * @expectedException Elchroy\PotatoORMExceptions\FaultyConnectionException
147
     *
148
     * @expectedExceptionMessage Please provide a driver for the connection to the database.
149
     */
150
    public function testSetConnectionFunctionThrowsException()
151
    {
152
        $connection = $this->connector->connect('wrongAdaptar', 'wrongHostname', 'wrongDbName', 'wrongUsername', 'wrongPassword');
0 ignored issues
show
Unused Code introduced by
The call to PotatoConnector::connect() has too many arguments starting with 'wrongHostname'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
$connection is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
    }
154
155
    public function testSetConnection()
156
    {
157
        $connection = $this->connector->setConnection();
158
        $this->assertInstanceOf('PDO', $connection);
159
    }
160
161
    public function tearDown()
162
    {
163
        m::close();
164
    }
165
}
166