mirror of
https://github.com/nextcloud/server.git
synced 2026-04-21 14:23:17 -04:00
Unit tests
Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
parent
814924a787
commit
f732cf1b04
2 changed files with 55 additions and 7 deletions
|
|
@ -32,7 +32,7 @@ class PgSqlFunctionBuilder extends FunctionBuilder {
|
|||
$args = func_get_args();
|
||||
$list = [];
|
||||
foreach ($args as $item) {
|
||||
$list[] = $this->helper->quoteColumnName($item);
|
||||
$list[] = $this->queryBuilder->expr()->castColumn($item, IQueryBuilder::PARAM_STR);
|
||||
}
|
||||
return new QueryFunction(sprintf('(%s)', implode(' || ', $list)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,17 +41,62 @@ class FunctionBuilderTest extends TestCase {
|
|||
$this->connection = \OC::$server->getDatabaseConnection();
|
||||
}
|
||||
|
||||
public function testConcat() {
|
||||
/**
|
||||
* @dataProvider providerTestConcatString
|
||||
*/
|
||||
public function testConcatString($closure) {
|
||||
$query = $this->connection->getQueryBuilder();
|
||||
[$real, $arguments, $return] = $closure($query);
|
||||
if ($real) {
|
||||
$this->addDummyData();
|
||||
$query->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
|
||||
}
|
||||
|
||||
$query->select($query->func()->concat($query->createNamedParameter('foo'), new Literal("'bar'")));
|
||||
$query->select($query->func()->concat(...$arguments));
|
||||
$query->from('appconfig')
|
||||
->setMaxResults(1);
|
||||
|
||||
$result = $query->execute();
|
||||
$column = $result->fetchOne();
|
||||
$result->closeCursor();
|
||||
$this->assertEquals('foobar', $column);
|
||||
$this->assertEquals($return, $column);
|
||||
}
|
||||
|
||||
public function providerTestConcatString(): array {
|
||||
return [
|
||||
'1 column: string param unicode' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->createNamedParameter('👍')], '👍'];
|
||||
}],
|
||||
'2 columns: string param and string param' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->createNamedParameter('foo'), $q->createNamedParameter('bar')], 'foobar'];
|
||||
}],
|
||||
'2 columns: string param and int literal' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->createNamedParameter('foo'), $q->expr()->literal(1)], 'foo1'];
|
||||
}],
|
||||
'2 columns: string param and string literal' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->createNamedParameter('foo'), $q->expr()->literal('bar')], 'foobar'];
|
||||
}],
|
||||
'2 columns: string real and int literal' =>
|
||||
[function ($q) {
|
||||
return [true, ['configkey', $q->expr()->literal(2)], '12'];
|
||||
}],
|
||||
'4 columns: string literal' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->expr()->literal('foo'), $q->expr()->literal('bar'), $q->expr()->literal('foo'), $q->expr()->literal('bar')], 'foobarfoobar'];
|
||||
}],
|
||||
'4 columns: int literal' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->expr()->literal(1), $q->expr()->literal(2), $q->expr()->literal(3), $q->expr()->literal(4)], '1234'];
|
||||
}],
|
||||
'5 columns: string param with special chars used in the function' =>
|
||||
[function ($q) {
|
||||
return [false, [$q->createNamedParameter("b"), $q->createNamedParameter("'"), $q->createNamedParameter('||'), $q->createNamedParameter(','), $q->createNamedParameter('a')], "b'||,a"];
|
||||
}],
|
||||
];
|
||||
}
|
||||
|
||||
protected function clearDummyData(): void {
|
||||
|
|
@ -90,7 +135,6 @@ class FunctionBuilderTest extends TestCase {
|
|||
$result = $query->execute();
|
||||
$column = $result->fetchOne();
|
||||
$result->closeCursor();
|
||||
$this->assertEquals('1,2,3', $column);
|
||||
$this->assertStringContainsString(',', $column);
|
||||
$actual = explode(',', $column);
|
||||
$this->assertEqualsCanonicalizing([1,2,3], $actual);
|
||||
|
|
@ -123,7 +167,9 @@ class FunctionBuilderTest extends TestCase {
|
|||
$result = $query->execute();
|
||||
$column = $result->fetchOne();
|
||||
$result->closeCursor();
|
||||
$this->assertEquals('1\'2\'3', $column);
|
||||
$this->assertStringContainsString("'", $column);
|
||||
$actual = explode("'", $column);
|
||||
$this->assertEqualsCanonicalizing([1,2,3], $actual);
|
||||
}
|
||||
|
||||
public function testGroupConcatWithDoubleQuoteSeparator(): void {
|
||||
|
|
@ -137,7 +183,9 @@ class FunctionBuilderTest extends TestCase {
|
|||
$result = $query->execute();
|
||||
$column = $result->fetchOne();
|
||||
$result->closeCursor();
|
||||
$this->assertEquals('1"2"3', $column);
|
||||
$this->assertStringContainsString('"', $column);
|
||||
$actual = explode('"', $column);
|
||||
$this->assertEqualsCanonicalizing([1,2,3], $actual);
|
||||
}
|
||||
|
||||
protected function clearIntDummyData(): void {
|
||||
|
|
|
|||
Loading…
Reference in a new issue