Added tests for sqlite emulated functions

This commit is contained in:
Jan Böhmer 2023-07-26 23:39:53 +02:00
parent d3ead8742e
commit 1d03b6c38d
2 changed files with 101 additions and 8 deletions

View file

@ -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 //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' )) { if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) {
$native_connection->sqliteCreateFunction('REGEXP', $this->regexp(...), 2, \PDO::SQLITE_DETERMINISTIC); $native_connection->sqliteCreateFunction('REGEXP', self::regexp(...), 2, \PDO::SQLITE_DETERMINISTIC);
$native_connection->sqliteCreateFunction('FIELD', $this->field(...), -1, \PDO::SQLITE_DETERMINISTIC); $native_connection->sqliteCreateFunction('FIELD', self::field(...), -1, \PDO::SQLITE_DETERMINISTIC);
$native_connection->sqliteCreateFunction('FIELD2', $this->field2(...), 2, \PDO::SQLITE_DETERMINISTIC); $native_connection->sqliteCreateFunction('FIELD2', self::field2(...), 2, \PDO::SQLITE_DETERMINISTIC);
} }
} }
} }
@ -60,10 +60,11 @@ class SQLiteRegexExtension
* @param string $value * @param string $value
* @return int * @return int
*/ */
private function regexp(string $pattern, string $value): int final public static function regexp(string $pattern, string $value): int
{ {
try { try {
return (mb_ereg($pattern, $value)) ? 1 : 0; return (mb_ereg($pattern, $value)) ? 1 : 0;
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
throw InvalidRegexException::fromMBRegexError($e); throw InvalidRegexException::fromMBRegexError($e);
} }
@ -76,21 +77,21 @@ class SQLiteRegexExtension
* @param string $imploded_array * @param string $imploded_array
* @return int * @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); $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 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. * If the first argument is not found or is NULL, 0 is returned.
* @param string|int|null $value * @param string|int|null $value
* @param mixed ...$array * @param mixed ...$array
* @return int * @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) { if ($value === null) {
return 0; return 0;

View file

@ -0,0 +1,92 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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));
}
}