Page MenuHomeDevCentral

D3775.diff
No OneTemporary

D3775.diff

diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -41,7 +41,7 @@
"replace": {
"keruald/cache": "0.1.0",
"keruald/commands": "0.0.1",
- "keruald/database": "0.5.0",
+ "keruald/database": "0.5.1",
"keruald/github": "0.2.1",
"keruald/omnitools": "0.15.0",
"keruald/report": "0.1.0"
diff --git a/database/src/Exceptions/SqlException.php b/database/src/Exceptions/SqlException.php
--- a/database/src/Exceptions/SqlException.php
+++ b/database/src/Exceptions/SqlException.php
@@ -85,4 +85,12 @@
return new self(null, $query, $context);
}
+ ///
+ /// Getters
+ ///
+
+ public function getQuery () : string {
+ return $this->query;
+ }
+
}
diff --git a/database/tests/Engines/MySQLiEngineTest.php b/database/tests/Engines/MySQLiEngineTest.php
--- a/database/tests/Engines/MySQLiEngineTest.php
+++ b/database/tests/Engines/MySQLiEngineTest.php
@@ -7,6 +7,7 @@
use Keruald\Database\Exceptions\SqlException;
use LogicException;
+use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class MySQLiEngineTest extends TestCase {
@@ -171,8 +172,22 @@
$this->assertSame(1701, $this->db->nextId());
}
- public function testEscape () {
- $this->assertEquals("foo\')", $this->db->escape("foo')"));
+ public static function provideStringsToEscape() : iterable {
+ yield 'empty string' => ['', ''];
+ yield 'simple latin' => ['Lorem ipsum dolor', 'Lorem ipsum dolor'];
+ yield 'apostrophe' => ["L'arbre", "L\\'arbre"];
+ yield 'double apostrophes' => ["''", "\\'\\'"];
+ yield 'unicode' => ['cœur', 'cœur'];
+ yield 'apostrophe and parenthesis' => ["foo')", "foo\\')"];
+ }
+
+ #[DataProvider("provideStringsToEscape")]
+ public function testEscape(string $input, string $expected) : void {
+ $this->assertEquals(
+ $expected,
+ $this->db->escape($input),
+ "The following string isn't properly escaped: '$input'"
+ );
}
public function testGetUnderlyingDriver () {
diff --git a/database/tests/Exceptions/SqlExceptionTest.php b/database/tests/Exceptions/SqlExceptionTest.php
new file mode 100644
--- /dev/null
+++ b/database/tests/Exceptions/SqlExceptionTest.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Keruald\Database\Tests\Exceptions;
+
+use Keruald\Database\Exceptions\SqlException;
+
+use PHPUnit\Framework\TestCase;
+
+class SqlExceptionTest extends TestCase {
+
+ public function testGetQuery () {
+ $sql = 'SELECT 1+';
+ $ex = SqlException::fromQuery($sql, [
+ "error" => 'Syntax error',
+ "errno" => 1064,
+ ]);
+ $this->assertEquals(
+ $sql,
+ $ex->getQuery(),
+ ""
+ );
+
+ $ex = SqlException::fromQuery("", []);
+ $this->assertEquals(
+ "",
+ $ex->getQuery(),
+ "If the query isn't specified during the constructor call, getQuery shall not return null but must return an empty string too."
+ );
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Oct 21, 02:24 (20 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3090993
Default Alt Text
D3775.diff (3 KB)

Event Timeline