Page MenuHomeDevCentral

D3198.id.diff
No OneTemporary

D3198.id.diff

diff --git a/database/README.md b/database/README.md
--- a/database/README.md
+++ b/database/README.md
@@ -34,3 +34,21 @@
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/database/src/Engines/BlackholeEngine.php b/database/src/Engines/BlackholeEngine.php
new file mode 100644
--- /dev/null
+++ b/database/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/database/tests/Engines/BlackholeEngineTest.php b/database/tests/Engines/BlackholeEngineTest.php
new file mode 100644
--- /dev/null
+++ b/database/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());
+ }
+}
diff --git a/phpcs.xml b/phpcs.xml
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -28,7 +28,15 @@
<rule ref="Generic.NamingConventions.CamelCapsFunctionName.NotCamelCaps">
<exclude-pattern>*/_register_to_global_space.php</exclude-pattern>
</rule>
-
+
+ <!--
+ Database exception
+ Allow unused parameters
+ -->
+ <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter">
+ <exclude-pattern>database/src/*</exclude-pattern>
+ </rule>
+
<file>commands/src</file>
<file>commands/tests</file>

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 15:02 (4 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260260
Default Alt Text
D3198.id.diff (4 KB)

Event Timeline