Commit | Line | Data |
---|---|---|
f0acec37 CW |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | Copyright CiviCRM LLC. All rights reserved. | | |
6 | | | | |
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 | +--------------------------------------------------------------------+ | |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
16 | * @copyright CiviCRM LLC https://civicrm.org/licensing | |
f0acec37 CW |
17 | */ |
18 | ||
19 | ||
20 | namespace api\v4\Query; | |
21 | ||
22 | use api\v4\UnitTestCase; | |
23 | use Civi\Api4\Query\SqlExpression; | |
24 | ||
25 | /** | |
26 | * @group headless | |
27 | */ | |
28 | class SqlExpressionParserTest extends UnitTestCase { | |
29 | ||
30 | public function aggregateFunctions() { | |
31 | return [ | |
32 | ['AVG'], | |
33 | ['COUNT'], | |
34 | ['MAX'], | |
35 | ['MIN'], | |
36 | ['SUM'], | |
37 | ]; | |
38 | } | |
39 | ||
40 | /** | |
41 | * @param string|\Civi\Api4\Query\SqlFunction $fnName | |
42 | * @dataProvider aggregateFunctions | |
43 | */ | |
44 | public function testAggregateFuncitons($fnName) { | |
45 | $className = 'Civi\Api4\Query\SqlFunction' . $fnName; | |
46 | $params = $className::getParams(); | |
47 | $this->assertNotEmpty($params[0]['prefix']); | |
48 | $this->assertEmpty($params[0]['suffix']); | |
49 | ||
50 | $sqlFn = new $className($fnName . '(total)'); | |
51 | $this->assertEquals($fnName, $sqlFn->getName()); | |
52 | $this->assertEquals(['total'], $sqlFn->getFields()); | |
7ce7b1cd CW |
53 | $args = $sqlFn->getArgs(); |
54 | $this->assertCount(1, $args); | |
55 | $this->assertNull($args[0]['prefix']); | |
56 | $this->assertNull($args[0]['suffix']); | |
57 | $this->assertTrue(is_a($args[0]['expr'][0], 'Civi\Api4\Query\SqlField')); | |
f0acec37 CW |
58 | |
59 | $sqlFn = SqlExpression::convert($fnName . '(DISTINCT stuff)'); | |
60 | $this->assertEquals($fnName, $sqlFn->getName()); | |
61 | $this->assertEquals("Civi\Api4\Query\SqlFunction$fnName", get_class($sqlFn)); | |
62 | $this->assertEquals($params, $sqlFn->getParams()); | |
63 | $this->assertEquals(['stuff'], $sqlFn->getFields()); | |
7ce7b1cd CW |
64 | $args = $sqlFn->getArgs(); |
65 | $this->assertCount(1, $args); | |
66 | $this->assertEquals('DISTINCT', $args[0]['prefix']); | |
67 | $this->assertNull($args[0]['suffix']); | |
68 | $this->assertTrue(is_a($args[0]['expr'][0], 'Civi\Api4\Query\SqlField')); | |
f0acec37 CW |
69 | |
70 | try { | |
71 | $sqlFn = SqlExpression::convert($fnName . '(*)'); | |
72 | if ($fnName === 'COUNT') { | |
7ce7b1cd CW |
73 | $args = $sqlFn->getArgs(); |
74 | $this->assertCount(1, $args); | |
75 | $this->assertNull($args[0]['prefix']); | |
76 | $this->assertNull($args[0]['suffix']); | |
77 | $this->assertTrue(is_a($args[0]['expr'][0], 'Civi\Api4\Query\SqlWild')); | |
f0acec37 CW |
78 | } |
79 | else { | |
80 | $this->fail('SqlWild should only be allowed in COUNT.'); | |
81 | } | |
82 | } | |
83 | catch (\API_Exception $e) { | |
df347a8c | 84 | $this->assertStringContainsString('Illegal', $e->getMessage()); |
f0acec37 CW |
85 | } |
86 | } | |
87 | ||
f0acec37 | 88 | } |