4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace api\v
4\Query
;
24 use api\v
4\UnitTestCase
;
25 use Civi\Api4\Query\SqlExpression
;
30 class SqlExpressionParserTest
extends UnitTestCase
{
32 public function aggregateFunctions() {
43 * @param string|\Civi\Api4\Query\SqlFunction $fnName
44 * @dataProvider aggregateFunctions
46 public function testAggregateFuncitons($fnName) {
47 $className = 'Civi\Api4\Query\SqlFunction' . $fnName;
48 $params = $className::getParams();
49 $this->assertNotEmpty($params[0]['prefix']);
50 $this->assertEmpty($params[0]['suffix']);
52 $sqlFn = new $className('total');
53 $this->assertEquals($fnName, $sqlFn->getName());
54 $this->assertEquals(['total'], $sqlFn->getFields());
55 $this->assertCount(1, $this->getArgs($sqlFn));
57 $sqlFn = SqlExpression
::convert($fnName . '(DISTINCT stuff)');
58 $this->assertEquals($fnName, $sqlFn->getName());
59 $this->assertEquals("Civi\Api4\Query\SqlFunction$fnName", get_class($sqlFn));
60 $this->assertEquals($params, $sqlFn->getParams());
61 $this->assertEquals(['stuff'], $sqlFn->getFields());
62 $this->assertCount(2, $this->getArgs($sqlFn));
65 $sqlFn = SqlExpression
::convert($fnName . '(*)');
66 if ($fnName === 'COUNT') {
67 $this->assertTrue(is_a($this->getArgs($sqlFn)[0], 'Civi\Api4\Query\SqlWild'));
70 $this->fail('SqlWild should only be allowed in COUNT.');
73 catch (\API_Exception
$e) {
74 $this->assertContains('Illegal', $e->getMessage());
79 * @param \Civi\Api4\Query\SqlFunction $fn
81 * @throws \ReflectionException
83 private function getArgs($fn) {
84 $ref = new \
ReflectionClass($fn);
85 $args = $ref->getProperty('args');
86 $args->setAccessible(TRUE);
87 return $args->getValue($fn);