Skip to content

Commit b370c50

Browse files
committed
have Database config setup to only accept array, removed string options, more code coverage tests
1 parent 9f1c5ad commit b370c50

File tree

4 files changed

+154
-95
lines changed

4 files changed

+154
-95
lines changed

lib/Config.php

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
class Config extends ConfigAbstract implements ConfigInterface
3838
{
39-
public function __construct(string $driver = '', $arguments = null)
39+
public function __construct(string $driver = '', array $arguments = null)
4040
{
4141
$sql = \strtolower($driver);
4242
if (!\array_key_exists($sql, \VENDOR) || empty($arguments)) {
@@ -94,7 +94,7 @@ public function __construct(string $driver = '', $arguments = null)
9494
* for: sqlite3
9595
* - (filePath, database) - filePath|args[0] // The path to open an SQLite database
9696
*/
97-
public static function initialize(string $driver = '', $arguments = null)
97+
public static function initialize(string $driver = '', array $arguments = null)
9898
{
9999
return new self($driver, $arguments);
100100
}
@@ -103,9 +103,8 @@ private function setupMysqli($args)
103103
{
104104
if ( ! \function_exists ('mysqli_connect') )
105105
throw new Exception('<b>Fatal Error:</b> ez_mysql requires mySQLi Lib to be compiled and or linked in to the PHP engine');
106-
elseif (\is_string($args))
107-
$this->parseConnectionString($args, ['user', 'name', 'password']);
108-
elseif (\count($args)>=3) {
106+
107+
if (\count($args)>=3) {
109108
$this->setUser($args[0]);
110109
$this->setPassword($args[1]);
111110
$this->setName($args[2]);
@@ -121,9 +120,7 @@ private function setupPdo($args)
121120
{
122121
if ( ! \class_exists ('PDO') )
123122
throw new Exception('<b>Fatal Error:</b> ez_pdo requires PDO Lib to be compiled and or linked in to the PHP engine');
124-
elseif (\is_string($args))
125-
$this->parseConnectionString($args, ['dsn', 'user', 'password']);
126-
elseif (\count($args)>=3) {
123+
if (\count($args)>=3) {
127124
$this->setDsn($args[0]);
128125
$this->setUser($args[1]);
129126
$this->setPassword($args[2]);
@@ -137,9 +134,8 @@ private function setupSqlsrv($args)
137134
{
138135
if ( ! \function_exists ('sqlsrv_connect') )
139136
throw new Exception('<b>Fatal Error:</b> ez_sqlsrv requires the php_sqlsrv.dll or php_pdo_sqlsrv.dll to be installed. Also enable MS-SQL extension in PHP.ini file ');
140-
elseif (\is_string($args))
141-
$this->parseConnectionString($args, ['user', 'password', 'name']);
142-
elseif (\count($args)>=3) {
137+
138+
if (\count($args)>=3) {
143139
$this->setUser($args[0]);
144140
$this->setPassword($args[1]);
145141
$this->setName($args[2]);
@@ -153,9 +149,8 @@ private function setupPgsql($args)
153149
{
154150
if ( ! \function_exists ('pg_connect') )
155151
throw new Exception('<b>Fatal Error:</b> ez_pgsql requires PostgreSQL Lib to be compiled and or linked in to the PHP engine');
156-
elseif (\is_string($args))
157-
$this->parseConnectionString($args, ['user', 'password', 'name']);
158-
elseif (count($args)>=3) {
152+
153+
if (count($args)>=3) {
159154
$this->setUser($args[0]);
160155
$this->setPassword($args[1]);
161156
$this->setName($args[2]);
@@ -168,46 +163,11 @@ private function setupPgsql($args)
168163
private function setupSqlite3($args) {
169164
if ( ! \class_exists ('SQLite3') )
170165
throw new Exception('<b>Fatal Error:</b> ez_sqlite3 requires SQLite3 Lib to be compiled and or linked in to the PHP engine');
171-
elseif (\is_string($args))
172-
$this->parseConnectionString($args, ['path', 'name']);
173-
elseif (\count($args)==2) {
166+
167+
if (\count($args)==2) {
174168
$this->setPath($args[0]);
175169
$this->setName($args[1]);
176170
} else
177171
throw new Exception(\MISSING_CONFIGURATION);
178172
}
179-
180-
/**
181-
* @param string $connectionString
182-
* @throws Exception If vendor specifics not provided.
183-
*/
184-
private function parseConnectionString(string $connectionString, array $check_for)
185-
{
186-
$params = \explode(";", $connectionString);
187-
188-
if (\count($params) === 1) { // Attempt to explode on a space if no ';' are found.
189-
$params = \explode(" ", $connectionString);
190-
}
191-
192-
foreach ($params as $param) {
193-
list($key, $value) = \array_map("trim", \explode("=", $param, 2) + [1 => null]);
194-
195-
if (isset(\KEY_MAP[$key])) {
196-
$key = \KEY_MAP[$key];
197-
}
198-
199-
if (!in_array($key, \ALLOWED_KEYS, true)) {
200-
throw new Exception("Invalid key in connection string: " . $key);
201-
}
202-
203-
$this->{$key} = $value;
204-
}
205-
206-
foreach ($check_for as $must_have) {
207-
if(!isset($this->{$must_have}))
208-
throw new Exception("Required parameters ".$must_have." need to be passed in connection string");
209-
}
210-
211-
return true;
212-
}
213173
}

lib/ConfigInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ interface ConfigInterface
3636
* @param string $driver
3737
* @param string|array $arguments
3838
*/
39-
public static function initialize(string $driver = '', $arguments = null);
39+
public static function initialize(string $driver = '', array $arguments = null);
4040
}

lib/Constants.php

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -88,49 +88,6 @@
8888
\define('SQLSERVER', 'sqlsrv');
8989
\define('MSSQL', 'sqlsrv');
9090

91-
\define('ALLOWED_KEYS', [
92-
'host',
93-
'hostname',
94-
'user',
95-
'username',
96-
'password',
97-
'database',
98-
'db',
99-
'name',
100-
'dsn',
101-
'char',
102-
'charset',
103-
'path',
104-
'port',
105-
'file',
106-
'filebase',
107-
'tosql',
108-
'tomssql',
109-
'options'
110-
]);
111-
112-
\define('KEY_MAP', [
113-
'host' => 'host',
114-
'hostname' => 'host',
115-
'user' => 'user',
116-
'username' => 'user',
117-
'pass' => 'password',
118-
'password' => 'password',
119-
'database' => 'name',
120-
'db' => 'name',
121-
'name' => 'name',
122-
'dsn' => 'dsn',
123-
'char' => 'charset',
124-
'charset' => 'charset',
125-
'path' => 'path',
126-
'port' => 'port',
127-
'file' => 'isFile',
128-
'filebase' => 'isFile',
129-
'tosql' => 'toMssql',
130-
'tomssql' => 'toMssql',
131-
'options' => 'options'
132-
]);
133-
13491
// String SQL data types
13592
\define('CHAR', 'CHAR');
13693
\define('VARCHAR', 'VARCHAR');

tests/ConfigTest.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ public function testSetupMysqli()
2727
$this->assertEquals(self::TEST_DB_NAME, $settings->getName());
2828
}
2929

30+
public function testInitializeMysqli()
31+
{
32+
if (!extension_loaded('mysqli')) {
33+
$this->markTestSkipped(
34+
'The MySQLi extension is not available.'
35+
);
36+
}
37+
38+
$settings = Config::initialize('mysqli', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
39+
$this->assertTrue($settings instanceof ConfigInterface);
40+
}
41+
42+
public function testErrorMysqli()
43+
{
44+
if (!extension_loaded('mysqli')) {
45+
$this->markTestSkipped(
46+
'The MySQLi extension is not available.'
47+
);
48+
}
49+
50+
$this->expectException(\Exception::class);
51+
$this->expectExceptionMessageRegExp('/[Missing configuration details to connect to database]/');
52+
$settings = Config::initialize('mysqli', [self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
53+
}
54+
3055
/**
3156
* @covers ezsql\Config::SetupPdo
3257
*/
@@ -38,6 +63,19 @@ public function testSetupPdo()
3863
);
3964
}
4065

66+
$dsn = 'mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port=3306';
67+
$settings = new Config('pdo', [$dsn, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
68+
$this->assertTrue($settings instanceof ConfigAbstract);
69+
}
70+
71+
public function testInitializePdo()
72+
{
73+
if ( ! \class_exists ('PDO') ) {
74+
$this->markTestSkipped(
75+
'The PDO Lib is not available.'
76+
);
77+
}
78+
4179
$dsn = 'mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port=3306';
4280
$settings = Config::initialize('pdo', [$dsn, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
4381
$this->assertTrue($settings instanceof ConfigAbstract);
@@ -46,6 +84,35 @@ public function testSetupPdo()
4684
$this->assertEquals(self::TEST_DB_PASSWORD, $settings->getPassword());
4785
}
4886

87+
public function testErrorPdo()
88+
{
89+
if ( ! \class_exists ('PDO') ) {
90+
$this->markTestSkipped(
91+
'The PDO Lib is not available.'
92+
);
93+
}
94+
95+
$dsn = 'mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port=3306';
96+
$this->expectException(\Exception::class);
97+
$this->expectExceptionMessageRegExp('/[Missing configuration details to connect to database]/');
98+
$settings = Config::initialize('pdo', [$dsn]);
99+
}
100+
101+
public function test__callPdo()
102+
{
103+
if ( ! \class_exists ('PDO') ) {
104+
$this->markTestSkipped(
105+
'The PDO Lib is not available.'
106+
);
107+
}
108+
109+
$dsn = 'mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port=3306';
110+
$this->expectException(\Exception::class);
111+
$this->expectExceptionMessageRegExp('/[does not exist]/');
112+
$settings = new Config('pdo', [$dsn, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
113+
$settings->getNotAnProperty();
114+
}
115+
49116
/**
50117
* @covers ezsql\Config::SetupPgsql
51118
*/
@@ -66,6 +133,31 @@ public function testSetupPgsql()
66133
$this->assertEquals(self::TEST_DB_PORT, $settings->getPort());
67134
}
68135

136+
public function testInitializePgsql()
137+
{
138+
if (!extension_loaded('pgsql')) {
139+
$this->markTestSkipped(
140+
'The PostgreSQL Lib is not available.'
141+
);
142+
}
143+
144+
$settings = Config::initialize('pgsql', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT]);
145+
$this->assertTrue($settings instanceof ConfigInterface);
146+
}
147+
148+
public function testErrorPgsql()
149+
{
150+
if (!extension_loaded('pgsql')) {
151+
$this->markTestSkipped(
152+
'The PostgreSQL Lib is not available.'
153+
);
154+
}
155+
156+
$this->expectException(\Exception::class);
157+
$this->expectExceptionMessageRegExp('/[Missing configuration details to connect to database]/');
158+
$settings = Config::initialize('pgsql', [self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
159+
}
160+
69161
/**
70162
* @covers ezsql\Config::SetupSqlsrv
71163
*/
@@ -84,6 +176,31 @@ public function testSetupSqlsrv()
84176
$this->assertEquals(self::TEST_DB_NAME, $settings->getName());
85177
}
86178

179+
public function testInitializeSqlsrv()
180+
{
181+
if (!extension_loaded('sqlsrv')) {
182+
$this->markTestSkipped(
183+
'The sqlsrv Lib is not available.'
184+
);
185+
}
186+
187+
$settings = Config::initialize('sqlsrv', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
188+
$this->assertTrue($settings instanceof ConfigAbstract);
189+
}
190+
191+
public function testErrorSqlsrv()
192+
{
193+
if (!extension_loaded('sqlsrv')) {
194+
$this->markTestSkipped(
195+
'The sqlsrv Lib is not available.'
196+
);
197+
}
198+
199+
$this->expectException(\Exception::class);
200+
$this->expectExceptionMessageRegExp('/[Missing configuration details to connect to database]/');
201+
$settings = new Config('sqlsrv', [self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
202+
}
203+
87204
/**
88205
* @covers ezsql\Config::SetupSqlite3
89206
*/
@@ -100,6 +217,31 @@ public function testSetupSqlite3()
100217
$this->assertEquals(self::TEST_SQLITE_DB_DIR, $settings->getPath());
101218
$this->assertEquals(self::TEST_SQLITE_DB, $settings->getName());
102219
}
220+
221+
public function testInitializeSqlite3()
222+
{
223+
if (!extension_loaded('sqlite3')) {
224+
$this->markTestSkipped(
225+
'The sqlite3 Lib is not available.'
226+
);
227+
}
228+
229+
$settings = Config::initialize('sqlite3', [self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB]);
230+
$this->assertTrue($settings instanceof ConfigInterface);
231+
}
232+
233+
public function testErrorSqlite3()
234+
{
235+
if (!extension_loaded('sqlite3')) {
236+
$this->markTestSkipped(
237+
'The sqlite3 Lib is not available.'
238+
);
239+
}
240+
241+
$this->expectException(\Exception::class);
242+
$this->expectExceptionMessageRegExp('/[Missing configuration details to connect to database]/');
243+
$settings = new Config('sqlite3', [self::TEST_SQLITE_DB_DIR]);
244+
}
103245

104246
/**
105247
* @covers ezsql\Config::__construct

0 commit comments

Comments
 (0)