Skip to content

Commit f07dc52

Browse files
committed
Added db tests
1 parent eed9451 commit f07dc52

8 files changed

Lines changed: 1420 additions & 43 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,6 @@ In the majority of cases, the most used build-in function with underscore are re
1212
composer require floatphp/classes
1313
```
1414

15-
## Contributing:
16-
17-
Please read [CONTRIBUTING.md](https://github.com/floatphp/Classes/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
18-
19-
## Versioning:
20-
21-
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/floatphp/Classes/tags).
22-
23-
## Authors:
24-
25-
* **Jihad Sinnaour** - [Jakiboy](https://github.com/Jakiboy) (*Initial work*)
26-
27-
See also the full list of [contributors](https://github.com/floatphp/Classes/contributors) who participated in this project. Any suggestions (Pull requests) are welcome!
28-
29-
## License:
30-
31-
This project is licensed under the MIT License - see the [LICENSE](https://github.com/floatphp/Classes/blob/master/LICENSE) file for details.
32-
3315
## ⭐ Support:
3416

35-
Please give it a Star if you like the project.
17+
Skip the coffee! If you like the project, a **Star** would mean a lot.

‎composer.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@
2424
"php": ">=8.2",
2525
"floatphp/interfaces": "^1.5.0"
2626
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^11.5"
29+
},
2730
"autoload": {
2831
"psr-4": {
2932
"FloatPHP\\Classes\\": "src/"
3033
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"FloatPHP\\Tests\\Classes\\": "tests/"
38+
}
3139
}
3240
}

‎src/Connection/Db.php‎

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ class Db
2727
{
2828
/**
2929
* @access protected
30-
* @var object $pdo
31-
* @var object $query
30+
* @var ?PDO $pdo
31+
* @var ?\PDOStatement $query
3232
* @var bool $isConnected
3333
* @var array $parameters
34-
* @var object $logger
34+
* @var ?LoggerInterface $logger
3535
*/
36-
protected $pdo;
37-
protected $query;
38-
protected $isConnected = false;
39-
protected $parameters = [];
40-
protected $logger;
36+
protected ?PDO $pdo = null;
37+
protected ?\PDOStatement $query = null;
38+
protected bool $isConnected = false;
39+
protected array $parameters = [];
40+
protected ?LoggerInterface $logger = null;
4141

4242
/**
4343
* Connect to database.
@@ -53,6 +53,17 @@ public function __construct(array $config = [], ?LoggerInterface $logger = null)
5353
$this->parameters = [];
5454
}
5555

56+
/**
57+
* Destructor - Close connection automatically.
58+
*
59+
* @access public
60+
* @return void
61+
*/
62+
public function __destruct()
63+
{
64+
$this->close();
65+
}
66+
5667
/**
5768
* Close connection.
5869
*
@@ -62,6 +73,7 @@ public function __construct(array $config = [], ?LoggerInterface $logger = null)
6273
public function close() : void
6374
{
6475
$this->pdo = null;
76+
$this->query = null;
6577
}
6678

6779
/**
@@ -87,12 +99,10 @@ public function bind(string $bind, $value = null) : void
8799
*/
88100
public function bindMore(array $bind) : void
89101
{
90-
if ( empty($this->parameters) ) {
91-
if ( TypeCheck::isArray($bind) ) {
92-
$columns = Arrayify::keys($bind);
93-
foreach ($columns as $i => &$column) {
94-
$this->bind($column, $bind[$column]);
95-
}
102+
if ( TypeCheck::isArray($bind) && !empty($bind) ) {
103+
$columns = Arrayify::keys($bind);
104+
foreach ($columns as $column) {
105+
$this->bind($column, $bind[$column]);
96106
}
97107
}
98108
}
@@ -133,11 +143,14 @@ public function query(string $sql, ?array $params = null, int $mode = 2) : mixed
133143
* Get last inserted Id.
134144
*
135145
* @access public
136-
* @return mixed
146+
* @return int|false
137147
*/
138-
public function lastInsertId() : mixed
148+
public function lastInsertId() : int|false
139149
{
140-
return $this->pdo->lastInsertId();
150+
if ( !$this->pdo ) {
151+
return false;
152+
}
153+
return (int)$this->pdo->lastInsertId();
141154
}
142155

143156
/**
@@ -148,6 +161,9 @@ public function lastInsertId() : mixed
148161
*/
149162
public function beginTransaction() : bool
150163
{
164+
if ( !$this->pdo ) {
165+
return false;
166+
}
151167
return $this->pdo->beginTransaction();
152168
}
153169

@@ -159,6 +175,9 @@ public function beginTransaction() : bool
159175
*/
160176
public function executeTransaction() : bool
161177
{
178+
if ( !$this->pdo ) {
179+
return false;
180+
}
162181
return $this->pdo->commit();
163182
}
164183

@@ -170,6 +189,9 @@ public function executeTransaction() : bool
170189
*/
171190
public function rollBack() : bool
172191
{
192+
if ( !$this->pdo ) {
193+
return false;
194+
}
173195
return $this->pdo->rollBack();
174196
}
175197

@@ -267,7 +289,7 @@ protected function connect(array $config = []) : void
267289
} catch (PDOException $e) {
268290
$message = $e->getMessage();
269291
$this->log($message);
270-
exit($message);
292+
throw new PDOException("Database connection failed: " . $message, 0, $e);
271293
}
272294
}
273295

@@ -276,10 +298,10 @@ protected function connect(array $config = []) : void
276298
*
277299
* @access protected
278300
* @param string $sql
279-
* @param array $params
301+
* @param ?array $params
280302
* @return void
281303
*/
282-
protected function init(string $sql, array $params = []) : void
304+
protected function init(string $sql, ?array $params = null) : void
283305
{
284306
// Connect to database
285307
if ( !$this->isConnected ) {
@@ -292,7 +314,9 @@ protected function init(string $sql, array $params = []) : void
292314
$this->query = $this->pdo->prepare($sql);
293315

294316
// Add bind parameters
295-
$this->bindMore($params);
317+
if ( $params !== null ) {
318+
$this->bindMore($params);
319+
}
296320

297321
// Bind parameters
298322
if ( !empty($this->parameters) ) {
@@ -321,7 +345,7 @@ protected function init(string $sql, array $params = []) : void
321345
// Write into log and display exception
322346
$message = $e->getMessage();
323347
$this->log($message, $sql);
324-
exit($message);
348+
throw new PDOException("Query execution failed: " . $message, 0, $e);
325349
}
326350

327351
// Reset bind parameters
@@ -333,9 +357,9 @@ protected function init(string $sql, array $params = []) : void
333357
*
334358
* @access protected
335359
* @param string $sql
336-
* @return mixed
360+
* @return string|false
337361
*/
338-
protected function getStatementType(string $sql = '') : mixed
362+
protected function getStatementType(string $sql = '') : string|false
339363
{
340364
$sql = Stringify::replaceRegex(regex: "/\s+|\t+|\n+/", replace: ' ', subject: $sql);
341365
$raw = explode(' ', $sql);

0 commit comments

Comments
 (0)