Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3770135
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/README.md b/README.md
index 23b2692..4cc5a29 100644
--- a/README.md
+++ b/README.md
@@ -1,36 +1,54 @@
# keruald/database
This library offers a simple layer of abstraction for database operations.
## Configuration
To get a database instance, you need to pass configuration as an array.
The properties and values depend on the engine you want to use.
### MySQLi
| Key | Value | |
|----------|--------------------------------------|:--------:|
| engine | MySQLiEngine class reference | |
| host | The MySQL hostname, e.g. "localhost" | |
| username | The MySQL user to use for connection | |
| password | The clear text password to use | |
| database | The default db to select for queries | optional |
For example:
```php
[
'engine' => Keruald\Database\Engines\MySQLiEngine::class,
'host' => 'localhost',
'username' => 'app',
'password' => 'someSecret',
'database' => 'app', // optional
]
```
## Legacy drivers
The mysql extension has been deprecated in PHP 5.7 and removed in PHP 7.
As such, this extension isn't supported anymore. You can use straightforwardly
replace 'MySQL' by 'MySQLi' as engine.
+
+## Specialized drivers
+### Blackhole
+
+The blackhole engine does nothing and always returns `true` as query result.
+
+This engine can be used for mocks:
+
+ - directly, when database behavior does not matter
+ - to build a mock by overriding behavior of query() or any other method
+
+It can also be used with the loader, without any engine-specific configuration:
+
+```php
+[
+ 'engine' => Keruald\Database\Engines\BlackholeEngine::class,
+]
+```
diff --git a/src/Engines/BlackholeEngine.php b/src/Engines/BlackholeEngine.php
new file mode 100644
index 0000000..ff67774
--- /dev/null
+++ b/src/Engines/BlackholeEngine.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Keruald\Database\Engines;
+
+use Keruald\Database\DatabaseEngine;
+
+class BlackholeEngine extends DatabaseEngine {
+
+ public function escape (string $expression) : string {
+ return $expression;
+ }
+
+ public function query (string $query) : bool {
+ return true;
+ }
+
+ public function nextId () : int|string {
+ return 0;
+ }
+
+ public function countAffectedRows () : int {
+ return 0;
+ }
+
+ protected function getExceptionContext () : array {
+ return [];
+ }
+
+ public static function load (array $config): DatabaseEngine {
+ return new self;
+ }
+
+ public function getUnderlyingDriver () : mixed {
+ return null;
+ }
+
+ public function isExistingTable (string $database, string $table) : bool {
+ // Everything and nothing exists in a blackhole
+ return true;
+ }
+
+}
diff --git a/tests/Engines/BlackholeEngineTest.php b/tests/Engines/BlackholeEngineTest.php
new file mode 100644
index 0000000..c71dadb
--- /dev/null
+++ b/tests/Engines/BlackholeEngineTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Keruald\Database\Tests\Engines;
+
+use Keruald\Database\Database;
+use Keruald\Database\Engines\BlackholeEngine;
+
+use PHPUnit\Framework\TestCase;
+
+class BlackholeEngineTest extends TestCase {
+
+ private BlackholeEngine $db;
+
+ protected function setUp () : void {
+ $this->db = new BlackholeEngine();
+ }
+
+ public function testGetUnderlyingDriver () {
+ $this->assertNull($this->db->getUnderlyingDriver());
+ }
+
+ public function testQuery () {
+ $this->assertTrue($this->db->query(""));
+ }
+
+ public function testNextId () {
+ $this->assertEquals(0, $this->db->nextId());
+ }
+
+ public function testIsExistingTable () {
+ $this->assertTrue($this->db->isExistingTable("quux", "quuxians"));
+ }
+
+ public function provideStringsToEscape () : iterable {
+ yield ["Lorem ipsum"];
+ yield [""];
+ yield ["\\\\ \n"];
+ }
+
+ /**
+ * @dataProvider provideStringsToEscape
+ */
+ public function testEscape ($string) {
+ $this->assertEquals($string, $this->db->escape($string));
+ }
+
+ public function testLoad () {
+ $config = [
+ 'engine' => BlackholeEngine::class,
+ ];
+ $engine = Database::load($config);
+
+ $this->assertInstanceOf(BlackholeEngine::class, $engine);
+ }
+
+ public function testCountAffectedRows () {
+ $this->assertEquals(0, $this->db->nextId());
+ }
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 19:09 (8 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260811
Default Alt Text
(4 KB)
Attached To
Mode
rKDB Keruald Database
Attached
Detach File
Event Timeline
Log In to Comment