|
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; |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.