From 1d03b6c38dbd0cfaa168ee287a80969af330a114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 26 Jul 2023 23:39:53 +0200 Subject: [PATCH] Added tests for sqlite emulated functions --- src/Doctrine/SQLiteRegexExtension.php | 17 ++-- tests/Doctrine/SQLiteRegexExtensionTest.php | 92 +++++++++++++++++++++ 2 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 tests/Doctrine/SQLiteRegexExtensionTest.php diff --git a/src/Doctrine/SQLiteRegexExtension.php b/src/Doctrine/SQLiteRegexExtension.php index b1972a85..2407880d 100644 --- a/src/Doctrine/SQLiteRegexExtension.php +++ b/src/Doctrine/SQLiteRegexExtension.php @@ -47,9 +47,9 @@ class SQLiteRegexExtension //Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) { - $native_connection->sqliteCreateFunction('REGEXP', $this->regexp(...), 2, \PDO::SQLITE_DETERMINISTIC); - $native_connection->sqliteCreateFunction('FIELD', $this->field(...), -1, \PDO::SQLITE_DETERMINISTIC); - $native_connection->sqliteCreateFunction('FIELD2', $this->field2(...), 2, \PDO::SQLITE_DETERMINISTIC); + $native_connection->sqliteCreateFunction('REGEXP', self::regexp(...), 2, \PDO::SQLITE_DETERMINISTIC); + $native_connection->sqliteCreateFunction('FIELD', self::field(...), -1, \PDO::SQLITE_DETERMINISTIC); + $native_connection->sqliteCreateFunction('FIELD2', self::field2(...), 2, \PDO::SQLITE_DETERMINISTIC); } } } @@ -60,10 +60,11 @@ class SQLiteRegexExtension * @param string $value * @return int */ - private function regexp(string $pattern, string $value): int + final public static function regexp(string $pattern, string $value): int { try { return (mb_ereg($pattern, $value)) ? 1 : 0; + } catch (\ErrorException $e) { throw InvalidRegexException::fromMBRegexError($e); } @@ -76,21 +77,21 @@ class SQLiteRegexExtension * @param string $imploded_array * @return int */ - private function field2(string|int|null $value, string $imploded_array): int + final public static function field2(string|int|null $value, string $imploded_array): int { $array = explode(',', $imploded_array); - return $this->field($value, ...$array); + return self::field($value, ...$array); } /** * This function emulates the MySQL field function for SQLite - * This function returns the index (position) of the first argument in the subsequent arguments.# + * This function returns the index (position) of the first argument in the subsequent arguments. * If the first argument is not found or is NULL, 0 is returned. * @param string|int|null $value * @param mixed ...$array * @return int */ - private function field(string|int|null $value, ...$array): int + final public static function field(string|int|null $value, ...$array): int { if ($value === null) { return 0; diff --git a/tests/Doctrine/SQLiteRegexExtensionTest.php b/tests/Doctrine/SQLiteRegexExtensionTest.php new file mode 100644 index 00000000..a2098185 --- /dev/null +++ b/tests/Doctrine/SQLiteRegexExtensionTest.php @@ -0,0 +1,92 @@ +. + */ + +namespace App\Tests\Doctrine; + +use App\Doctrine\SQLiteRegexExtension; +use PHPUnit\Framework\TestCase; + +class SQLiteRegexExtensionTest extends TestCase +{ + + public function regexpDataProvider(): \Generator + { + yield [1, 'a', 'a']; + yield [0, 'a', 'b']; + yield [1, 'a', 'ba']; + yield [1, 'a', 'ab']; + yield [1, 'a', 'baa']; + + yield [1, '^a$', 'a']; + yield [0, '^a$', 'ab']; + yield [1, '^a\d+$', 'a123']; + } + + /** + * @dataProvider regexpDataProvider + */ + public function testRegexp(int $expected, string $pattern, string $value): void + { + $this->assertSame($expected, SQLiteRegexExtension::regexp($pattern, $value)); + } + + public function fieldDataProvider(): \Generator + { + yield [ + // Null cases + 0, null, [], + 0, null, [1], + 0, 42, [1, 2], + + // Ints + 1, 1, [1], + 1, 2, [2, 1], + 2, 1, [2, 1], + 2, 2, [2, 1, 2], + 5, 3, [2, 1, 2, 1, 2, 3], + 1, 2, [2, 1, 2, 1, 2, 1, 2, 1, 2, 1], + + // Strings + 1, 'a', ['a'], + 1, 'b', ['b', 'a'], + 2, 'a', ['b', 'a'], + 2, 'b', ['b', 'a', 'b'], + 5, 'c', ['b', 'a', 'b', 'a', 'b', 'c'], + ]; + } + + /** + * @dataProvider fieldDataProvider + */ + public function testField(int $expected, string|int|null $value, array $array): void + { + $this->assertSame($expected, SQLiteRegexExtension::field($value, ...$array)); + } + + /** + * @dataProvider fieldDataProvider + */ + public function testField2(int $expected, string|int|null $value, array $array): void + { + //Should be the same as field, but with the array comma imploded + $string = implode(',', $array); + $this->assertSame($expected, SQLiteRegexExtension::field2($value, $string)); + } +}