Merge pull request #17467 from civicrm/5.26
[civicrm-core.git] / tests / phpunit / CRM / Utils / SQLTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_SQLTest
5 * @group headless
6 */
7 class CRM_Utils_SQLTest extends CiviUnitTestCase {
8
9 public function testInterpolate() {
10 // This function is a thin wrapper for `CRM_Utils_SQL_BaseParamQuery::interpolate()`, which already has
11 // lots of coverage in other test classes. This test just checks the basic wiring.
12 $sql = CRM_Utils_SQL::interpolate('FROBNICATE some_table WITH MAX(!dynamicField) OVER #times USING (@list) OR (#ids) OR @item', [
13 '!dynamicField' => 'the(field)',
14 '#times' => 123,
15 '@list' => ['abc def', '45'],
16 '#ids' => [6, 7, 8],
17 '@item' => "it's text",
18 ]);
19 $this->assertEquals('FROBNICATE some_table WITH MAX(the(field)) OVER 123 USING ("abc def", "45") OR (6, 7, 8) OR "it\\\'s text"', $sql);
20 }
21
22 public function testInterpolateBad() {
23 try {
24 CRM_Utils_SQL::interpolate("UPDATE !the_table SET !the_field = @THE_VALUE", [
25 // MISSING: 'the_table'
26 'the_field' => 'my_field',
27 'the_value' => 'ny value',
28 ]);
29 }
30 catch (CRM_Core_Exception $e) {
31 $this->assertRegExp(';Cannot build query. Variable "!the_table" is unknown.;', $e->getMessage());
32 }
33 }
34
35 }