Skip to content

Commit ec705c6

Browse files
committed
tests, refactor/corrections for databse config and access
1 parent fd814ce commit ec705c6

15 files changed

+76
-57
lines changed

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ezsql/ezdba",
33
"description": "Advance database access library. Make interacting with a database ridiculously easy.",
4-
"keywords": ["mysql", "mysqli", "postgres", "mssql", "sqlsrv", "sqlserver", "pdo", "sqlite", "sqlite3", "database", "abstraction", "sql", "dba"],
4+
"keywords": ["mysql", "mysqli", "postgresql", "mssql", "sqlsrv", "sqlserver", "pdo", "sqlite", "sqlite3", "database", "abstraction", "sql", "dba"],
55
"license": "LGPL-3.0-or-later",
66
"authors": [
77
{
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"name": "l. stubbs",
17-
"email": "lstubbs@technoexpress.net"
17+
"email": "lstubbs@techno.express"
1818
}
1919
],
2020
"support": {
@@ -37,8 +37,7 @@
3737
}
3838
},
3939
"require-dev": {
40-
"phpunit/phpunit": ">5.7.0",
41-
"php-coveralls/php-coveralls": "^2.1"
40+
"phpunit/phpunit": ">5.7.0"
4241
},
4342
"autoload-dev": {
4443
"psr-4": {

lib/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private function setupSqlite3($args) {
133133
* @param string $connectionString
134134
* @throws Exception If vendor specifics not provided.
135135
*/
136-
public function parseConnectionString(string $connectionString, array $check_for)
136+
private function parseConnectionString(string $connectionString, array $check_for)
137137
{
138138
$params = \explode(";", $connectionString);
139139

lib/DInjector.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace ezsql;
66

77
use Psr\Container\ContainerInterface;
8+
use ezsql\Exception\ContainerException;
9+
use ezsql\Exception\NotFoundException;
810

911
/**
1012
* Dependency Injection Container
@@ -19,8 +21,10 @@ class DInjector implements ContainerInterface
1921
protected $instances = [];
2022

2123
/**
22-
* @param $abstract
23-
* @param null $concrete
24+
* Register a service with the container.
25+
*
26+
* @param string $abstract - className
27+
* @param string $concrete - friendlyName
2428
*/
2529
public function set($abstract, $concrete = NULL)
2630
{
@@ -41,44 +45,43 @@ public function register($abstract, $concrete = NULL)
4145
/**
4246
* Execute with any dependencies
4347
*
44-
* @param $abstract
48+
* @param $abstract
4549
* @param array $values
4650
*
4751
* @return mixed
48-
* @throws Exception
52+
* @throws ContainerException
4953
*/
5054
public function call($abstract, $values = [])
5155
{
5256
$concrete = $this->instances[$abstract];
5357
if ($concrete instanceof \Closure) {
5458
return $concrete($this, $values);
5559
} else
56-
throw new \Exception("{$concrete} is not callable");
60+
throw new ContainerException("{$concrete} is not callable");
5761
}
5862

5963
/**
60-
* @param $abstract
61-
* @param array $values
64+
* @param $abstract
6265
*
6366
* @return null|object
64-
* @throws Exception
67+
* @throws NotFoundException
6568
*/
6669
public function get($abstract)
6770
{
6871
if (!$this->has($abstract)) {
69-
throw new \Exception("{$abstract} does not exists");
72+
throw new NotFoundException("{$abstract} does not exists");
7073
}
7174
return $this->instances[$abstract];
7275
}
7376

7477
/**
7578
* Auto setup, execute, or resolve any dependencies.
7679
*
77-
* @param $abstract
80+
* @param $abstract
7881
* @param array $values
7982
*
8083
* @return mixed|null|object
81-
* @throws Exception
84+
* @throws ContainerException
8285
*/
8386
public function autoWire($abstract, $values = [])
8487
{
@@ -92,12 +95,12 @@ public function autoWire($abstract, $values = [])
9295

9396
/**
9497
* Do we have dependence
95-
* @param $abstract
96-
* @return bool
97-
*/
98-
public function has($abstract)
99-
{
100-
return isset($this->instances[$abstract]);
98+
* @param $abstract
99+
* @return bool
100+
*/
101+
public function has($abstract): bool
102+
{
103+
return isset($this->instances[$abstract]);
101104
}
102105

103106
/**
@@ -107,18 +110,18 @@ public function has($abstract)
107110
* @param $values
108111
*
109112
* @return mixed|object
110-
* @throws Exception
113+
* @throws ContainerException
111114
*/
112-
public function resolve($concrete, $values = [])
115+
protected function resolve($concrete, $values = [])
113116
{
114117
if ($concrete instanceof \Closure) {
115118
return $concrete($this, $values);
116119
}
117120

118-
$reflector = new ReflectionClass($concrete);
121+
$reflector = new \ReflectionClass($concrete);
119122
// check if class is instantiable
120123
if (!$reflector->isInstantiable()) {
121-
throw new \Exception("Class {$concrete} is not instantiable");
124+
throw new ContainerException("Class {$concrete} is not instantiable");
122125
}
123126

124127
// get class constructor
@@ -129,7 +132,7 @@ public function resolve($concrete, $values = [])
129132
}
130133

131134
// get constructor params
132-
$parameters = $constructor->getParameters();
135+
$parameters = $constructor->getParameters();
133136
$dependencies = $this->getDependencies($parameters, $values);
134137

135138
// get new instance with dependencies resolved
@@ -142,7 +145,7 @@ public function resolve($concrete, $values = [])
142145
* @param $parameters
143146
*
144147
* @return array
145-
* @throws Exception
148+
* @throws ContainerException
146149
*/
147150
public function getDependencies($parameters, $values)
148151
{
@@ -152,21 +155,21 @@ public function getDependencies($parameters, $values)
152155
$dependency = $parameter->getClass();
153156
if ($dependency === NULL) {
154157
// check if the constructor parameter name exists as a key in the values array
155-
if (array_key_exists($parameter->getName(), $values)) {
158+
if (\array_key_exists($parameter->getName(), $values)) {
156159
// get default value of parameter
157160
$dependencies[] = $values[$parameter->getName()];
158161
} else {
159162
// check if default value for a parameter is available
160163
if ($parameter->isDefaultValueAvailable()) {
161-
// get default value of parameter
162-
$dependencies[] = $parameter->getDefaultValue();
164+
// get default value of parameter
165+
$dependencies[] = $parameter->getDefaultValue();
163166
} else {
164-
throw new \Exception("Can not resolve class dependency {$parameter->name}");
167+
throw new ContainerException("Can not resolve class dependency {$parameter->name}");
165168
}
166169
}
167170
} else {
168171
// get dependency resolved
169-
$dependencies[] = $this->get($dependency->name);
172+
$dependencies[] = $this->autoWire($dependency->name, $values);
170173
}
171174
}
172175

lib/Database.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ private function __wakeup() {}
2929
/**
3030
* Initialize and connect a vendor database.
3131
*
32-
* @param object $settings - Has SQL driver and connection parameters
32+
* @param object $settings - Has SQL driver connection parameters
3333
*/
34-
public static function initialize(Configuration $settings)
34+
public static function initialize(string $vendor, $settings)
3535
{
36-
if (empty($settings) || (!$settings instanceof Configuration)) {
36+
if (empty($settings) || empty($vendor)) {
3737
throw new \Exception('<b>Fatal Error:</b> Missing configuration details to connect to database');
3838
} else {
3939
self::$_ts = \microtime();
40-
self::$database = $settings;
40+
self::$database = new Configuration($vendor, $settings);
4141
$key = self::$database->getDriver();
4242
$value = \VENDOR[$key];
4343

@@ -55,12 +55,17 @@ public static function initialize(Configuration $settings)
5555
*
5656
* @return array|float time elapsed, memory usage.
5757
*/
58-
public function benchmark()
58+
public static function benchmark()
5959
{
6060
return [
61-
'start' => $this->_ts,
62-
'elapse' => \microtime() - $this->_ts,
61+
'start' => self::$_ts,
62+
'elapse' => \microtime() - self::$_ts,
6363
'memory' => \memory_get_usage(true),
6464
];
6565
}
66+
67+
public static function settings()
68+
{
69+
return self::$database;
70+
}
6671
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ezsql\Exception;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
use RuntimeException;
7+
8+
class ContainerException extends RuntimeException implements ContainerExceptionInterface
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ezsql\Exception;
4+
5+
use Psr\Container\NotFoundExceptionInterface;
6+
use InvalidArgumentException;
7+
8+
class NotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
9+
{
10+
}

pause.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717
# Commands to be run before your environment runs.
1818
before_script:
1919
- composer self-update
20-
- composer install --dev --no-interaction
20+
- composer require php-coveralls/php-coveralls
2121
- mysql -e 'CREATE DATABASE IF NOT EXISTS ez_test;'
2222
- mysql -e 'GRANT ALL PRIVILEGES ON ez_test.* TO ez_test@localhost;'
2323
- mysql -e "SET PASSWORD FOR 'ez_test'@'localhost' = PASSWORD('ezTest')"

tests/mysqli/mysqliTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ protected function setUp(): void
2525
);
2626
}
2727

28-
$setting = new Configuration('mysqli', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
29-
$this->object = Database::initialize($setting);
28+
$this->object = Database::initialize('mysqli', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]);
3029
$this->object->setPrepare();
3130
}
3231

tests/pdo/pdo_mysqlTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ protected function setUp(): void
2525
);
2626
}
2727

28-
$setting = new Configuration('pdo', ['mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port='.self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
29-
$this->object = Database::initialize($setting);
28+
$this->object = Database::initialize('pdo', ['mysql:host='.self::TEST_DB_HOST.';dbname='. self::TEST_DB_NAME.';port='.self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
3029
$this->object->setPrepare();
3130
} // setUp
3231

tests/pdo/pdo_pgsqlTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ protected function setUp(): void
3535
);
3636
}
3737

38-
$setting = new Configuration('pdo', ['pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
39-
$this->object = Database::initialize($setting);
38+
$this->object = Database::initialize('pdo', ['pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD]);
4039
$this->object->setPrepare();
4140
} // setUp
4241

0 commit comments

Comments
 (0)